Java宠物管理系统(运用接口简易版)
生活随笔
收集整理的這篇文章主要介紹了
Java宠物管理系统(运用接口简易版)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java練習之寵物管理系統
該寵物管理具有功能:
代碼如下:
Pet類
public class Pet {private String name;private String gender;private String type;private int age;private int price;private int weight;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getType() {return type;}public void setType(String type) {this.type = type;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}public int getWeight() {return weight;}public void setWeight(int weight) {this.weight = weight;}public Pet() {}public Pet(String name, String gender, String type, int age, int price, int weight) {this.name = name;this.gender = gender;this.type = type;this.age = age;this.price = price;this.weight = weight;}public void play(){System.out.printf("寵物:%s\t\t性別:%s\t\t類型:%s\t\t年齡:%d\t\t價格:%d\t\t重量:%d\n",this.name,this.gender,this.type,this.age,this.price,this.weight);} }PetManage接口類:
import java.util.ArrayList;public interface PetManage {/*** 顯示所有寵物的信息*/void showAllPets(ArrayList<Pet> pets);/*** 按寵物的名字查找寵物* @param name* @param pets*/void findPetByName(String name, ArrayList<Pet> pets);/*** 輸入寵物信息* @return*/Pet inputPetInfo();/*** 添加寵物* @param pet* @param pets*/void addPet(Pet pet, ArrayList<Pet> pets);/*** 按名字修改寵物* @param name* @param pets*/void updatePetByName(String name,ArrayList<Pet> pets);/*** 刪除寵物* @param name* @param pets*/void removePetByName(String name,ArrayList<Pet> pets);/*** 按年齡排序* @param pets*/void sortByAge(ArrayList<Pet> pets);/*** 按價格排序* @param pets*/void sortByPrice(ArrayList<Pet> pets);}PetShop類:
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner;public class PetShop implements PetManage{@Overridepublic void showAllPets(ArrayList<Pet> pets) {for(Pet pet:pets){pet.play();}}@Overridepublic void findPetByName(String name, ArrayList<Pet> pets) {boolean flag = false;for(Pet pe : pets){if(pe.getName().equals(name)){pe.play();flag = true;break;}}if(!flag)System.out.println("This pet is not exist");}@Overridepublic Pet inputPetInfo() {Scanner scanner = new Scanner(System.in);System.out.println("輸入寵物的名字");String name = scanner.next();System.out.println("輸入寵物的性別");String gender = scanner.next();System.out.println("輸入寵物的類型");String type = scanner.next();System.out.println("輸入寵物的年齡");int age = scanner.nextInt();System.out.println("輸入寵物的價格");int price = scanner.nextInt();System.out.println("輸入寵物的重量");int weight = scanner.nextInt();return new Pet(name,gender,type,age,price,weight);}@Overridepublic void addPet(Pet pet, ArrayList<Pet> pets) {pets.add(pet);}@Overridepublic void updatePetByName(String name, ArrayList<Pet> pets) {for(Pet p : pets){if(p.getName().equals(name)){pets.remove(p);Pet pet2=inputPetInfo();pets.add(pet2);break;}}}@Overridepublic void removePetByName(String name,ArrayList<Pet> pets) {boolean flag = false;for(Pet pet : pets){if(pet.getName().equals(name)){pets.remove(pet);flag = true;break;}}if(!flag)System.out.println("This pet is not exist");}@Overridepublic void sortByAge(ArrayList<Pet> pets) {Collections.sort(pets,(o1,o2)-> o1.getAge() - o2.getAge());}@Overridepublic void sortByPrice(ArrayList<Pet> pets) {Collections.sort(pets,(o1,o2)->o1.getPrice()-o2.getPrice());} }PetShopApp類:
import java.util.ArrayList; import java.util.Scanner;public class PetShopApp {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);PetShop petshop = new PetShop();ArrayList<Pet> pets = new ArrayList<>();pets.add(new Pet("white","M","dog",1,2000,2));pets.add(new Pet("black","M","dog",2,3000,3));pets.add(new Pet("blue","M","cat",1,1600,4));pets.add(new Pet("flower","F","cat",3,5000,3));pets.add(new Pet("red","F","cat",1,4600,1));for(;;){System.out.println("--------------------歡迎使用寵物管理系統--------------------");System.out.println("請選擇執行的操作: 1.查看所有寵物 2.添加寵物 3.修改寵物 4.查找寵物 " +"5.刪除寵物 6.按年齡排序 7.按價格排序 其他:退出");int op = scanner.nextInt();switch(op){case 1:petshop.showAllPets(pets);break;case 2:Pet pet = petshop.inputPetInfo();petshop.addPet(pet, pets);break;case 3:System.out.println("請輸入要修改的寵物名");String petName = scanner.next();petshop.updatePetByName(petName,pets);break;case 4:System.out.println("請輸入要查找的寵物名");String searchName = scanner.next();petshop.findPetByName(searchName, pets);break;case 5:System.out.println("請輸入要刪除的寵物名");String deleteName = scanner.next();petshop.removePetByName(deleteName,pets);break;case 6:petshop.sortByAge(pets);petshop.showAllPets(pets);break;case 7:petshop.sortByPrice(pets);petshop.showAllPets(pets);break;default: return;}}}}運行展示:
添加寵物:
修改寵物:
查找寵物:
刪除寵物:
按年齡排序:
按價格排序:
總結
以上是生活随笔為你收集整理的Java宠物管理系统(运用接口简易版)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 行列存储方式比较
- 下一篇: 2022华为杯研究生数学建模竞赛B题思路