Java 杂谈

=====图书管理系统=====

2019-03-18  本文已影响2人  佐半边的翅膀

1.    显示所有的图书

2.    添加图书

3.    删除图书

4.    编辑图书

5.    购买

----------

请输入功能编号(1~5):

Book类

属性->数组BookList

Boolist[0] = “书名,价格,作者”;

构造方法

在构造方法中,初始化数组BookList,在没有添加图书时,至少内部有三本书。

第一个方法:showAllBook 显示所有数组中的图书

第二个方法:addBook 接收三个参数,分别是书名、价格、作者,存如数组

第三个方法:delBook删除图书,接收一个参数:书名,根据书名删除数组中的元素

第四个方法:editBook编辑图书,接收三个参数,分别是书名、价格、作者,根据书名修改价格和作者

第五个方法:buyBook购买图书,把所有图书显示出来,在左侧显示出图书的编号,

1 书名,价格,作者

2 书名,价格,作者

3 书名,价格,作者

4 书名,价格,作者

用户可以输入编号:1,2,3,4,表示想买哪种书,用户可以输入购买数量,

在控制台输出用户的购物清单,同时计算总价格。

----------


    public class Book {

    private String bookName;//书名 

    private double price; //价格

    private String author; //作者  

    public Book() {

    }

    public Book(String bookName, double price, String author) {

        this.bookName = bookName;

        this.price = price;

        this.author = author;

    }

    public String getBookName() {

        return bookName;

    }

    public void setBookName(String bookName) {

        this.bookName = bookName;

    }

    public double getPrice() {

        return price;

    }

    public void setPrice(double price) {

        this.price = price;

    }

    public String getAuthor() {

        return author;

    }

    public void setAuthor(String author) {

        this.author = author;

    }

图书管理类

    public class BookManager {

    // 存储图书

    Book[] bookArr = new Book[200];

    // 声明成属性,所有方法都共享

    Scanner input = new Scanner(System.in);

    /**

     * 初始化数据

     */

    public BookManager() {

        bookArr[0] = new Book("春", 10.0, "赵");

        bookArr[1] = new Book("夏", 20.0, "钱");

        bookArr[2] = new Book("秋", 30.0, "孙");

        bookArr[3] = new Book("冬", 40.0, "李");

    }

//    显示所有图书

    public void showAll() {

        System.out.println("编号\t书名\t价格\t作者");

        for (int i = 0; i < bookArr.length; i++) {

            if (bookArr[i] != null) {

                System.out.println((i + 1) + "\t" + bookArr[i].getBookName() + "\t" + bookArr[i].getPrice() + "\t"

                        + bookArr[i].getAuthor());

            }

        }

    }

    public int getIndex(String bookName) {

        int index = -1;

        for (int i = 0; i < bookArr.length; i++) {

            if (bookArr[i] != null && bookName.equals(bookArr[i].getBookName())) {

                index = i;

                break;

            }

        }

        return index;

    }

//增加书籍信息

    public void addBook() {

        System.out.print("请输入要新增的图书名:");

        String bookName = input.next();

        System.out.print("请输入新增图书的价格:");

        double price = input.nextDouble();

        System.out.print("请输入新增图书的作者:");

        String author = input.next();

        // 重名验证

        if (getIndex(bookName) != -1) {

            System.out.println("书名为《" + bookName + "》的书已存在!!!");

        }

        // 存储,往数组中空的地方存储

        for (int i = 0; i < bookArr.length; i++) {

            if (bookArr[i] == null) {

                bookArr[i] = new Book(bookName, price, author);

                break;

            }

        }

    }

//    删除书籍信息

    public void delBook() {

        System.out.print("请输入要删除的图书名:");

        String bookName = input.next();

        int index = getIndex(bookName);

        if (index == -1) {

            System.out.println("要删除的用户" + bookName + "不存在,无法删除");

        } else {

            int j = index;

            while ((j + 1 < bookArr.length) && (bookArr[j + 1] != null)) {

                bookArr[j] = bookArr[j + 1];

                j++;

            }

            // 最后一位不为空的元素要置空

            bookArr[j] = null;

        }

    }

    // 编辑书籍信息

    public void updateBook() {

        System.out.println("请输入要修改图书的书名:");

        String bookName = new Scanner(System.in).next();

        System.out.println("请输入该图书价格:");

        double price = new Scanner(System.in).nextDouble();

        System.out.println("请输入该图书作者:");

        String author = new Scanner(System.in).next();

        for (int i = 0; i < bookArr.length; i++) {

            if (bookArr[i] != null) {

                if (bookName.equals(bookArr[i].getBookName())) {

                    bookArr[i].setPrice(price);

                    bookArr[i].setAuthor(author);

                    System.out.println("恭喜您!修改成功!");

                    break;

                }

            }

        }

        System.out.println("你编辑的书籍不存在!!!");

    }

//购买图书

    public void buyBook() {

        double sum = 0;

        int i;

        showAll();

        int bs = 1;

        for (i = 0; i < bookArr.length; i++) {

            if (bookArr[i] != null) {

                bs++;

            }

        }

        System.out.println("请输入您要购买书籍的编号:");

        if (input.hasNext()) {

            int bh = input.nextInt();

            if (bh < bs) {

                System.out.println("请输入购买数量:");

                int number = input.nextInt();

                for (i = 1; i < bookArr.length; i++) {

                    if (bh == i + 1) {

                        double p = bookArr[i].getPrice();

                        sum = p * number;

                        break;

                    }

                }

                System.out.println("您购买图书清单信息如下:");

                System.out.println("***********************************************");

                System.out.println("书名\t作者\t价格\t金额合计");

                System.out.println(bookArr[i].getBookName() + "\t" + bookArr[i].getAuthor() + "\t"

                        + bookArr[i].getPrice() + "\t" + sum);

            } else {

                System.out.println("请输入存在的书籍编号!!");

            }

        } else {

            System.out.println("请输入正确的数字编号!");

        }

    }

    public void showMenu() {

        boolean isGo = true;

        do {

            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("===============================================");

            System.out.println("请输入要选择的序号:");

            Scanner input = new Scanner(System.in);

            if (input.hasNextInt()) {

                // 接收输入的数字

                int a = input.nextInt();

                switch (a) {

                case 1:

                    showAll();

                    break;

                case 2:

                    addBook();

                    break;

                case 3:

                    delBook();

                    break;

                case 4:

                    updateBook();

                    break;

                case 5:

                    buyBook();

                    break;

                case 6:

                    isGo = false;

                    System.out.println("谢谢使用图书管理系统!!!");

                    break;

                }

            }

        } while (isGo);

    }

    }

测试类

    public class BookTest {

    public static void main(String[] args) {

          BookManager bk=new BookManager();

    //      bk.showAll();

      bk.showMenu();;

        }

    }

上一篇下一篇

猜你喜欢

热点阅读