您好,欢迎访问一九零五行业门户网

Java实战分享—图书借阅系统运行效果及存在的问题

自己一直在自学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; } }}
运行效果图存在问题在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实战分享—图书借阅系统运行效果及存在的问题的详细内容。
其它类似信息

推荐信息