java宠物商店_Java实现宠物商店管理系统

本文实例为大家分享了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[dogFoot] = dog;

dogFoot++;

System.out.println(“添加成功!”);

inNum++;

num++;

}

else {

System.out.println(“添加失败!”);

}

}

else if(choice.equals(“2”)) {

if(catFoot

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

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

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

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

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

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

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

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

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;

}

}

}

}

没用很复杂的容器类,只适合萌新看的,大佬勿喷,完成作业还是不错的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持免费资源网。

文章知识点与官方知识档案匹配,可进一步学习相关知识Java技能树首页概览92888 人正在系统学习中 相关资源:c#编写的鸡兔同笼程序

来源:南大鳥

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月17日
下一篇 2021年1月17日

相关推荐