宠物交易系统,分为宠物主人和宠物商店两种角色,宠物主人可以购买宠物或将已有的宠物卖给商店,宠物商店可以购买宠物、卖出宠物、培育宠物、查询待售宠物、查看商店账目和开设新的宠物商店。系统启动后会显示所有宠物主人、宠物和宠物商店信息,并提示两种角色登录。编写Java代码
时间: 2024-03-06 08:48:05 浏览: 17
好的,以下是一个简单的Java代码示例,实现了宠物交易系统的基本功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class PetTradeSystem {
// 宠物类
public static class Pet {
private String type; // 宠物类型
private String name; // 宠物名字
private int age; // 宠物年龄
private int health; // 宠物健康值
private int love; // 宠物爱心指数
private String shop; // 所属商店
public Pet(String type, String name, int age, int health, int love, String shop) {
this.type = type;
this.name = name;
this.age = age;
this.health = health;
this.love = love;
this.shop = shop;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getHealth() {
return health;
}
public int getLove() {
return love;
}
public String getShop() {
return shop;
}
@Override
public String toString() {
return "宠物类型:" + type + ",宠物名字:" + name + ",宠物年龄:" + age + "岁,宠物健康值:" + health + ",宠物爱心指数:" + love + ",所属商店:" + shop;
}
}
// 宠物主人类
public static class PetOwner {
private String name; // 主人名字
private int money; // 主人账户余额
private ArrayList<Pet> pets; // 主人拥有的宠物
public PetOwner(String name, int money) {
this.name = name;
this.money = money;
this.pets = new ArrayList<>();
}
public String getName() {
return name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public ArrayList<Pet> getPets() {
return pets;
}
public void addPet(Pet pet) {
pets.add(pet);
}
public void removePet(Pet pet) {
pets.remove(pet);
}
@Override
public String toString() {
return "主人名字:" + name + ",账户余额:" + money;
}
}
// 宠物商店类
public static class PetShop {
private String name; // 商店名字
private String password; // 商店密码
private int money; // 商店账户余额
private ArrayList<Pet> pets; // 商店拥有的宠物
public PetShop(String name, String password, int money) {
this.name = name;
this.password = password;
this.money = money;
this.pets = new ArrayList<>();
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public ArrayList<Pet> getPets() {
return pets;
}
public void addPet(Pet pet) {
pets.add(pet);
}
public void removePet(Pet pet) {
pets.remove(pet);
}
@Override
public String toString() {
return "商店名字:" + name + ",账户余额:" + money;
}
}
private static Scanner sc = new Scanner(System.in);
private static ArrayList<PetOwner> petOwners = new ArrayList<>();
private static ArrayList<PetShop> petShops = new ArrayList<>();
private static ArrayList<Pet> pets = new ArrayList<>();
private static ArrayList<String> logs = new ArrayList<>(); // 记录交易日志
public static void main(String[] args) {
// 添加几只宠物
pets.add(new Pet("狗", "旺财", 3, 80, 90, "宠物之家"));
pets.add(new Pet("猫", "咪咪", 2, 90, 80, "宠物之家"));
pets.add(new Pet("鸟", "小鸟", 1, 70, 95, "宠物之家"));
// 添加几个宠物主人
petOwners.add(new PetOwner("张三", 100));
petOwners.add(new PetOwner("李四", 50));
// 添加几个宠物商店
petShops.add(new PetShop("宠物之家", "123456", 500));
petShops.add(new PetShop("宠物乐园", "654321", 300));
while (true) {
System.out.println("欢迎使用宠物交易系统,请选择登录角色:");
System.out.println("1. 宠物主人");
System.out.println("2. 宠物商店");
System.out.println("3. 退出系统");
int choice = sc.nextInt();
sc.nextLine(); // 读取换行符
switch (choice) {
case 1:
petOwnerLogin();
break;
case 2:
petShopLogin();
break;
case 3:
System.out.println("感谢使用宠物交易系统,再见!");
System.exit(0);
default:
System.out.println("无效选项,请重新选择!");
break;
}
}
}
// 宠物主人登录
private static void petOwnerLogin() {
System.out.println("请输入主人名字:");
String name = sc.nextLine();
for (PetOwner petOwner : petOwners) {
if (petOwner.getName().equals(name)) {
System.out.println("欢迎回来," + name + "!");
petOwnerOperate(petOwner);
return;
}
}
System.out.println("主人名字不存在,请重新选择登录角色!");
}
// 宠物主人操作
private static void petOwnerOperate(PetOwner petOwner) {
while (true) {
System.out.println("请选择功能:");
System.out.println("1. 购买宠物");
System.out.println("2. 卖出宠物");
System.out.println("3. 返回登录界面");
int choice = sc.nextInt();
sc.nextLine(); // 读取换行符
switch (choice) {
case 1:
buyPet(petOwner);
break;
case 2:
sellPet(petOwner);
break;
case 3:
return;
default:
System.out.println("无效选项,请重新选择!");
break;
}
}
}
// 宠物商店登录
private static void petShopLogin() {
System.out.println("请输入商店名字:");
String name = sc.nextLine();
System.out.println("请输入商店密码:");
String password = sc.nextLine();
for (PetShop petShop : petShops) {
if (petShop.getName().equals(name) && petShop.getPassword().equals(password)) {
System.out.println("欢迎回来," + name + "!");
petShopOperate(petShop);
return;
}
}
System.out.println("商店名字或密码错误,请重新选择登录角色!");
}
// 宠物商店操作
private static void petShopOperate(PetShop petShop) {
while (true) {
System.out.println("请选择功能:");
System.out.println("1. 购买宠物");
System.out.println("2. 卖出宠物");
System.out.println("3. 培育宠物");
System.out.println("4. 查询待售宠物");
System.out.println("5. 查看商店结余");
System.out.println("6. 查看商店账目");
System.out.println("7. 开设新的宠物商店");
int choice = sc.nextInt();
sc.nextLine(); // 读取换行符
switch (choice) {
case 1:
buyPet(petShop);
break;
case 2:
sellPet(petShop);
break;
case 3:
breedPet(petShop);
break;
case 4:
showPets(petShop.getPets());
break;
case 5:
System.out.println("商店账户余额:" + petShop.getMoney());
break;
case 6:
showLogs(petShop.getName());
break;
case 7:
createPetShop();
break;
default:
System.out.println("无效选项,请重新选择!");
break;
}
}
}
// 购买宠物
private static void buyPet(Object obj) {
ArrayList<Pet> availablePets = new ArrayList<>();
for (Pet pet : pets) {
if (pet.getShop().equals("宠物之家") || obj instanceof PetShop && pet.getShop().equals(((PetShop) obj).getName())) {
availablePets.add(pet);
}
}
showPets(availablePets);
System.out.println("请选择要购买的宠物编号:");
int index = sc.nextInt();
sc.nextLine(); // 读取换行符
Pet pet = availablePets.get(index - 1);
if (pet.getAge() <= 5) {
petOwners.get(0).setMoney(petOwners.get(0).getMoney() - 5);
if (obj instanceof PetShop) {
((PetShop) obj).setMoney(((PetShop) obj).getMoney() + 5);
} else {
pet.setShop(((PetOwner) obj).getName());
}
logs.add("购买宠物:" + pet.getName() + ",价格:5元");
} else {
petOwners.get(0).setMoney(petOwners.get(0).getMoney() - 3);
if (obj instanceof PetShop) {
((PetShop) obj).setMoney(((PetShop) obj).getMoney() + 3);
} else {
pet.setShop(((PetOwner) obj).getName());
}
logs.add("购买宠物:" + pet.getName() + ",价格:3元");
}
if (obj instanceof PetShop) {
((PetShop) obj).addPet(pet);
} else {
((PetOwner) obj).addPet(pet);
}
System.out.println("购买成功!");
}
// 卖出宠物
private static void sellPet(Object obj) {
PetOwner petOwner = null;
PetShop petShop = null;
if (obj instanceof PetOwner) {
petOwner = (PetOwner) obj;
} else {
petShop = (PetShop) obj;
}
showPets(petOwner != null ? petOwner.getPets() : petShop.getPets());
System.out.println("请选择要卖出的宠物编号:");
int index = sc.nextInt();
sc.nextLine(); // 读取换行符
Pet pet = petOwner != null ? petOwner.getPets().get(index - 1) : petShop.getPets
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)