Java實現(xiàn)圖書借閱系統(tǒng)
今天這個是一個Java小練習,一個圖書借閱系統(tǒng),需要實現(xiàn)的功能有:
- 判斷用戶是否需要進行借書
- 在用戶選擇借書時,展示出圖書列表
- 圖書列表包含 圖書序號、圖書名稱、借閱價格、作者
- 用戶選擇借書數(shù)量、并選擇對應圖書、借閱天數(shù)
- 計算出用戶需支付金額
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("~~~~~~~歡迎使用圖書借閱系統(tǒng)~~~~~~~~ "); ? ? ? ? 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, "漢鄉(xiāng)", 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("-->請輸入你要借書的數(shù)量:"); ? ? ? ? ? ? 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("->請輸入借閱的天數(shù):"); ? ? ? ? ? ? ? ? Scanner g = new Scanner(System.in); ? ? ? ? ? ? ? ? int bookDay = g.nextInt(); ? ? ? ? ? ? ? ? bookPrice = bookPrice * bookDay; ? ? ? ? ? ? ? ? System.out.println("------------借閱選書完成------------" + "\n" ? ? ? ? ? ? ? ? ? ? ? ? + "下面開始統(tǒng)計數(shù)據(jù).........."); ? ? ? ? ? ? ? ? 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("………………繼續(xù)借書請按1,退出請按其他鍵………………"); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? System.out.println("您輸入的借書數(shù)量為“0”,自動為您退出系統(tǒng)"); ? ? ? ? ? ? ? ? System.exit(0); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? private static Object bookPrice(int nextInt) { ? ? ? ? // TODO Auto-generated method stub ? ? ? ? return null; ? ? } ? ? // 捕獲輸入?yún)?shù)不正確異常 ? ? public boolean test1() { ? ? ? ? try { ? ? ? ? ? ? Scanner z = new Scanner(System.in); ? ? ? ? ? ? if (z.nextInt() == 1) { ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? } catch (Exception e1) { ? ? ? ? ? ? return false; ? ? ? ? } ? ? } }
運行效果圖
存在問題
在BorrowBooks.java這個Class中,下面這段代碼本想實現(xiàn)判斷用戶輸入的金額是否和應付金額一致,不一致時給出不同的回復,但是自己試了好多種方法,都沒有實現(xià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 + "元"); ? ? ? ? } ? ? ? ? }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java中對象?和?json?互轉(zhuǎn)四種方式?json-lib、Gson、FastJson、Jackson
這篇文章主要介紹了Java中對象?和?json?互轉(zhuǎn)?四種方式?json-lib、Gson、FastJson、Jackson,需要的朋友可以參考下2023-11-11Spring中@Service注解的作用與@Controller和@RestController之間區(qū)別
這篇文章主要介紹了Spring中@Service注解的作用與@Controller和@RestController之間的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-03-03java中Servlet監(jiān)聽器的工作原理及示例詳解
這篇文章主要介紹了java中Servlet監(jiān)聽器的工作原理及示例詳解。Servlet監(jiān)聽器用于監(jiān)聽一些重要事件的發(fā)生,監(jiān)聽器對象可以在事情發(fā)生前、發(fā)生后可以做一些必要的處理。感興趣的可以來了解一下2020-07-07老生常談Java?網(wǎng)絡編程?——?Socket?詳解
這篇文章主要介紹了Java?網(wǎng)絡編程?——?Socket?相關知識,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05