Java宠物管理系统(运用接口简易版)
2021/7/18 1:06:27
本文主要是介绍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{ @Override public void showAllPets(ArrayList<Pet> pets) { for(Pet pet:pets){ pet.play(); } } @Override public 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"); } @Override public 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); } @Override public void addPet(Pet pet, ArrayList<Pet> pets) { pets.add(pet); } @Override public 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; } } } @Override public 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"); } @Override public void sortByAge(ArrayList<Pet> pets) { Collections.sort(pets,(o1,o2)-> o1.getAge() - o2.getAge()); } @Override public 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宠物管理系统(运用接口简易版)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16使用vue3+springboot构建简单Web应用教程
- 2024-11-15全栈开发项目实战:从入门到初级项目的实现
- 2024-11-15数据库项目实战:从入门到初级应用教程
- 2024-11-15IDEA项目实战入门教程
- 2024-11-15IT编程项目实战:新手入门的全面指南
- 2024-11-15Java开发项目实战:新手入门与初级技巧
- 2024-11-15Java零基础项目实战:从入门到独立开发
- 2024-11-15MyBatis Plus教程:入门与基础操作详解
- 2024-11-15MyBatis-Plus教程:新手入门与实战技巧
- 2024-11-15MyBatis教程:从入门到实践