<>【 case 3-1】 A Book Purchasing System Based on console

<>【 Case introduction 】

With the vigorous development of the Internet , Network book purchasing system as a form of e-commerce , With its high efficiency , Low cost advantage has gradually become a new business model , People are no longer satisfied with the use of the Internet, only limited to the browsing and publishing of information , More eager to fully enjoy the more convenience brought by the Internet . The online book purchasing system is adapting to the fast-paced life of today's society , So that customers can easily, quickly and easily purchase their favorite books without leaving home .
Requirements of this task , Using the learned knowledge to write a console based book purchasing system , Realize book purchasing function .

* Output information of all books : Include the number of each book , title , Unit Price , stock .
* When customers buy books , Follow the prompts to enter the book number to purchase the required book , And input the number of books to be purchased according to the prompt .
* Output customer's order information after purchase , include : order number , order details , Total order .
<>【 analysis 】

(1) According to the task description , The system must include 3 Entity classes , Class name and attribute settings are as follows :

* Books (Book):
a) Book No (id)
b) Book name (name)
c) Book unit price (price)
d) Inventory quantity (storage)
* Order item class (OrderItem):
a) books (book)
b) Purchase quantity (num)
* Order type (Order):
a) order number (orderID)
b) Total order (total)
c) Order item list (items)
(2) Because when buying books , Number of books to select , Therefore, you need to define the method to obtain the book object and book quantity in the order item class .
(3) Because it is necessary to specify the order items and obtain the order list of the order , order number , Order amount and other information , So you need an order list , order number , Order total specify order items and other methods .

<>【 code 】

Book.java
package com.j2se.myInstances.example3_1; public class Book { private int bno;
private String bid; private String name; private double price; private int
stocks; public Book(int bno, String bid, String name, double price, int stocks)
{ this.bno = bno; this.bid = bid; this.name = name; this.price = price; this.
stocks= stocks; } public int getBno() { return bno; } public String getBid() {
return bid; } public String getName() { return name; } public double getPrice()
{ return price; } public int getStocks() { return stocks; } }
OrderItem.java
package com.j2se.myInstances.example3_1; public class OrderItem { private Book
book; private int nums; public OrderItem(Book book, int nums) { this.book = book
; this.nums = nums; } public Book getBook() { return book; } public int getNums(
) { return nums; } public void setBook(Book book) { this.book = book; } public
void setNums(int nums) { this.nums = nums; } }
Order.java
package com.j2se.myInstances.example3_1; public class Order { private String
oid; private OrderItem[] items; private double total; public Order(String oid) {
this.oid = oid; this.items = new OrderItem[3]; } public String getOid() { return
oid; } public void setOid(String oid) { this.oid = oid; } public OrderItem[]
getItems() { return items; } public void setItems(OrderItem item, int idx) {
this.items[idx] = item; } public double getTotal() { calcTotal(); return total;
} private void calcTotal() { double total = 0; for (int i = 0; i < items.length;
++i) { total += items[i].getNums() * items[i].getBook().getPrice(); } this.total
= total; } public void setTotal(double total) { this.total = total; } }
BookDemo.java
package com.j2se.myInstances.example3_1; import java.time.LocalTime; import
java.util.Scanner; public class BookDemo { public static void main(String[] args
) { Book[] books = new Book[3]; // Store books outBookInfo(books); // Purchase books Order order
= purchase(books); // Print order outOrderInfo(order); } private static void
outOrderInfo(Order order) { System.out.println(
"-------------------\t Book order \t--------------------"); System.out.println(" Order No :" +
order.getOid()); System.out.println("\t Book name \t\t Purchase quantity \t Book unit price "); OrderItem[] items
= order.getItems(); String line = null; for (int i = 0; i < items.length; ++i) {
line= items[i].getBook().getName() + "\t" + items[i].getNums() + "\t" + items[i
].getBook().getPrice(); System.out.println(line); } System.out.println(" Total order :¥"
+ order.getTotal()); System.out.println(
"------------------------------------------------"); } private static Order
purchase(Book[] books) { Scanner sc = new Scanner(System.in); OrderItem item =
null; Order order = new Order("book-order-" + LocalTime.now().toString()); for (
int i = 0; i < books.length; i++) { System.out.print(" Please select the book number to purchase :"); int bno =
sc.nextInt(); System.out.print(" Purchase quantity :"); int num = sc.nextInt(); item = new
OrderItem(books[bno - 1], num); order.setItems(item, i); System.out.println(
" Please continue to purchase books ."); } sc.close(); return order; } private static void outBookInfo(Book[
] books) { books[0] = new Book(1, "b978-7-115-548", "Java Basic case programming ", 19.9, 100);
books[1] = new Book(2, "b328-3-124-431", "Python Data analysis ", 29.9, 56); books[2] = new
Book(3, "b112-8-765-904", " Machine learning practice ( upper )", 39.9, 220); System.out.println(
"-------------------\t List of books \t--------------------"); System.out.println(
" number \t\tISBN\t\t\t name \t\t\t Price \t\t stock "); String line = null; for (int i = 0; i <
books.length; i++) { line = books[i].getBno() + "\t" + books[i].getBid() + "\t"
+ books[i].getName() + "\t" + books[i].getPrice() + "\t" + books[i].getStocks();
System.out.println(line); } System.out.println(
"------------------------------------------------"); } }
<>【 Running results 】

Technology