


I have been learning the basics of Java by myself and hope to develop in the direction of JavaWeb. I have been studying by myself for a long time, but I basically don’t know how to record it every time. Yesterday I saw the GitChat push, and I felt that I should record my daily learning, so that it can serve as a supervision.
Today this is a small Java exercise, a book lending system. The functions that need to be implemented are:
-
Determine whether the user needs to borrow a book
When the user chooses to borrow a book, the book list is displayed
The book list includes the book serial number, book name, borrowing price, and author
The user selects the number of books to borrow, and selects the corresponding book and the number of borrowing days
Calculate the amount the user needs to pay
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; } } }
Running renderings
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 + "元"); } }Related articles:
Detailed explanation of the steps to simulate the book borrowing system using Java exception mechanism
As a newbie to the library system, I have a question Related questions
The above is the detailed content of Java practical sharing—the operating effects and existing problems of the book lending system. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!
