基于Java web的图书借阅系统,用java实现图书管理系统
今天这个是一个爪哇小练习,一个图书借阅系统,需要实现的功能有:
判断用户是否需要进行借书在用户选择借书时,展示出图书列表图书列表包含图书序号、图书名称、借阅价格、作者用户选择借书数量、并选择对应图书、借阅天数计算出用户需支付金额Book.java
包com.imooc/***图书类包含图书序号名称价格* */public class Book { private int id;私有字符串名称;私人双倍价格;私有字符串作者;public Book(int id,String name,double price,String author) { //TODO自动生成的构造函数stub this.id=idthis.setName(名称);this.price=价格;this.author=作者;} public void setId(int id){ this。id=id} public int getId(){ return id;}公共void设置价格(双倍价格){ this。价格=价格;} public double getPrice(){ return price;} public void set author(字符串作者){ this。作者=作者;}公共字符串getAuthor() {返回作者;} public void set name(String name){ this。name=名称;} public String getName(){ return name;} }BorrowBooks.java
包com.imooc导入Java。util。ArrayList导入Java。util。列表;导入Java。util。扫描仪;公共类借书{/* * * * @ param args */public static void main(String[]args){//TODO自动生成的方法stub System.out.println(~~~~~欢迎使用图书借阅系统~~~~~~~~ );System.out.println(您是否要借书:1.是点击其他键退出);借书测试=新借书();while(测试。test1()){ system。出去。println(您可选择图书及其价目表:);系统。出去。println(-);Book[] books={新书(0,红楼梦, 12, 曹雪芹),新书(1,西游记, 12, 吴承恩),新书(2,汉乡, 12, 孑与2),新书(3,大魏宫廷, 12, 贱宗首席),新书(4,三国演义, 12, 罗贯中),新书(5,水浒传, 12, 施耐庵) };System.out.println(序号 t 书名 t 租金 t 作者);for(书书:书){ if(书。getclass().等于(书。class)){ system。出去。打印
ln(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; } }}运行效果图
存在问题
在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 + "元"); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。