温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用Java实现图书借阅系统

发布时间:2022-03-11 09:52:41 来源:亿速云 阅读:232 作者:iii 栏目:开发技术

这篇文章主要介绍“怎么用Java实现图书借阅系统”,在日常操作中,相信很多人在怎么用Java实现图书借阅系统问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用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实现图书借阅系统”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI