Java实现宠物商店管理系统
程序员文章站
2022-03-25 11:13:21
本文实例为大家分享了java实现宠物商店管理系统的具体代码,供大家参考,具体内容如下一、实验目的1.掌握java类的继承、多态等的基本概念;2.掌握简单的信息管理系统的设计与实现。二、实验环境实验建议...
本文实例为大家分享了java实现宠物商店管理系统的具体代码,供大家参考,具体内容如下
一、实验目的
1.掌握java类的继承、多态等的基本概念;
2.掌握简单的信息管理系统的设计与实现。
二、实验环境
实验建议在安装了以下软件的计算机上完成:
1. windows xp/win7/win8/win10操作系统
2. jdk 1.6以上版本
3. eclipse或netbeans ide或editplus或其它开发工具
三、实验内容与要求
(一) 问题描述
要求采用java面向对象的基本知识,实现宠物商店管理系统。
(二) 实验要求
1、宠物商店有狗和猫两种动物,请为这两种动物创建各自的类,而且它们都继承宠物类,为这些类定义基本的属性和方法;
2、为宠物商店也创建一个类,该类有基本属性,比如商店名称等,还有宠物笼子的属性,此外,还具备一些方法,比如:买进宠物、销售宠物、清点宠物库存、销售统计和盈利情况等;
3、实现买进宠物的方法,输入狗或猫的基本属性和进货价格,并把该买进的宠物放进宠物笼子;
4、实现销售宠物的方法,输入狗或猫的基本属性和销售价格,并把宠物从宠物笼子取出;
5、实现清点宠物库存方法,列出所有库存的宠物清单;
6、实现销售和盈利统计,查询所有已销售的宠物清单,包括进货价格和销售价格,还有总利润;
四、实现提示
1. 宠物笼子采用数组实现,数组的数据类型为宠物类;
2. 销售清单也采用数组实现。
五、代码
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 getage() { return age; } public void setage(int age) { this.age = 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; } }
cat类
public class cat extends pets{ public cat(string color, int age, string sex) { super(color, age, sex); } public cat() { } }
dog类
public class dog extends pets{ public dog(string color, int age, string sex) { super(color, age, sex); } public dog() { } }
petsstore类
import java.util.scanner; import java.util.date; public class petsstore { scanner input = new scanner(system.in); private string name; private cat[] cats; private dog[] dogs; private pets[] pets; private int dogfoot = 0; // 狗的当前长度 private int catfoot = 0; //猫的当前长度 private int petfoot = 0; private int num = 0; //库存数量 private int innum = 0; //进货数量 private int outnum = 0; //销售数量 public petsstore(int len) { if (len > 0) { this.cats = new cat[len]; // 开辟数组大小 this.dogs = new dog[len]; this.pets = new pets[len]; } else { this.dogs = new dog[1]; // 至少开辟一个空间 this.cats = new cat[1]; this.pets = new pets[1]; } } public string getname() { return name; } public void setname(string name) { this.name = name; } public void add() { // 添加的是一个宠物 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.setinprice(input.nextdouble()); system.out.print("出售价格:"); dog.setoutprice(input.nextdouble()); if(dogfoot < dogs.length) { dogs[dogfoot] = dog; dogfoot++; system.out.println("添加成功!"); innum++; num++; } else { system.out.println("添加失败!"); } } else if(choice.equals("2")) { if(catfoot < cats.length) { 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.setinprice(input.nextdouble()); system.out.print("出售价格:"); cat.setoutprice(input.nextdouble()); cats[catfoot] = cat; catfoot++; system.out.println("添加成功!"); innum++; num++; } else { system.out.println("添加失败!"); } } else { system.out.println("没有这个选项,请重新输入!"); } } public void print() { date date = new date(); system.out.println("===============宠物商店库存清单==============="); system.out.println("*******************c a t s*******************"); system.out.println("color age sex inprice outprice"); for (int i = 0; i < cats.length; i++) { if (cats[i] != null) { system.out.println(cats[i].getcolor() + "\t" + cats[i].getage() + "\t" + cats[i].getsex() + "\t" + cats[i].getinprice() + "\t" + cats[i].getoutprice()); } } system.out.println("\n*******************d o g s*******************"); system.out.println("color age sex inprice outprice"); for (int i = 0; i < dogs.length; i++) { if (dogs[i] != null) { system.out.println(dogs[i].getcolor() + "\t" + dogs[i].getage() + "\t" + dogs[i].getsex() + "\t" + dogs[i].getinprice() + "\t" + dogs[i].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 i, flag = catfoot; for(i = 0; i < catfoot; i++) { if(color1.equals(cats[i].getcolor()) && age1 == cats[i].getage() && sex1.equals(cats[i].getsex())) { flag = i; break; } } if(i == catfoot) { system.out.println("查无此猫!请核对后重新输入 \n"); sell(); } else { pets[petfoot] = cats[i]; pets[petfoot].setkind("cat"); petfoot++; for(int j = flag; j < catfoot; j++) { cats[j] = cats[j + 1]; } system.out.println("售出成功!\n"); catfoot -= 1; //不减1会报数组越界的错误 outnum++; num--; } } 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 i, flag = dogfoot; for(i = 0; i < dogfoot; i++) { if(color1.equals(dogs[i].getcolor()) && age1 == dogs[i].getage() && sex1.equals(dogs[i].getsex())) { flag = i; break; } } if(i == dogfoot) { system.out.println("查无此狗!请核对后重新输入 "); sell(); } else { pets[petfoot].setkind("dog"); pets[petfoot] = dogs[i]; petfoot++; for(int j = flag; j < catfoot; j++) { dogs[j] = dogs[j + 1]; } system.out.println("售出成功!\n"); dogfoot -= 1; //不减1会报数组越界的错误 outnum++; num--; } } 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.length; i++) { if(pets[i] != null) { system.out.println(pets[i].getkind() + "\t" + pets[i].getcolor() + "\t" + pets[i].getage() + "\t" + pets[i].getsex() + "\t" + pets[i].getinprice() + "\t" + pets[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.length; i++) { if(pets[i] != null) { cost += pets[i].getinprice(); income += pets[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; } }
main类
import java.util.scanner; public class main { public static void main(string[] args) { scanner input = new scanner(system.in); petsstore store = new petsstore(100); 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.add(); 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; } } } }
没用很复杂的容器类,只适合萌新看的,大佬勿喷,完成作业还是不错的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。