Java教程

Java宠物管理系统(运用接口简易版)

本文主要是介绍Java宠物管理系统(运用接口简易版),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Java练习之宠物管理系统

该宠物管理具有功能:

  1. 保存大量宠物信息
  2. 查看所有的宠物
  3. 通过名字查看某个宠物
  4. 输入宠物信息,录入系统中
  5. 修改系统中的宠物信息
  6. 删除宠物的信息
  7. 按年龄或价格进行排序

代码如下:

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宠物管理系统(运用接口简易版)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!