一.开发环境
Windows 11 -- JDK 21 -- IDEA 2021.3.3
二.需求
三.代码部分
//创建一个宠物类,被另外两类继承
public class Pet {
private String name;
private int age;
private String gender;
private double cost=0;//买进价格
private double sellprice=0;//卖出价格
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public double getSellprice() {
return sellprice;
}
public void setSellprice(int sellprice) {
this.sellprice = sellprice;
}
public Pet(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
public class Dog extends Pet{
private String breed;//品种
public Dog(String name, int age, String gender,String breed) {
super(name, age, gender);
this.breed=breed;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
}
public class Cat extends Pet{
private String isIndoor;//猫类的一个关键属性是否习惯在家里待着
public Cat(String name, int age, String gender,String isIndoor) {
super(name, age, gender);
this.isIndoor=isIndoor;
}
public String getIsIndoor() {
return isIndoor;
}
public void setIsIndoor(String isIndoor) {
this.isIndoor = isIndoor;
}
}
import java.util.ArrayList;
import java.util.Scanner;
class petshop{
private String name;
private ArrayList<Pet>stocks;//宠物的库存
private ArrayList<Pet>sales;//记录销售的信息
private double revenue;//商店的流动资金
public petshop(String name){
this.name=name;
this.stocks=new ArrayList<>();
this.sales=new ArrayList<>();
this.revenue=30000;
}
public void buy(Pet pet,double cost){
pet.setCost(cost);
stocks.add(pet);
this.revenue-=cost;
}
public void BuyRecord(Pet pet,double cost){
pet.setCost(cost);
sales.add(pet);
}
//实现检查库存-猫的基本信息
public void chekStocks_Cat(){
System.out.println("店内库存-猫的基本信息");
System.out.println("----------------------------------------");
System.out.println("姓名:\t年龄:\t性别:\t备注:\t成本:\t");
for(Pet pet:stocks){
if(pet instanceof Cat){
Cat cat =(Cat) pet;
System.out.println(cat.getName()+"\t"+cat.getAge()+"\t"+cat.getGender()+"\t"+cat.getIsIndoor()+"\t"+cat.getCost());
}
}
}
//实现检查库存-狗的基本信息
public void chekStocks_Dog(){
System.out.println("店内库存-狗的基本信息");
System.out.println("----------------------------------------");
System.out.println("姓名:\t年龄:\t性别:\t品种:\t成本:\t");
for(Pet pet:stocks){
if(pet instanceof Dog){
Dog dog =(Dog) pet;
System.out.println(dog.getName()+"\t"+dog.getAge()+"\t"+dog.getGender()+"\t"+dog.getBreed()+"\t"+dog.getCost());
}
}
}
//通过输入名字卖出库存中的宠物同时记录这个信息
public boolean removePetByName(String name,int sellprice){
for(Pet pet:stocks){
if(pet.getName().equals(name)){
stocks.remove(pet);
recordSellpriceByName(name,sellprice);
revenue+=sellprice;
return true;
}
}
return false;
}
//通过名字来修改销售表中的宠物sellprice
public void recordSellpriceByName(String name,int sellprice){
for(Pet pet:sales){
if(pet.getName().equals(name)){
pet.setSellprice(sellprice);
}
}
}
public void printSaleReport(){ //实现统计销售和盈利
double totalProfit=0;
for(Pet pet:sales){
if(pet instanceof Dog){ //检查一个pet的对象是否为dog
Dog dog =(Dog) pet;//Pet 对象强制转换为 Dog 类型,并将结果存储在名为 dog 的变量中。现在,你就可以调用 Dog 类的方法或访问其属性
totalProfit+=(dog.getSellprice()-dog.getCost());
System.out.println("Dog "+"名字: "+dog.getName()+" 进价:"+dog.getCost()+" ¥ "+" 售出:"+dog.getSellprice()+" ¥ "+" 净利润:"+(dog.getSellprice()-dog.getCost())+" ¥ ");
}else if(pet instanceof Cat){
Cat cat=(Cat)pet;
totalProfit+=(cat.getSellprice()-cat.getCost());
System.out.println("Cat "+"名字: "+cat.getName()+" 进价:"+cat.getCost()+" ¥ "+" 售出:"+cat.getSellprice()+" ¥ "+" 净利润:"+(cat.getSellprice()-cat.getCost())+" ¥ ");
}
}
System.out.println("总利润:"+totalProfit+" ¥");
System.out.println("商店流动资金:"+revenue);
}
public void addPet(Pet pet){
stocks.add(pet);
}
}
public class Pet_Manager {
public static void printmenu(){
System.out.println("---------------------------------------");
System.out.println("欢迎宠物商店管理系统");
System.out.println("---------------------------------------");
System.out.println("1. 买进宠物");
System.out.println("2. 卖出宠物");
System.out.println("3. 清点宠物库存");
System.out.println("4. 统计销售和盈利情况");
System.out.println("0. 退出系统");
System.out.println("---------------------------------------");
System.out.print("请输入操作序号:");
}
public static void prinemenu2(){
//System.out.println("----------------------------------------");
System.out.println("选择一:买进宠物狗");
System.out.println("选择二:买进宠物猫");
System.out.println("----------------------------------------");
}
public static void prinemenu3(){
//System.out.println("----------------------------------------");
System.out.println("选择一:卖出宠物狗");
System.out.println("选择二:卖出宠物猫");
System.out.println("----------------------------------------");
}
//-----------------主函数
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
petshop petmanager = new petshop("爪印乐园");
while (true){
printmenu();
int opeartion = scanner.nextInt();
if (opeartion < 0 || opeartion > 4) {
System.out.println("没有这个选项,请重新选择!");
scanner.nextLine();
continue;
}
scanner.nextLine();
switch (opeartion){
case 1:{
System.out.println("您正在使用功能一:买进宠物");
System.out.println("---------------------------------------");
prinemenu2();
System.out.print("请输入操作序号:");
int opertation2=scanner.nextInt();
System.out.println("---------------------------------------");
scanner.nextLine();
switch (opertation2){
case 1:{
System.out.print("请输入狗的名字:");
String name = scanner.nextLine();
System.out.println("---------------------------------------");
System.out.print("请输入狗的年龄:");
int age = scanner.nextInt();
System.out.println("---------------------------------------");
scanner.nextLine();
System.out.print("请输入狗的性别:");
String gender =scanner.nextLine();
System.out.println("---------------------------------------");
System.out.print("请输入狗的品种:");
String breed =scanner.nextLine();
System.out.println("---------------------------------------");
System.out.print("请输入买进宠物价格:");
int cost= scanner.nextInt();
Dog newDog = new Dog(name,age,gender,breed);
petmanager.buy(newDog,cost);//买入存入库存
//同时要记录这个销售信息
petmanager.BuyRecord(newDog,cost);
break;
}
case 2:{
System.out.print("请输入猫的名字:");
String name = scanner.nextLine();
System.out.println("---------------------------------------");
System.out.print("请输入猫的年龄:");
int age = scanner.nextInt();
System.out.println("---------------------------------------");
scanner.nextLine();
System.out.print("请输入猫的性别:");
String gender =scanner.nextLine();
System.out.println("---------------------------------------");
System.out.print("请输入猫是否喜欢呆在家里|| 喜欢/不喜欢 :");
String isIndoor = scanner.nextLine()+"呆在家里";
System.out.println("---------------------------------------");
System.out.print("请输入买进宠物价格:");
int cost= scanner.nextInt();
Cat newCat = new Cat(name,age,gender,isIndoor);
petmanager.buy(newCat,cost);//买入存入库存
//同时要记录这个销售的信息;
petmanager.BuyRecord(newCat,cost);
break;
}
default:break;
}
break;
}
case 2:{
System.out.println("您正在使用功能二:卖出宠物");
System.out.println("---------------------------------------");
prinemenu3();
System.out.print("请输入操作序号:");
int opertation2=scanner.nextInt();
System.out.println("---------------------------------------");
scanner.nextLine();
switch (opertation2) {
case 1:{
petmanager.chekStocks_Dog();
System.out.println("---------------------------------------");
System.out.print("请输入卖出的宠物狗的名字:");
String name =scanner.nextLine();
System.out.println("---------------------------------------");
System.out.print("请输入卖出的宠物狗的价格:");
int sellprice = scanner.nextInt();
//同时移除库存中宠物的数据和记录销售记录
boolean isRemoved = petmanager.removePetByName(name,sellprice);
if(isRemoved){
System.out.println("---------------------------------------");
System.out.println("成功卖出名为 " + name + " 的宠物");
}else {
System.out.println("---------------------------------------");
System.out.println("没有找到改宠物");
}
scanner.nextLine();
break;
}
case 2:{
petmanager.chekStocks_Cat();
System.out.println("---------------------------------------");
System.out.print("请输入卖出的宠物猫的名字:");
String name =scanner.nextLine();
System.out.println("---------------------------------------");
System.out.print("请输入卖出的宠物猫的价格:");
int sellprice = scanner.nextInt();
//同时移除库存中宠物的数据和记录销售记录
boolean isRemoved = petmanager.removePetByName(name,sellprice);
if(isRemoved){
System.out.println("---------------------------------------");
System.out.println("成功卖出名为 " + name + " 的宠物");
}else {
System.out.println("---------------------------------------");
System.out.println("没有找到改宠物");
}
scanner.nextLine();
break;
}
default:break;
}
break;
}
case 3:{
System.out.println("您正在使用功能三:清点宠物库存");
System.out.println("---------------------------------------");
petmanager.chekStocks_Dog();
System.out.println("---------------------------------------");
petmanager.chekStocks_Cat();
scanner.nextLine();
break;
}
case 4:{
System.out.println("您正在使用功能四:统计销售情况");
System.out.println("---------------------------------------");
petmanager.printSaleReport();
scanner.nextLine();
break;
}
case 0:{
System.exit(0);
}
}
}
}
}