首页 > 编程语言 >Java零基础-面向对象入门案例:宠物商店

Java零基础-面向对象入门案例:宠物商店

时间:2022-11-10 16:14:05浏览次数:43  
标签:Java scanner pet System 面向对象 println 宠物商店 id out

Java零基础-面向对象入门案例:宠物商店

案例需求:

设计宠物类,包含有:编号、名称、年龄、种类、性别、是否防疫等信息,

设计宠物商店,实现宠物添加、删除、修改宠物信息,记录宠物是否出售等

Pet类

package pet;

public class Pet {
    private Integer id;
    private String name;
    private Integer age;
    private String kind;
    private String sex;
    private String isEpidemicPrevention;
    public Pet() {}
    // get、set方法
    ...
}

PetStore

package pet;

import java.util.*;

public class PetStore {
    private ArrayList<Pet> arrayList;
    private Map<Integer,String> map;

    public PetStore() {
        this.arrayList = new ArrayList<>();
        this.map = new HashMap<>();
    }

    /**
     * 添加宠物进入商店
     * @return 商店中已存在id一致的宠物则返回false
     */
    public Pet selectPet(int pet_id){
        for (Pet pet : arrayList) {
            if (pet_id == pet.getId()){
                return pet;
            }
        }
        return null;
    }

    /**
     * 在商店中查询宠物信息
     * @return 商店中不存在该宠物则返回false
     */
    public boolean addPet(Pet pet){
        if(selectPet(pet.getId()) != null){
            return false;
        }
        arrayList.add(pet);
        map.put(pet.getId(),"否");
        return true;
    }

    /**
     * 将宠物从商店删除
     * @return 商店中若不存在该宠物id则返回false
     */
    public boolean delPet(int pet_id){
        Pet pet = selectPet(pet_id);
        if(pet == null){
            return false;
        }
        arrayList.remove(pet);
        map.remove(pet_id);
        return true;
    }

    /**
     * 将宠物信息修改
     * @return 商店中若不存在该宠物id则返回false
     */
    public Pet updPet(int pet_id,Pet changeInfo){
        Pet pet = selectPet(pet_id);
        if(pet == null){
            return null;
        }

        pet.setAge(changeInfo.getAge() == null ? pet.getAge() : changeInfo.getAge());
        pet.setKind(changeInfo.getKind() == null ? pet.getKind() : changeInfo.getKind());
        pet.setName(changeInfo.getName() == null ? pet.getName() : changeInfo.getName());
        pet.setSex(changeInfo.getSex() == null ? pet.getSex() : changeInfo.getSex());
        pet.setEpidemicPrevention(changeInfo.getEpidemicPrevention() == null ? pet.getEpidemicPrevention() : changeInfo.getEpidemicPrevention());
        return pet;
    }

    /**
     * 记录宠物是否售卖
     * @return 商店中若不存在该宠物id则返回false
     */
    public boolean recordSell(Integer pet_id,String str){
        if(selectPet(pet_id) == null){
            return false;
        }
        map.put(pet_id,str);
        return true;

    }

    /**
     * 查询宠物是否售卖
     * @return 商店中若不存在该宠物id则返回false
     */
    public String selectSell(Integer pet_id){

        return map.get(pet_id);

    }


}

Start:实现页面显示

package pet;

import java.util.Scanner;

public class Start {
    public static void showStore(){
        System.out.println("==========    宠物商店    ==========");
        System.out.println("========== 1、查询宠物信息 ==========");
        System.out.println("========== 2、添加宠物信息 ==========");
        System.out.println("========== 3、删除宠物信息 ==========");
        System.out.println("========== 4、修改宠物信息 ==========");
        System.out.println("========== 5、登记宠物出售 ==========");
        System.out.println("========== 6、查询宠物出售 ==========");
        System.out.println("========== 0、退出宠物商店 ==========");
    }

    public static void receiveInfo(PetStore petStore){
        Scanner scanner = new Scanner(System.in);
        boolean exit = true;
        int storeOperate;
        while(exit){
            storeOperate = scanner.nextInt();
            // 增强性Switch - 新特性
            switch (storeOperate) {
                case 0 -> exit = false;
                case 1 -> selectPet(petStore, scanner);
                case 2 -> addPet(petStore, scanner);
                case 3 -> delPet(petStore, scanner);
                case 4 -> updPet(petStore, scanner);
                case 5 -> recordSell(petStore, scanner);
                case 6 -> selectSell(petStore, scanner);
                default -> System.out.println("输入错误请重试");
            }
        }
    }

    private static void selectSell(PetStore petStore, Scanner scanner) {
        int pet_id = inputPetID(scanner);
        String state = petStore.selectSell(pet_id);
        if (state != null){
            System.out.println("宠物出售状态" + petStore.selectSell(pet_id));
        } else {
            System.out.println("宠物不存在");
        }
    }

    private static void recordSell(PetStore petStore, Scanner scanner) {
        int pet_id = inputPetID(scanner);
        System.out.println("是否已经出售该宠物");
        String isSell = scanner.next();
        petStore.recordSell(pet_id,isSell);
        if (petStore.recordSell(pet_id,isSell)){
            System.out.println("登记成功");
        } else {
            System.out.println("宠物编号不存在,请重试!");
        }
    }

    private static void updPet(PetStore petStore, Scanner scanner) {
        int pet_id = inputPetID(scanner);
        Pet pet = new Pet();
        System.out.println("请需要修改的宠物信息:");
        System.out.print("姓名:");
        pet.setName(scanner.next());
        System.out.print("年龄:");
        pet.setAge(scanner.nextInt());
        System.out.print("性别:");
        pet.setSex(scanner.next());
        System.out.print("品种:");
        pet.setKind(scanner.next());
        System.out.print("是否防疫:");
        pet.setEpidemicPrevention(scanner.next());

        Pet upPet = petStore.updPet(pet_id,pet);
        if (upPet != null){
            System.out.println("修改成功,宠物信息如下:");
            System.out.println("编号:" + upPet.getId());
            System.out.println("姓名:" + upPet.getName());
            System.out.println("年龄:" + upPet.getAge());
            System.out.println("性别:" + upPet.getSex());
            System.out.println("品种:" + upPet.getKind());
            System.out.println("是否防疫:" + upPet.getEpidemicPrevention());
        } else {
            System.out.println("宠物编号不存在,请重试!");
        }
    }

    private static void delPet(PetStore petStore, Scanner scanner) {
        int pet_id = inputPetID(scanner);
        if (petStore.delPet(pet_id)){
            System.out.println("该宠物已被删除");
        } else {
            System.out.println("宠物编号不存在,请重试!");
        }
    }

    private static void addPet(PetStore petStore, Scanner scanner) {
        Pet pet = new Pet();
        System.out.println("请输入宠物信息:");
        System.out.print("编号:");
        pet.setId(scanner.nextInt());
        System.out.print("姓名:");
        pet.setName(scanner.next());
        System.out.print("年龄:");
        pet.setAge(scanner.nextInt());
        System.out.print("性别:");
        pet.setSex(scanner.next());
        System.out.print("品种:");
        pet.setKind(scanner.next());
        System.out.print("是否防疫:");
        pet.setEpidemicPrevention(scanner.next());
        if(petStore.addPet(pet)){
            System.out.println("添加成功");
        } else{
            System.out.println("添加失败");
        }
    }

    private static void selectPet(PetStore petStore, Scanner scanner) {
        int pet_id = inputPetID(scanner);
        Pet pet = petStore.selectPet(pet_id);
        if (pet != null){
            System.out.println("宠物信息如下:");
            System.out.println("编号:" + pet.getId());
            System.out.println("姓名:" + pet.getName());
            System.out.println("年龄:" + pet.getAge());
            System.out.println("性别:" + pet.getSex());
            System.out.println("品种:" + pet.getKind());
            System.out.println("是否防疫:" + pet.getEpidemicPrevention());
        } else {
            System.out.println("宠物编号不存在,请重试!");
        }
    }

    private static int inputPetID(Scanner scanner) {
        int pet_id;
        System.out.println("请输入宠物编号");
        pet_id = scanner.nextInt();
        return pet_id;
    }


    public static void main(String[] args) {
        PetStore petStore = new PetStore();
        showStore();
        receiveInfo(petStore);

    }
}

标签:Java,scanner,pet,System,面向对象,println,宠物商店,id,out
From: https://www.cnblogs.com/L-TT/p/16877363.html

相关文章

  • 学习JAVA的第二天-JAVA开发工具-ideaIC 免费+汉化Chinese ​(Simplified)​ Language
    学习JAVA的第二天开发工具安装开发工具我本来下载的 ideaIU收费的问了一下我同事,他用的社区版,使用体验ok,最重要的是免费。下载IntelliJIDEA:JetBrains功能强大、......
  • Elasticsearch自定义评分+折叠Java实现
    QueryBuilderboolQueryBuilder=query.getBoolQueryBuilder(localInfoRequest,QueryEnum.termsQuery); FunctionScoreQueryBuilder.FilterFunctionBuilder[]filterF......
  • 【java八股】ArrayList自动扩容过程
    ArrayList是一个数组类型的存储容器,默认大小是10个容量的数组,容量也可以在构件ArrayList的时候通过构造器指定大小,当容量不够时会进行自动扩容到原来的1.5倍,由于底层是数组......
  • JavaSE实现国际化操作(十五)
    勿以恶小而为之,勿以善小而不为--------------------------刘备劝诸君,多行善事积福报,莫作恶上一章简单介绍了Java批量修改文件名称(十四)),如果没有看过,​​请观看上一章​​......
  • java 判断string是否为数字的方法
    1、用JAVA自带的函数1publicstaticbooleanisNumeric(Stringstr){2for(inti=0;i<str.length();i++){3System.out.println(st......
  • linux安装java
    1.在/usr下新建java2.上传安装包到java,并解压tar-xvfjdk....tar.gz3.ln-s/usr/java/jdk1.8.0_351/usr/jdk 设置链接,节约目录长度4.vim/etc/profile 5. 配置......
  • eclipse搭建第一个java web应用
    Eslicpe安装下载地址,https://www.eclipse.org/downloads/tomcat服务器安装下载地址,https://tomcat.apache.org/download-80.cgiDynamicwebproject如果你的软件里没有它,请......
  • 用java做一个内存缓存
    项目中对接第三方系统需要先获取认证token后,才能调用其他接口,token的有效期(固定为1小时),如果使用redis来做,十分简单,设置redis缓存加上1个小时有效期就可以解决。现在需要自......
  • 【JAVA基础】常量变量维护
    常量维护//参与签名的系统Header前缀,只有指定前缀的Header才会参与到签名中//换行符privatestaticfinalcharLF='\n';publicstaticfinalLong......
  • javaScript中的数组
    javaScript中的数组之前我们已经简单了解过数组了,现在我们开始详细的学习数组,数组就是存放一组数据的优雅变量创建数组的两种方法:let数组名=newArray()letarr=newA......