Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to realize Book Lending system

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use Java to achieve the book lending system". In the daily operation, I believe that many people have doubts about how to use Java to achieve the book lending system. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Java to achieve the book lending system". Next, please follow the editor to study!

The functions that need to be implemented are:

Judge whether users need to borrow books or not

When the user chooses to borrow a book, a list of books is displayed

The book list contains the book serial number, book name, borrowing price, and author.

The user chooses the number of books to borrow, the corresponding books and the number of days to borrow

Calculate the amount that users have to pay.

Book.java

Package com.imooc;/** * Book category includes book serial number name price * * / 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 ("~ Welcome to the book lending system ~"); System.out.println ("do you want to borrow a book: 1. Yes > > Click other keys to exit "); BorrowBooks test = new BorrowBooks (); while (test.test1 ()) {System.out.println (" > you can choose a book and its price list: "); System.out.println ("-- ") Book [] books = {new Book (0, "A Dream of Red Mansions", 12, "Cao Xueqin"), new Book (1, "Journey to the West", 12, "Wu Chengen"), new Book (2, "Han Xiang", 12, "Chao and 2"), new Book (3, "the Court of the Great Wei Dynasty", 12, "cheap Chief") New Book (4, Romance of the three Kingdoms, 12, Luo Guanzhong), new Book (5, Water margin, 12, Shi Nai an)} System.out.println ("serial number" + "+"\ t "+" title "+"+"\ t "+" rent "+" + "\ t" + "author") For (Book book: books) {if (book.getClass (). Equals (Book.class)) {System.out.println (book.getId () + "\ t" + "\ t" + book.getName () + "\ t" + "\ t" + book.getPrice ()) + "/ Day" + "\ t" + "\ t" + book.getAuthor () + "/ author") }} System.out.println ("- -"); System.out.println ("--> Please enter the number of books you want to borrow:"); Scanner zScanner = new Scanner (System.in) Int BookNum = zScanner.nextInt (); if (BookNum > 0) {List bookList = new ArrayList (); int add = 0; int bookPrice = 0; for (int I = 0; I

< BookNum; i++) { System.out.println(">

> Please enter the serial number of the book "+ (I + 1) +": "); int num = zScanner.nextInt (); try {bookList.add (books[ num]) System.out.println ("- successfully added:" + 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 ("the book serial number you entered is incorrect"); I = I-1; BookNum = BookNum }} System.out.println ("- > Please enter the number of days to borrow:"); Scanner g = new Scanner (System.in); int bookDay = g.nextInt (); bookPrice = bookPrice * bookDay System.out.println ("- loan selection completed -" + "\ n" + "start statistics below."); System.out.print ("Books you borrowed" + BookNum + "Ben:" + ") For (Book book: bookList) {System.out.println (book.getName () + "+"\ n ");} System.out.println (); System.out.println (" Co-lease: "+ bookDay +" days ") System.out.println ("need to pay:" + bookPrice + "yuan"); System.out.println ("- > Please enter payment amount:"); System.out.println ("-"); Scanner x = new Scanner (System.in); int priceSpread = bookPrice-x.nextInt () / / define the deviation while (bookPrice! = x.nextInt ()) System.out.println ("-" + "\ n" + "input error, please re-enter the amount!") / * while (bookPrice! = x.nextInt ()) {if (bookPrice > x.nextInt ()) {int priceSpread = bookPrice-x.nextInt () / / define the difference System.out.println ("-" + "\ n" + "you have paid" + x.nextInt () + "yuan, and also pay" + priceSpread + "yuan");} if (bookPrice x.nextInt ()) {int priceSpread = bookPrice-x.nextInt () / / define System.out.println for deviation ("-" + "\ n" + "you have paid" + x.nextInt () + "yuan, plus" + priceSpread + "yuan");} if (bookPrice)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report