1. 程式人生 > 程式設計 >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,String sex) {
 super(color,age,sex);
 }
 
 public Cat() {
 
 }
 
}

Dog類

public class Dog extends Pets{
 
 public Dog(String color,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;
  }
  
 }
 }
 
}

沒用很複雜的容器類,只適合萌新看的,大佬勿噴,完成作業還是不錯的。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。