Java宠物商店管理系统代码
时间: 2023-07-14 11:58:41 浏览: 218
当然,我可以为您提供一个简单的Java宠物商店管理系统的代码示例。请注意,这只是一个基本的示例,可能需要根据您的具体需求进行进一步的修改和优化。
```java
import java.util.ArrayList;
import java.util.Scanner;
class Pet {
private String name;
private String species;
public Pet(String name, String species) {
this.name = name;
this.species = species;
}
public String getName() {
return name;
}
public String getSpecies() {
return species;
}
public void setName(String name) {
this.name = name;
}
public void setSpecies(String species) {
this.species = species;
}
}
class PetShop {
private ArrayList<Pet> pets;
public PetShop() {
pets = new ArrayList<>();
}
public void addPet(Pet pet) {
pets.add(pet);
}
public void removePet(Pet pet) {
pets.remove(pet);
}
public void displayPets() {
System.out.println("Pets in the shop:");
for (Pet pet : pets) {
System.out.println(pet.getName() + " - " + pet.getSpecies());
}
}
}
public class PetShopManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PetShop petShop = new PetShop();
boolean exit = false;
while (!exit) {
System.out.println("\n===== Pet Shop Management System =====");
System.out.println("1. Add a pet");
System.out.println("2. Remove a pet");
System.out.println("3. Display pets");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 0:
exit = true;
break;
case 1:
System.out.print("Enter pet name: ");
String name = scanner.next();
System.out.print("Enter pet species: ");
String species = scanner.next();
Pet pet = new Pet(name, species);
petShop.addPet(pet);
System.out.println("Pet added successfully!");
break;
case 2:
System.out.print("Enter pet name to remove: ");
String petName = scanner.next();
boolean found = false;
for (Pet p : petShop.getPets()) {
if (p.getName().equals(petName)) {
petShop.removePet(p);
found = true;
System.out.println("Pet removed successfully!");
break;
}
}
if (!found) {
System.out.println("Pet not found!");
}
break;
case 3:
petShop.displayPets();
break;
default:
System.out.println("Invalid choice!");
}
}
scanner.close();
}
}
```
这段代码实现了一个简单的宠物商店管理系统。用户可以选择添加宠物、删除宠物或显示宠物列表。您可以根据需要进行进一步的扩展和修改。
希望这个示例对您有所帮助!如有任何疑问,请随时提问。
阅读全文