package pet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
PetsStore store = new PetsStore();
System.out.print(“请为您的宠物商店取个名字:”);
store.setName(input.nextLine());
System.out.println(“您好!” + store.getName() + “的店长,欢迎使用宠物商店管理系统!”);
String choice = “1”;
while (choice.equals(“0”) == false) {
System.out.println(“==========宠物商店管理系统==========”);
System.out.println(“1.查看库存\n” + “2.添加宠物\n” + “3.出售宠物\n” + “4.查看盈利\n” + “5.销售记录\n” + “0.退出程序”);
System.out.println(“===================================”);
System.out.print(“请输入你的选择:”);
choice = input.next();
switch (choice) {
case “1”:
store.print();
System.out.println(“请问您还需要什么服务?”);
break;
case “2”:
String choice1 = “1”;
do {
store.addPets();
System.out.println(“是否继续添加?\n” + “1.是 2.否”);
choice1 = input.next();
} while (choice1.equals(“1”));
System.out.println(“请问您还需要什么服务?”);
break;
case “3”:
String choice2 = “1”;
do {
store.sell();
System.out.println(“是否继续出售?\n” + “1.是 2.否”);
choice2 = input.next();
} while (choice2.equals(“1”));
System.out.println(“请问您还需要什么服务?”);
break;
case “4”:
store.profitNote();
System.out.println(“请问您还需要什么服务?”);
break;
case “5”:
store.note();
System.out.println(“请问您还需要什么服务?”);
break;
case “0”:
System.out.println(“谢谢您的使用,欢迎下次再来!\n” + “**********按任意键结束程序**********”);
break;
default:
System.out.println(“错误输入, 请重新输入!”);
break;
}
}
}
}
——————————————————————————————————————————–
package pet;
public class Pets {
private String color; // 颜色
private int age; // 年龄
private String sex; // 性别
private String kind;// 性情
private double inPrice; // 进货价格
private double outPrice; // 销售价格
private double profit; // 盈利
public Pets(String color, int age, String sex) {
this.color = color;
this.age = age;
this.sex = sex;
}
public Pets() {
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public double getProfit() {
return profit;
}
public void setProfit(double profit) {
this.profit = profit;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int cat() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public double getInPrice() {
return inPrice;
}
public void setInPrice(double inPrice) {
this.inPrice = inPrice;
}
public double getOutPrice() {
return outPrice;
}
public void setOutPrice(double outPrice) {
this.outPrice = outPrice;
}
}
——————————————————————————————————————————–
package pet;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class PetsStore {
Scanner input = new Scanner(System.in);
private String name; // 店名
private int num = 0; // 库存数量
private int inNum = 0; // 进货数量
private int outNum = 0; // 销售数量
ArrayList<Cat> cats = new ArrayList<Cat>();
ArrayList<Dog> dogs = new ArrayList<Dog>();
ArrayList<Pets> pets = new ArrayList<Pets>();
public PetsStore() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addPets() { // 添加的是一个宠物
System.out.println(“您添加的是狗还是猫?\n” + “1.狗 2.猫”);
String choice = input.next();
if (choice.equals(“1”)) {
Dog dog = new Dog();
System.out.println(“请输入您要添加的宠物的信息”);
System.out.print(“颜色:”);
dog.setColor(input.next());
System.out.print(“年龄:”);
dog.setAge(input.nextInt());
System.out.print(“性别:”);
dog.setSex(input.next());
System.out.print(“性情:”);
dog.setKind(input.next());
System.out.print(“进货价格:”);
dog.setInPrice(input.nextDouble());
System.out.print(“出售价格:”);
dog.setOutPrice(input.nextDouble());
dogs.add(dog);// 将狗添加到猫集合中
System.out.println(“添加成功!”);
inNum++;
num++;
} else if (choice.equals(“2”)) {
Cat cat = new Cat();
System.out.println(“请输入您要添加的宠物的信息”);
System.out.print(“颜色:”);
cat.setColor(input.next());
System.out.print(“年龄:”);
cat.setAge(input.nextInt());
System.out.print(“性别:”);
cat.setSex(input.next());
System.out.print(“性情:”);
cat.setKind(input.next());
System.out.print(“进货价格:”);
cat.setInPrice(input.nextDouble());
System.out.print(“出售价格:”);
cat.setOutPrice(input.nextDouble());
cats.add(cat);// 将猫添加到猫集合中
System.out.println(“添加成功!”);
inNum++;
num++;
} else {
System.out.println(“没有这个选项,请重新输入!”);
}
}
public void print() {
Date date = new Date();
System.out.println(“===============宠物商店库存清单===============”);
System.out.println(“*******************CATS*******************”);
System.out.println(“Color Age Sex InPrice OutPrice”);
for (Cat cat : cats) {
System.out.println(cat.getColor() + “\t” + cat.getAge() + “\t” + cat.getSex() + “\t” + cat.getInPrice()
+ “\t” + cat.getOutPrice());
}
System.out.println(“\n*******************DOGS*******************”);
System.out.println(“Color Age Sex InPrice OutPrice”);
for (Dog dog : dogs) {
System.out.println(dog.getColor() + “\t” + dog.getAge() + “\t” + dog.getSex() + “\t” + dog.getInPrice()
+ “\t” + dog.getOutPrice());
}
System.out.println(“=============================================”);
System.out.println(“date: ” + date);
}
public void sell() {
if (num == 0) {
System.out.println(“库存为零,请及时购进宠物!\n”);
} else {
System.out.println(“您要出售的是猫还是狗?\n” + “1.猫 2.狗”);
String choice = input.next();
if (choice.equals(“1”)) {
System.out.println(“请输入您要出售的猫的特征”);
System.out.print(“颜色:”);
String color1 = input.next();
System.out.print(“年龄:”);
int age1 = input.nextInt();
System.out.print(“性别:”);
String sex1 = input.next();
int flag = cats.size() + 1;// 用于判断是否找到过
for (int i = 0; i < cats.size(); i++) {
if (color1.equals(cats.get(i).getColor()) && age1 == cats.get(i).getAge()
&& sex1.equals(cats.get(i).getSex())) {
flag = i;// 如果找到则改变flag的值
pets.add(cats.get(i));// 将找到的猫放入宠物清单中
cats.remove(i);// 如果找到,则从猫集合中删除该猫的信息
System.out.println(“售出成功!\n”);
outNum++;// 售出数量+1
num–;// 库存-1
break;
}
}
if (flag > cats.size()) {
// 如果没找到,flag的值将不变还是cats.size()+1
System.out.println(“查无此猫!请核对后重新输入 \n”);
sell();// 重新出售
}
} else if (choice.equals(“2”)) {
System.out.println(“请输入您要出售的狗的特征”);
System.out.print(“颜色:”);
String color1 = input.next();
System.out.print(“年龄:”);
int age1 = input.nextInt();
System.out.print(“性别:”);
String sex1 = input.next();
int flag = dogs.size() + 1;// 用于判断是否找到过
for (int i = 0; i < dogs.size(); i++) {
if (color1.equals(dogs.get(i).getColor()) && age1 == dogs.get(i).getAge()
&& sex1.equals(dogs.get(i).getSex())) {
flag = i;// 如果找到则改变flag的值
pets.add(dogs.get(i));// 将找到的狗放入宠物清单中
dogs.remove(i);// 如果找到,则从狗猫集合中删除该猫的信息
System.out.println(“售出成功!\n”);
outNum++;// 售出数量+1
num–;// 库存-1
break;
}
}
if (flag > dogs.size()) {
// 如果没找到,flag的值将不变还是dogs.size()+1
System.out.println(“查无此狗!请核对后重新输入 \n”);
sell();// 重新出售
}
} else {
System.out.println(“没有这个选项,请重新输入!”);
}
}
}
public void note() {
Date date = new Date();
System.out.println(“===============宠物商店销售记录清单===============”);
System.out.println(“Kind Color Age Sex InPrice OutPrice”);
for (int i = 0; i < pets.size(); i++) {
if (pets.get(i) != null) {
System.out.println(pets.get(i).getKind() + “\t” + pets.get(i).getColor() + “\t” + pets.get(i).getAge()
+ “\t” + pets.get(i).getSex() + “\t” + pets.get(i).getInPrice() + “\t”
+ pets.get(i).getOutPrice());
}
}
System.out.println(“=================================================”);
System.out.println(“date: ” + date);
}
public void profitNote() {
Date date = new Date();
System.out.println(“===========宠物商店盈利情况===========”);
double cost = 0.0;
double income = 0.0;
double myProfit = 0.0;
for (int i = 0; i < pets.size(); i++) {
if (pets.get(i) != null) {
cost += pets.get(i).getInPrice();
income += pets.get(i).getOutPrice();
}
}
myProfit = income – cost;
System.out.println(“成本:” + cost + “\n” + “总收入:” + income + “\n” + “利润:” + myProfit);
if (myProfit > 0) {
System.out.println(“恭喜,您的店处于盈利状态!”);
} else {
System.out.println(“很遗憾,您的店处于亏损状况!”);
}
System.out.println(“=======================================”);
System.out.println(“date: ” + date);
}
public int getOutNum() {
return outNum;
}
public int getInNum() {
return inNum;
}
public int getNum() {
return num;
}
}
——————————————————————————————————————————–
package pet;
public class Cat extends Pets {
public Cat(String color, int age, String sex) {
super(color, age, sex);
}
public Cat() {
}
}
——————————————————————————————————————————–
package pet;
public class Dog extends Pets {
public Dog(String color, int age, String sex) {
super(color, age, sex);
}
public Dog() {
}
}