Java實現(xiàn)寵物商店管理系統(tǒng)
本文實例為大家分享了Java實現(xiàn)寵物商店管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
一、實驗?zāi)康?/strong>
1.掌握java類的繼承、多態(tài)等的基本概念;
2.掌握簡單的信息管理系統(tǒng)的設(shè)計與實現(xiàn)。
二、實驗環(huán)境
實驗建議在安裝了以下軟件的計算機上完成:
1. Windows xp/win7/win8/win10操作系統(tǒng)
2. JDK 1.6以上版本
3. Eclipse或NetBeans IDE或EditPlus或其它開發(fā)工具
三、實驗內(nèi)容與要求
(一) 問題描述
要求采用java面向?qū)ο蟮幕局R,實現(xiàn)寵物商店管理系統(tǒng)。
(二) 實驗要求
1、寵物商店有狗和貓兩種動物,請為這兩種動物創(chuàng)建各自的類,而且它們都繼承寵物類,為這些類定義基本的屬性和方法;
2、為寵物商店也創(chuàng)建一個類,該類有基本屬性,比如商店名稱等,還有寵物籠子的屬性,此外,還具備一些方法,比如:買進(jìn)寵物、銷售寵物、清點寵物庫存、銷售統(tǒng)計和盈利情況等;
3、實現(xiàn)買進(jìn)寵物的方法,輸入狗或貓的基本屬性和進(jìn)貨價格,并把該買進(jìn)的寵物放進(jìn)寵物籠子;
4、實現(xiàn)銷售寵物的方法,輸入狗或貓的基本屬性和銷售價格,并把寵物從寵物籠子取出;
5、實現(xiàn)清點寵物庫存方法,列出所有庫存的寵物清單;
6、實現(xiàn)銷售和盈利統(tǒng)計,查詢所有已銷售的寵物清單,包括進(jìn)貨價格和銷售價格,還有總利潤;
四、實現(xiàn)提示
1. 寵物籠子采用數(shù)組實現(xiàn),數(shù)組的數(shù)據(jù)類型為寵物類;
2. 銷售清單也采用數(shù)組實現(xiàn)。
五、代碼
Pet類
public class Pets { private String color; //顏色 private int age; //年齡 private String sex; //性別 private String kind; private double inPrice; //進(jìn)貨價格 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; // 狗的當(dāng)前長度 private int catFoot = 0; //貓的當(dāng)前長度 private int petFoot = 0; private int num = 0; //庫存數(shù)量 private int inNum = 0; //進(jìn)貨數(shù)量 private int outNum = 0; //銷售數(shù)量 public PetsStore(int len) { if (len > 0) { this.cats = new Cat[len]; // 開辟數(shù)組大小 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("進(jìn)貨價格:"); 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("進(jìn)貨價格:"); 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("庫存為零,請及時購進(jìn)寵物!\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會報數(shù)組越界的錯誤 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會報數(shù)組越界的錯誤 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("恭喜,您的店處于盈利狀態(tài)!"); } 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() + "的店長,歡迎使用寵物商店管理系統(tǒng)!"); String choice = "1"; while(choice.equals("0") == false) { System.out.println("==========寵物商店管理系統(tǒng)=========="); 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("請問您還需要什么服務(wù)?"); break; case "2": String choice1 = "1"; do { store.add(); System.out.println("是否繼續(xù)添加?\n" + "1.是 2.否"); choice1 = input.next(); } while(choice1.equals("1")); System.out.println("請問您還需要什么服務(wù)?"); break; case "3": String choice2 = "1"; do { store.sell(); System.out.println("是否繼續(xù)出售?\n" + "1.是 2.否"); choice2 = input.next(); } while(choice2.equals("1")); System.out.println("請問您還需要什么服務(wù)?"); break; case "4": store.profitNote(); System.out.println("請問您還需要什么服務(wù)?"); break; case "5": store.note(); System.out.println("請問您還需要什么服務(wù)?"); break; case "0": System.out.println("謝謝您的使用,歡迎下次再來!\n" + "**********按任意鍵結(jié)束程序**********"); break; default: System.out.println("錯誤輸入, 請重新輸入!"); break; } } } }
沒用很復(fù)雜的容器類,只適合萌新看的,大佬勿噴,完成作業(yè)還是不錯的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于LinkedList集合對元素進(jìn)行增查刪操作
LinkedList集合內(nèi)部包含有兩個Node類型的first和last屬性維護(hù)一個雙向循環(huán)鏈表,在鏈表中的每一個元素都使用引用的方式來記住它的前一個元素和后一個元素,從而可以將所有的元素彼此連接起來,需要的朋友可以參考下2023-04-04SpringBoot Java后端實現(xiàn)okhttp3超時設(shè)置的方法實例
Okhttp的使用沒有httpClient廣泛,網(wǎng)上關(guān)于Okhttp設(shè)置代理的方法很少,下面這篇文章主要給大家介紹了關(guān)于SpringBoot Java后端實現(xiàn)okhttp3超時設(shè)置的相關(guān)資料,需要的朋友可以參考下2021-10-10關(guān)于Springboot日期時間格式化處理方式總結(jié)
這篇文章主要介紹了關(guān)于Springboot日期時間格式化處理方式總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03DecimalFormat數(shù)字格式化 0和# 的區(qū)別及說明
這篇文章主要介紹了DecimalFormat數(shù)字格式化 0和# 的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10SpringCloud maven-assembly-plugin 多級目錄打包的實現(xiàn)
本文主要介紹了SpringCloud maven-assembly-plugin 多級目錄打包的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10java 直接調(diào)用python腳本,并傳遞參數(shù)代碼實例
這篇文章主要介紹了java調(diào)用python腳本傳遞參數(shù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04使用spring動態(tài)獲取接口的不同實現(xiàn)類
這篇文章主要介紹了使用spring動態(tài)獲取接口的不同實現(xiàn)類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02