首頁 > Java > java教程 > Java實戰分享—圖書借閱系統運作效果及存在的問題

Java實戰分享—圖書借閱系統運作效果及存在的問題

php是最好的语言
發布: 2018-08-04 11:30:17
原創
1864 人瀏覽過

自己一直在自學Java基礎,期望往JavaWeb方向發展。自學了好長時間,每次基本上都不會怎麼記錄,昨天看了GitChat的推送,覺著應當把自己每天的學習記錄下來,這樣可以做到監督的作用。

    今天這個是一個Java小練習,一個圖書借閱系統,需要實現的功能有:

  • 判斷使用者是否需要進行借書

  • 在使用者選擇借書時,顯示出圖書清單

  • #書籍清單包含圖書序號、圖書名稱、借閱價格、作者

  • #使用者選擇借書數量、並選擇對應圖書、借閱天數

  • #計算用戶需支付金額

Book.java

package com.imooc;/**
 * 图书类 包含图书序号 名称 价格
 * */public class Book {
    private int id;    private String name;    private double price;    private String author;    public Book(int id, String name, double price, String author) {        // TODO Auto-generated constructor stub
        this.id = id;        this.setName(name);        this.price = price;        this.author = author;
    }    public void setId(int id) {        this.id = id;
    }    public int getId() {        return id;
    }    public void setPrice(double price) {        this.price = price;
    }    public double getPrice() {        return price;
    }    public void setAuthor(String author) {        this.author = author;
    }    public String getAuthor() {        return author;
    }    public void setName(String name) {        this.name = name;
    }    public String getName() {        return name;
    }

}
登入後複製

BorrowBooks.java

package com.imooc;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class BorrowBooks {    /**
     * @param args
     */
    public static void main(String[] args) {        // TODO Auto-generated method stub
        System.out.println("~~~~~~~欢迎使用图书借阅系统~~~~~~~~ ");
        System.out.println("您是否要借书:1.是 >> 点击其他键退出");
        BorrowBooks test = new BorrowBooks();        while (test.test1()) {
            System.out.println(">>>您可选择图书及其价目表:");
            System.out.println("-------------------------------------------");
            Book[] books = { new Book(0, "红楼梦", 12, "曹雪芹"),                    new Book(1, "西游记", 12, "吴承恩"),                    new Book(2, "汉乡", 12, "孑与2"),                    new Book(3, "大魏宫廷", 12, "贱宗首席"),                    new Book(4, "三国演义", 12, "罗贯中"),                    new Book(5, "水浒传", 12, "施耐庵") };
            System.out.println("序号" + "  " + "\t" + "书名" + "     " + "\t"
                    + "租金" + "      " + "\t" + "作者");            for (Book book : books) {                if (book.getClass().equals(Book.class)) {
                    System.out.println(book.getId() + "\t" + "\t"
                            + book.getName() + "\t" + "\t" + book.getPrice()
                            + "/天" + "\t" + "\t" + book.getAuthor() + "/著");
                }
            }
            System.out.println("-------------------------------------------");
            System.out.println("-->请输入你要借书的数量:");
            Scanner zScanner = new Scanner(System.in);            int BookNum = zScanner.nextInt();            if (BookNum > 0) {
                List<Book> bookList = new ArrayList<Book>();                int add = 0;                int bookPrice = 0;                for (int i = 0; i < BookNum; i++) {
                    System.out.println(">>请输入第" + (i + 1) + "本书的序号:");                    int num = zScanner.nextInt();                    try {
                        bookList.add(books[num]);
                        System.out.println("----成功添加:"
                                + bookList.get(add).getName());                        if (books[num].getClass().equals(Book.class)) {
                            bookPrice += ((Book) bookList.get(add)).getPrice();
                        }
                        add++;
                    } catch (Exception e) {                        // TODO: handle exception
                        System.out.println("您输入的图书序号不正确");
                        i = i - 1;
                        BookNum = BookNum;
                    }

                }
                System.out.println("->请输入借阅的天数:");
                Scanner g = new Scanner(System.in);                int bookDay = g.nextInt();
                bookPrice = bookPrice * bookDay;
                System.out.println("------------借阅选书完成------------" + "\n"
                        + "下面开始统计数据..........");
                System.out.print("您借阅的图书" + BookNum + "本:" + " ");                for (Book book : bookList) {
                    System.out.println(book.getName() + " " + "\n");
                }
                System.out.println();
                System.out.println("共租用:" + bookDay + " 天");
                System.out.println("需要付款:" + bookPrice + " 元");
                System.out.println("->请输入付款金额:");
                System.out.println("------------");
                Scanner x = new Scanner(System.in);                 int priceSpread = bookPrice - x.nextInt();//定义差价
                 while (bookPrice != x.nextInt())

                 System.out.println("------------" + "\n" + "输入错误,请重新输入金额!");                /*
                 while (bookPrice != x.nextInt())
                 {
                 if (bookPrice > x.nextInt()) {
             int priceSpread = bookPrice - x.nextInt();//定义差价
                 System.out.println("------------" + "\n" + "您已付款"
             + x.nextInt() + "元,还需支付" + priceSpread + "元");
                 }

             if (bookPrice <x.nextInt()) {
                 int priceSpread = x.nextInt()-bookPrice ;//定义差价
             System.out.println("------------" + "\n" + "您已付款"
             + x.nextInt() + "元,找您" + priceSpread + "元");
             }
*/
                System.out.println("------------");
                System.out.println("              交易成功!");
                System.out.println();
                System.out.println("------------感谢您的使用--------------");
                System.out.println("………………继续借书请按1,退出请按其他键………………");
            } else {
                System.out.println("您输入的借书数量为“0”,自动为您退出系统");
                System.exit(0);
            }

        }

    }    private static Object bookPrice(int nextInt) {        // TODO Auto-generated method stub
        return null;
    }    // 捕获输入参数不正确异常
    public boolean test1() {        try {
            Scanner z = new Scanner(System.in);            if (z.nextInt() == 1) {                return true;
            } else {                return false;
            }
        } catch (Exception e1) {            return false;
        }
    }
}
登入後複製

運行效果圖

Java實戰分享—圖書借閱系統運作效果及存在的問題

存在問題

#在BorrowBooks.java這個Class中,下面這段程式碼本想實現判斷用戶輸入的金額是否和應付金額一致,不一致時給出不同的回复,但是自己試了好多種方法,都沒有實現,還是自己懂得太少:

     while (bookPrice != x.nextInt())
                 {                 if (bookPrice > x.nextInt()) {                 int priceSpread = bookPrice - x.nextInt();//定义差价
                 System.out.println("------------" + "\n" + "您已付款"
                 + x.nextInt() + "元,还需支付" + priceSpread + "元");
                 }                 if (bookPrice <x.nextInt()) {                 int priceSpread = x.nextInt()-bookPrice ;//定义差价
             System.out.println("------------" + "\n" + "您已付款"
             + x.nextInt() + "元,找您" + priceSpread + "元");
                 }
                 }
登入後複製

相關文章:

用Java異常機制模擬借書系統的步驟詳解

新手圖書館系統,問個有關問題

以上是Java實戰分享—圖書借閱系統運作效果及存在的問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板