JavaCore-OOP-3-图书管理系统(完善版)

来源:互联网 发布:淘宝网签证 编辑:程序博客网 时间:2024/06/05 08:17

话说

各位读者下午好,上一篇我们写了那个最最最原始的图书管理系统,在后来又优化了下,这次再次优化下。主要是把整个思路都优化的得非常明朗啦。

开发环境:Eclipse

目录


1.整体效果图
2.接口BookManagementI
3.实现类BookManagementImpl
4.总结


1.整体效果图

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

2.接口

BookManagementI

package com.hmc.bookmanagement;/****2017年11月26日*User:Meice*下午3:29:41*/public interface BookManagementI {    /**     * CURD四个功能     */    public void showAllBook();    public void addBook(Book book);    public void delBook(String book_name);    public void editBook(String book_name);    public void buyBook();}

3.实现类

实体类和上一篇相同
BookManagementImpl

package com.hmc.bookmanagement;import java.text.DecimalFormat;import java.util.InputMismatchException;import java.util.Scanner;import org.junit.Test;/****2017年11月26日*User:Meice*下午3:38:19*/public class BookManagementSystemImpl  implements BookManagementI{    //存放图书 相当于书架~    Book[] BookList = new Book[100];    //定义购物车    Book[] buiedBooks = new Book[20];    //定义购买总金额    double totalPrice = 0;    static BookManagementSystemImpl bms = new BookManagementSystemImpl();    public BookManagementSystemImpl() {        Book book1 = new Book(1, "《笑傲江湖》", 66.66, "金庸");        Book book2 = new Book(2, "《三国演义》", 55.4322, "罗贯中");        Book book3 = new Book(3, "《资治通鉴》", 23.4567, "Author");        Book book4 = new Book(4, "《Java从入门到放弃》", 45.678, "古灵精怪");        Book book5 = new Book(5, "《悲惨世界》", 11.123, "雨果");        BookList[0] = book1;        BookList[1] = book2;        BookList[2] = book3;        BookList[3] = book4;        BookList[4] = book5;    }    public static void main(String[] args) {        do {            bms.welcomeEnter();            switch(bms.getChooseNumber()) {                case 1:                    //TODO showAllBook()                    bms.showAllBook();                    break;                case 2:                    //TODO addBook()                    bms.addBook(bms.getOneBook());                    break;                case 3:                    //TODO delBook()                    bms.showAllBook();                    bms.delBook(bms.getOneBookName());                    break;                case 4:                    //TODO editBook()                    bms.showAllBook();                    bms.editBook(bms.getOneBookName());                    break;                case 5:                    //TODO buyBook()                    bms.showAllBook();                    bms.buyBook();                    break;                case 6:                    //TODO Exit                    System.out.println("谢谢惠顾!欢迎下次光临! *.* ");                    return;                default:                    //TODO others                    System.out.println("序号不存在,请输入正确序号:");                    break;            }        }while(bms.isContinue("是否继续(y/n)?","再会!期待下次相逢~"));    }    /**     * 方法1:欢迎界面     * welcomeEnter()     */    public void welcomeEnter() {        System.out.println("******************欢迎进入图书管理系统*************************");        System.out.println("1.查看图书列表\n2.新增图书\n3.删除图书\n4.编辑图书\n5.购买图书\n6.退出");        System.out.println("*********************************************************");    }    /**     * 方法2:返回用户选择数字     * getChooseNumber()     */    public int getChooseNumber() {        System.out.println("根据选项,输入对应序号:");        try {            return new Scanner(System.in).nextInt();        }catch(InputMismatchException e) {            System.out.println("输入有误,请重新输入正确序号:");            try {                return new Scanner(System.in).nextInt();            }catch(InputMismatchException e2) {                System.out.println("多次输入错误,程序终止。请向管理员【小美】求职吧!");                return 6;            }        }    }    /**     * 方法3:用户自行决定是否执行相关操作     * isContinue()     */    public boolean isContinue(String startInfo,String notContinueInfo) {        System.out.println(startInfo);        String choice = new Scanner(System.in).next();        if("y".equals(choice)|| "Y".equals(choice)) {            return true;        }else if("n".equals(choice)||"N".equals(choice)){            System.out.println(notContinueInfo);            return false;        }else {            System.out.println("您输入有误,重新输入y或Y表示继续;n或N表示停止:");            String choice2 = new Scanner(System.in).next();             if("y".equals(choice2)|| "Y".equals(choice2)) {                 return true;             }else if("n".equals(choice2)||"N".equals(choice2)) {                 System.out.println(notContinueInfo);                 return false;             }else {                 System.out.println("多次输入有误,程序自动终止.请联系管理员【小美】帮助您吧~");                 return false;             }        }    }    /**     * 方法4:显示图书列表     *      */    @Override    public void showAllBook() {        System.out.println("*********************图书列表**********************************");        if(bms.getBooksCount()==0) {            System.out.println("图书已售罄,期待下次光临!");        }else {            System.out.println("图书编号\t图书名称\t\t\t价格\t\t作者");        }        for(int i=0;i<BookList.length;i++) {            if(BookList[i]!=null) {                System.out.println(BookList[i].getBook_id()+"\t"+BookList[i].getBook_name()+"\t\t"+BookList[i].getBook_price()+"\t\t"+BookList[i].getBook_author());            }        }        System.out.println("************************************************************");    }    /**     * 方法5:得到一本书     * getOneBook()     */    public Book getOneBook() {        System.out.println("请输入新增图书书名:");        String book_name = new Scanner(System.in).next();        System.out.println("请输入图书价格:");        double book_price = new Scanner(System.in).nextDouble();        System.out.println("请输入图书作者:");        String book_author = new Scanner(System.in).next();        Book  book= new Book(bms.getBooksCount()+1, book_name, book_price, book_author);        return book;    }    /**     * 方法6:新增图书     */    @Override    public void addBook(Book book) {        //打印要新增的图书        System.out.println("*********************即将新增的图书**********************************");        System.out.println("图书编号\t图书名称\t\t\t价格\t\t作者");        System.out.println(book.getBook_id()+"\t"+book.getBook_name()+"\t\t"+book.getBook_price()+"\t\t"+book.getBook_author());        System.out.println("*****************************************************************");        //根据用户选择,是否确认执行新增操作:        if(isContinue("请核实新增图书信息,确认新增(y/n)?", "未执行新增操作,请选择其他操作")) {            for(int i=0;i<BookList.length;i++) {                if(BookList[i]!=null) {                    if(i==bms.getBooksCount()-1) {                        BookList[i+1] = book;                        //小心下标越界 方案1:length-1 这样导致所有null全部添加成了同一本书 方案2:break;最科学。                        System.out.println("恭喜您,新增成功! *.* ");                        break;                    }                }            }        }else {            //notContinueInfo已经处理了        }    }    /**     * 运行结果:     * *********************图书列表**********************************        图书编号    图书名称            价格      作者        1   《笑傲江湖》      66.66       金庸        2   《三国演义》      55.4322     罗贯中        3   《资治通鉴》      23.4567     Author        4   《Java从入门到放弃》        45.678      古灵精怪        5   《悲惨世界》      11.123      雨果        6   《致加西亚的信》        34.43       洗脑者        6   《致加西亚的信》        34.43       洗脑者        6   《致加西亚的信》        34.43       洗脑者        6   《致加西亚的信》        34.43       洗脑者        6   《致加西亚的信》        34.43       洗脑者        6   《致加西亚的信》        34.43       洗脑者        6   《致加西亚的信》        34.43       洗脑者        6   《致加西亚的信》        34.43       洗脑者     */    /**     * 方法6:获取图书数量     * getBooksCount()     */    public int getBooksCount() {        int count =0;        for(int i=0;i<BookList.length;i++) {            if(BookList[i]!=null) {                count++;            }        }        return count;    }    @Test    public void test01() {        System.out.println(getBooksCount());    }    /**     * 方法7:删除图书     */    @Override    public void delBook(String book_name) {        //只有在书存在的前提下,所有的删除才有意义        if(bms.isBookNameExist(book_name)) {            //根据书名,输出这本书            Book book = bms.returnBookByBookName(book_name);            System.out.println("*********************即将删除的图书**********************************");            System.out.println("图书编号\t图书名称\t\t\t价格\t\t作者");            System.out.println(book.getBook_id()+"\t"+book.getBook_name()+"\t\t"+book.getBook_price()+"\t\t"+book.getBook_author());            System.out.println("*****************************************************************");            //用户确认是否删除            if(bms.isContinue("请核对删除图书信息,确认删除(y/n)?","图书未删除,请选择其他操作:")) {                for(int i=0;i<BookList.length;i++) {                    if(BookList[i]!=null) {                        if(book_name.equals(BookList[i].getBook_name())) {                            BookList[i]=null;                            System.out.println("恭喜您!删除成功 *.* ");                            break;                        }                    }                }            }else {                //notContinueInfo已经处理,所以不必处理            }        }else {            System.out.println("此本书不存在,请重新选择操作:");        }    }    /**     * 方法8:得到一个书名     * getOneBookName()     */    public String getOneBookName() {        System.out.println("请输入书名:");        return new Scanner(System.in).next();    }    /**     * 方法9:判断该书名是否存在     * isBookNameExist()     */    public boolean isBookNameExist(String book_name) {        boolean flag = false;        for(int i=0;i<BookList.length;i++) {            if(BookList[i]!=null) {                if(book_name.equals(BookList[i].getBook_name())) {                    flag = true;                    break;                }else {                    flag = false;                }            }        }        return flag;    }    /**     * 方法10:根据书名返回这本书     * returnBookByBookName() ——已经判断这本书存在     */    public Book returnBookByBookName(String book_name) {        for(int i=0;i<BookList.length;i++) {            if(BookList[i]!=null) {                if(book_name.equals(BookList[i].getBook_name())) {                    return BookList[i];                }            }        }        //因为已经确保book_name已经存在,所以这种情况不存在        return null;    }    /**     * 方法11:编辑图书     */    @Override    public void editBook(String book_name) {        //这本书必须存在,才有得编辑        if(bms.isBookNameExist(book_name)) {            System.out.println("请输入修改后的价格:");            double book_price = new Scanner(System.in).nextDouble();            System.out.println("请输入修改后的作者:");            String book_author = new Scanner(System.in).next();            //输出修改后的图书            System.out.println("*********************即将修改的图书**********************************");            System.out.println("图书名称\t\t\t价格\t\t作者");            System.out.println(book_name+"\t\t"+book_price+"\t\t"+book_author);            System.out.println("*****************************************************************");            //确认是否执行修改操作?            if(bms.isContinue("请核对修改后的信息,确认修改(y/n)?", "未执行修改操作,请选择其他操作:")) {                //执行修改操作                for(int i=0;i<BookList.length;i++) {                    if(BookList[i]!=null) {                        if(book_name.equals(BookList[i].getBook_name())) {                            BookList[i].setBook_price(book_price);                            BookList[i].setBook_author(book_author);                            System.out.println("恭喜您!修改成功! *.*");                            //为了这句话只输出一次,有必要break                            break;                        }                    }                }            }else {                //notContinueInfo已经处理了,不用处理            }        }else {            System.out.println("此书不存在,请选择其他操作:");        }    }    /**     * 方法12:购买图书     */    @Override    public void buyBook() {        try {            System.out.println("请输入要购买图书编号【若是多本,编号请以英文逗号间隔】:");            String strId = new Scanner(System.in).next();            //根据逗号拆分            String[] strIds = strId.split(",");            for(int i=0;i<BookList.length;i++ ) {                if(BookList[i]!=null) {                    for(int j=0;j<strIds.length;j++) {                        if(BookList[i].getBook_id()==Integer.parseInt(strIds[j])) {                            buiedBooks[i] = BookList[i];                            totalPrice +=BookList[i].getBook_price();                            BookList[i] = null;                            System.out.println("恭喜您!购买成功!");                            //这个很关键,退出内层循环,否则包空指针                            break;                        }                    }                }            }            //输出购买信息            System.out.println("*********************购买清单**********************************");            System.out.println("图书编号\t图书名称\t\t\t价格\t\t作者\t总金额");            for(int i=0;i<buiedBooks.length;i++) {                if(buiedBooks[i]!=null) {                    System.out.println(buiedBooks[i].getBook_id()+"\t"+buiedBooks[i].getBook_name()+"\t\t"+buiedBooks[i].getBook_price()+"\t\t"+buiedBooks[i].getBook_author());                    }            }            DecimalFormat df = new DecimalFormat("#.00");            System.out.println("\t\t\t\t\t\t\t"+df.format(totalPrice)+"元");            System.out.println("*****************************************************************");        }catch(NumberFormatException e) {            System.out.println("输入有误,请重新选择:");        }    }    /**     * 解决问题:     * 1.Exception in thread "main" java.util.InputMismatchException     * 2.Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100 增加图书时候,如果后一位+1,就会出现     * 下标越界;如果把length-1;就会导致所有null值全部添加同一本书。     * 3.Exception in thread "main" java.lang.NumberFormatException: 购买图书如果输入中文逗号,会报这个错,整体处理下     * 4.如果图书售罄,给一定提示;     */}

4.总结


1.这次主要优化的是思路。比如isContinue()方法抽离出来就特别的好,因为程序在循环的时候会用到,增加确认、修改确认、删除确认都会用到这个方法,非常方便;

2.确保遍历结束,返回一个boolean类型,不要在循环体中返回,在确认返回的时候,要及时中断,否则这个值就在true和false之间摇摆不定;

3.方法能抽离就抽离,各司其职,非常明确。比如我们执行删除、修改的思路都类似:首先要判断这个书名是否存在==》输出对应信息==》根据用户输入判断是否执行操作==》执行操作,用不同方法调用起来就有调兵遣将的感觉,很爽!

4.温故而知新,真的是一句高度精辟的话!


好了,下期再会!

原创粉丝点击