Java实现宠物商店管理系统
本⽂实例为⼤家分享了Java实现宠物商店管理系统的具体代码,供⼤家参考,具体内容如下
⼀、实验⽬的
1.掌握java类的继承、多态等的基本概念;
2.掌握简单的信息管理系统的设计与实现。
⼆、实验环境
实验建议在安装了以下软件的计算机上完成:
1. Windows xp/win7/win8/win10操作系统
2. JDK 1.6以上版本
3. Eclip或NetBeans IDE或EditPlus或其它开发⼯具
三、实验内容与要求
(⼀) 问题描述
要求采⽤java⾯向对象的基本知识,实现宠物商店管理系统。
(⼆) 实验要求
1、宠物商店有狗和猫两种动物,请为这两种动物创建各⾃的类,⽽且它们都继承宠物类,为这些类定义基本的属性和⽅法;
2、为宠物商店也创建⼀个类,该类有基本属性,⽐如商店名称等,还有宠物笼⼦的属性,此外,还具备⼀些⽅法,⽐如:买进宠物、销售宠物、清点宠物库存、销售统计和盈利情况等;
3、实现买进宠物的⽅法,输⼊狗或猫的基本属性和进货价格,并把该买进的宠物放进宠物笼⼦;
4、实现销售宠物的⽅法,输⼊狗或猫的基本属性和销售价格,并把宠物从宠物笼⼦取出;
5、实现清点宠物库存⽅法,列出所有库存的宠物清单;
6、实现销售和盈利统计,查询所有已销售的宠物清单,包括进货价格和销售价格,还有总利润;
四、实现提⽰
1. 宠物笼⼦采⽤数组实现,数组的数据类型为宠物类;
2. 销售清单也采⽤数组实现。
五、代码
Pet类
public class Pets {
private String color; //颜⾊
private int age; //年龄
private String x; //性别
private String kind;
private double inPrice; //进货价格
private double outPrice; //销售价格
private double profit; //盈利
public Pets(String color, int age, String x) {
this.age = age;
this.x = x;
}
public Pets() {
}
public String getKind() {
职高有什么专业return kind;
}
public void tKind(String kind) {
this.kind = kind;
}
public double getProfit() {
return profit;
}
public void tProfit(double profit) {
this.profit = profit;
}
public String getSex() {
return x;
}
public void tSex(String x) {
this.x = x;
}
public String getColor() {
return color;
}
public void tColor(String color) {
}
public int getAge() {
return age;
}
public void tAge(int age) {
this.age = age;
}
public double getInPrice() {
return inPrice;
}
public void tInPrice(double inPrice) {
this.inPrice = inPrice;
}
public double getOutPrice() {
return outPrice;
}
public void tOutPrice(double outPrice) { this.outPrice = outPrice;
}
}
Cat类
public class Cat extends Pets{
public Cat(String color, int age, String x) { super(color, age, x);
}armchair
public Cat() {
}
}
Dog类
public class Dog extends Pets{
public Dog(String color, int age, String x) { super(color, age, x);
}
level
public Dog() {
}
}
PetsStore类
import java.util.Scanner;
import java.util.Date;
public class PetsStore {
cet 6Scanner 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];
} el {
this.dogs = new Dog[1]; // ⾄少开辟⼀个空间
this.cats = new Cat[1];
this.pets = new Pets[1];
}
}
public String getName() {
return name;
}
public void tName(String name) {
this.name = name;
}
public void add() { // 添加的是⼀个宠物
System.out.println("您添加的是狗还是猫?\n" + "1.狗 2.猫"); String choice = ();
if(choice.equals("1")) {
Dog dog = new Dog();
System.out.println("请输⼊您要添加的宠物的信息");
System.out.print("颜⾊:");
dog.());
System.out.print("年龄:");
dog.Int());
System.out.print("性别:");
dog.());
System.out.print("进货价格:");
dog.Double());
System.out.print("出售价格:");
dog.Double());
if(dogFoot < dogs.length) {
dogs[dogFoot] = dog;
dogFoot++;
System.out.println("添加成功!");
inNum++;
num++;
}
el {
System.out.println("添加失败!");
}
}
el if(choice.equals("2")) {
if(catFoot < cats.length) {
Cat cat = new Cat();
System.out.println("请输⼊您要添加的宠物的信息");
System.out.print("颜⾊:");
cat.());
System.out.print("年龄:");
cat.Int());
System.out.print("性别:");
cat.());
System.out.print("进货价格:");
cat.Double());
System.out.print("出售价格:");
cat.Double());
cats[catFoot] = cat;
catFoot++;
System.out.println("添加成功!");
inNum++;
num++;
}
el {
System.out.println("添加失败!");
}
}
el {
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("=============================================");
maybeSystem.out.println("date: " + date);
}
public void ll() {
if(num == 0) {
System.out.println("库存为零,请及时购进宠物!\n");
}
el {
System.out.println("您要出售的是猫还是狗?\n" + "1.猫 2.狗");
String choice = ();
if(choice.equals("1")) {
System.out.println("请输⼊您要出售的猫的特征");
harmony
System.out.print("颜⾊:");
String color1 = ();
System.out.print("年龄:");
int age1 = Int();
System.out.print("性别:");
String x1 = ();
int i, flag = catFoot;
for(i = 0; i < catFoot; i++) {
if(color1.equals(cats[i].getColor()) && age1 == cats[i].getAge() && x1.equals(cats[i].getSex())) {
flag = i; break;
}
}
if(i == catFoot) {
System.out.println("查⽆此猫!请核对后重新输⼊ \n");
ll();
}
el {
pets[petFoot] = cats[i];
pets[petFoot].tKind("cat");
petFoot++;
for(int j = flag; j < catFoot; j++) {
cats[j] = cats[j + 1];
}
System.out.println("售出成功!\n");
catFoot -= 1; //不减1会报数组越界的错误
outNum++;
num--;
}
}
el if(choice.equals("2")) {
System.out.println("请输⼊您要出售的狗的特征");
System.out.print("颜⾊:");
String color1 = ();
System.out.print("年龄:");
int age1 = Int();
System.out.print("性别:");
String x1 = ();
int i, flag = dogFoot;
for(i = 0; i < dogFoot; i++) {
if(color1.equals(dogs[i].getColor()) && age1 == dogs[i].getAge() && x1.equals(dogs[i].getSex())) {
pamela david
flag = i; break;
}
}
if(i == dogFoot) {
System.out.println("查⽆此狗!请核对后重新输⼊ ");
ll();
}
el {
pets[petFoot].tKind("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--;
dfx}
}
el {
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("恭喜,您的店处于盈利状态!");
}
el {
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) {