C#宠物领养系统,代码
时间: 2023-09-08 13:14:57 浏览: 161
以下是一个基于 C# 的宠物领养系统的示例代码:
```csharp
using System;
using System.Collections.Generic;
namespace PetAdoptionSystem
{
class Program
{
static void Main(string[] args)
{
List<Pet> availablePets = new List<Pet>();
List<Pet> adoptedPets = new List<Pet>();
// 添加可领养的宠物
availablePets.Add(new Pet("Dog", "Buddy", 2, "Male", "Golden Retriever"));
availablePets.Add(new Pet("Cat", "Fluffy", 1, "Female", "Persian"));
availablePets.Add(new Pet("Bird", "Tweety", 3, "Male", "Parakeet"));
Console.WriteLine("Welcome to the Pet Adoption System!");
Console.WriteLine();
while (true)
{
Console.WriteLine("Please select an option:");
Console.WriteLine("1. View available pets");
Console.WriteLine("2. View adopted pets");
Console.WriteLine("3. Adopt a pet");
Console.WriteLine("4. Exit");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
Console.WriteLine();
Console.WriteLine("Available pets:");
foreach (Pet pet in availablePets)
{
Console.WriteLine(pet);
}
Console.WriteLine();
break;
case "2":
Console.WriteLine();
Console.WriteLine("Adopted pets:");
foreach (Pet pet in adoptedPets)
{
Console.WriteLine(pet);
}
Console.WriteLine();
break;
case "3":
Console.WriteLine();
Console.WriteLine("Available pets:");
foreach (Pet pet in availablePets)
{
Console.WriteLine(pet);
}
Console.WriteLine();
Console.Write("Enter the name of the pet you want to adopt: ");
string petName = Console.ReadLine();
Pet selectedPet = null;
foreach (Pet pet in availablePets)
{
if (pet.Name == petName)
{
selectedPet = pet;
break;
}
}
if (selectedPet != null)
{
adoptedPets.Add(selectedPet);
availablePets.Remove(selectedPet);
Console.WriteLine("Congratulations! You have adopted " + selectedPet.Name + ".");
}
else
{
Console.WriteLine("Sorry, there is no pet with that name available for adoption.");
}
Console.WriteLine();
break;
case "4":
Console.WriteLine();
Console.WriteLine("Thank you for using the Pet Adoption System. Goodbye!");
return;
default:
Console.WriteLine();
Console.WriteLine("Invalid choice. Please try again.");
Console.WriteLine();
break;
}
}
}
}
class Pet
{
public string Species { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string Breed { get; set; }
public Pet(string species, string name, int age, string gender, string breed)
{
Species = species;
Name = name;
Age = age;
Gender = gender;
Breed = breed;
}
public override string ToString()
{
return $"{Name} ({Species}, {Age} years old, {Gender}, {Breed})";
}
}
}
```
该系统使用一个 `Pet` 类来表示宠物,包括物种、名字、年龄、性别和品种属性。在主程序中,有一个可领养宠物列表和一个已领养宠物列表。用户可以通过菜单选项来查看可领养和已领养的宠物,领养宠物或退出系统。领养宠物时,用户需要输入宠物的名字,系统会检查该名字是否在可领养宠物列表中,如果是,则将该宠物从可领养宠物列表中移除并添加到已领养宠物列表中。
请注意,以上代码仅为示例,实际的宠物领养系统可能需要更多的功能和验证。
阅读全文