2022/7/10 作业:图书管理

package com.hp;

public class Library {
    private int id;//编号
    private String title;//书名
    private String author;//作者
    private double price;//价格
    private String desc;//描述
    //tostring方法
    @Override
    public String toString() {
        return "Library{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", desc='" + desc + '\'' +
                '}';
    }
    //有参无参
    public Library(int id, String title, String author, double price, String desc) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.price = price;
        this.desc = desc;
    }
    public Library() {
    }
//get和set方法
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

package com.hp;

import java.util.Scanner;

public class Bookstore {
    private Library[]lis = new Library[10];//书架
    private int num;//图书种类
    public Bookstore() {
        this.lis[0] = new Library(1, "西游记", "吴承恩", 58, "名著");
        this.lis[1] = new Library(2, "安徒生童话", "安徒生", 38, "儿童教育");
        num = 2;
    }
        private void show(){
            System.out.println("-------------  书架列表  --------------");
            if(this.num==0){
                System.out.println("警告:没有商品..");
                return;
            }

            for (int i = 0; i < this.lis.length; i++) {
                if(this.lis[i]!=null){
                    System.out.println(this.lis[i]);
                }
            }
            System.out.println("------------------------------------------");
        }

        //菜单方法(柜台都有功能选择)
        public void main(){
            while(true) {
                System.out.println("---------------图书菜单-----------------");
                System.out.println("-------  1.图书列表    2.上架图书    ------");
                System.out.println("-------  3.下架图书    4.调整价格    ------");
                System.out.println("-------  0.退出                    ------");
                //选择功能,控制台输入
                System.out.println("--请输入功能编号:");
                int key = InputUtils.getNum();
                switch (key) {
                    case 1:
                        show();
                        break;
                    case 2:
                        add();
                        break;
                    case 3:
                        delete();
                        break;
                    case 4:
                        update();
                        break;
                    case 0:
                        System.exit(0);//退出JVM
                        System.out.println("退出菜单");
                }
            }
        }
    private void add() {
        if(this.num==this.lis.length){
            System.out.println("--警告:柜台已满,不能再添加商品了");
            return;
        }

        System.out.println("--请输入图书id:");
        int liId = InputUtils.BookstoreId();
        //丰富:id不能重复
        for (int i = 0; i < this.lis.length; i++) {
            if(this.lis[i]!=null && this.lis[i].getId()==liId){
                System.out.println("--警告:当前编号已存在!!请重新输入");
                add();
                return;//由于重复了,后续不执行
            }
        }

        //程序能走到这里,说明id没有重复
        System.out.println("--提示:图书id可以使用!!");

        System.out.println("--请输入图书名称:");
        String title = new Scanner(System.in).next();

        System.out.println("--请输入图书作者:");
        String author = new Scanner(System.in).next();

        System.out.println("--请输入图书价格:");
        double price = InputUtils.Price();

        System.out.println("--请输入图书描述:");
        String desc = new Scanner(System.in).next();


        Library newli = new Library(liId ,title,author, price, desc);

        //把商品对象保存柜台数组中为null的位置
        for (int i = 0; i < this.lis.length; i++) {
            //如果这个下标没有Goods商品对象,null,就把商品放进去
            if(this.lis[i]==null){
                this.lis[i] = newli;
                this.num++;//商品数量加一
                System.out.println("--提示:图书上架成功!!图书种类"+this.num);
                return;
            }
        }
    }
    private void delete() {
        System.out.println("--请输入要下架的图书编号:");
        int goodsId = InputUtils.BookstoreId();
        //第一件事?找到这个对应商品ID的商品对象
        for (int i = 0; i < this.lis.length; i++) {
            //查找对应id的商品
            if(this.lis[i]!=null && this.lis[i].getId()==goodsId){
                //找到以后,删除?
                this.lis[i] = null;
                //商品数量减一
                this.num--;
                System.out.println("--提示:下架成功!!当前图书种类"+this.num);
                //还需不需要继续循环?
                return;//跳出方法(只有找到商品才执行)
            }
        }
        //如果没找到商品,就会执行到这里
        System.out.println("-->警告:未找到图书!!");
    }
    private void update() {
        Library li = new Library();
        System.out.println("请输入要修改的图书id");
        int liId = new Scanner(System.in).nextInt();
        for (int i = 0; i < this.lis.length; i++) {
            if (this.lis[i].getId() != liId) {
                System.out.println("查无此编号!请重新输入");
                update();
                return;//发生错误,后续代码不执行
            } else {
                System.out.println("--请输入要修改商品价格");
                double price = InputUtils.Price();

                li.setId(liId);
                li.setPrice(price);

                for (int j = 0; j < this.lis.length; j++) {
                    if (this.lis[j].getPrice() != price) {
                        System.out.println("修改成功");
                        lis[j] = li;
                        return;
                    }
                }
            }
        }
    }
    }

package com.hp;

public class BookstoreTest {
    public static void main(String[] args) {
        Bookstore bookstore  = new Bookstore();
        bookstore.main();
    }
}

package com.hp;

import java.util.Scanner;

public class InputUtils {

        /**
         * 获取我们的功能序号
         * @return
         */
        public static int getNum(){
            int n = 0;
            try {
                n = new Scanner(System.in).nextInt();
            } catch (Exception e) {
                //e.printStackTrace();
                //如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
                System.out.println("-->警告:输入非法数字!!请重新输入");
                n = getNum();//递归
            }
            //必须是0-4
            if(n<0||n>4){
                System.out.println("--警告:非法命令!!请重新输入");
                n = getNum();//递归
            }
            return n;//返回输入的数字
        }


        /**
         * 获取输入的商品编号
         * @return
         */
        public static int BookstoreId(){
            int n = 0;
            try {
                n = new Scanner(System.in).nextInt();
            } catch (Exception e) {
                //e.printStackTrace();
                //如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
                System.out.println("-->警告:请重新输入");
                n = BookstoreId();//递归
            }
            return n;//返回输入的数字
        }

        /**
         * 获取输入的商品编号
         * @return
         */
        public static double Price(){
            double n = 0;
            try {
                n = new Scanner(System.in).nextDouble();
            } catch (Exception e) {
                //e.printStackTrace();
                //如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
                System.out.println("-->警告:输入非法数字!!请重新输入");
                n = Price();//递归
            }
            return n;//返回输入的数字
        }
    }


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值