實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的圖書(shū)購(gòu)買訂單系統(tǒng)
主要類為:
Admin類:
購(gòu)買方法:通過(guò)對(duì)預(yù)設(shè)的圖書(shū)列表進(jìn)行遍歷。如果輸入的購(gòu)買圖書(shū)編號(hào)與集合中的某個(gè)元素對(duì)象的圖書(shū)編號(hào)相同,且此元素對(duì)象的庫(kù)存大于購(gòu)買數(shù)忙上,購(gòu)買數(shù)大于0:則在order對(duì)象的items屬性列表中钱豁,新建一個(gè)OrderItem類型元素對(duì)象窥妇,將相對(duì)應(yīng)的圖書(shū)名稱恨统、價(jià)格纷纫、購(gòu)買數(shù)量作為其屬性枕扫,然后將book集合相應(yīng)對(duì)象的庫(kù)存減去購(gòu)買數(shù)量。如果輸入的購(gòu)買數(shù)量大于庫(kù)存:則輸出庫(kù)存不足辱魁。然后進(jìn)行循環(huán)調(diào)用自身烟瞧,或調(diào)用訂單方法。
訂單方法:對(duì)訂單對(duì)象中的items集合屬性進(jìn)行判斷染簇,是否為空参滴。不為空則遍歷items集合屬性,輸出items集合中元素對(duì)象的屬性剖笙,匯總購(gòu)買總額卵洗,打印訂單。
訂單Order類:
屬性:訂單號(hào)(orderId)弥咪、訂單總額(total)过蹂、訂單日期(date)、訂單項(xiàng)列表(items)
其中訂單號(hào)(orderId)為隨機(jī)數(shù)與字符串拼接聚至,訂單項(xiàng)列表(items)為ArrayList<OrderItem>數(shù)據(jù)類型
圖書(shū)訂單項(xiàng)OrderItem類:
屬性:圖書(shū)名稱(bookName)酷勺、圖書(shū)單價(jià)(price)、購(gòu)買數(shù)量(num)
圖書(shū)B(niǎo)ook類:
屬性:圖書(shū)編號(hào)(bookId)扳躬、圖書(shū)名稱(bookName)脆诉、圖書(shū)單價(jià)(price)、庫(kù)存數(shù)量(storage)
import java.util.*;
/**
* 主類測(cè)試
* @author 萬(wàn)星明
* @version 1.0
* @time
*/
public class Admin {
//生成全局Scanner對(duì)象sc贷币,方便輸入
static Scanner sc = new Scanner(System.in);
/**
* @Admin主類的主方法
* @param args
*/
public static void main(String[] args) {
//創(chuàng)建一個(gè)訂單對(duì)象order
Order order = new Order();
//創(chuàng)建一個(gè)自定義Book泛型的ArrayList集合book
ArrayList<Book> book = new ArrayList<Book>();
//創(chuàng)建book集合中的元素對(duì)象
book.add(new Book("1","java教程",30.6,30));
book.add(new Book("2","jsp 指南",42.1,40));
book.add(new Book("3","SSH 架構(gòu)",47.3,15));
System.out.println("=========圖書(shū)列表=========");
System.out.println("圖書(shū)列表===圖書(shū)名稱==單價(jià)==庫(kù)存");
//對(duì)集合book進(jìn)行遍歷击胜,輸出其元素對(duì)象的屬性
for (Book book2 : book) {
System.out.println(book2.getBookID()+"--------"+book2.getBookName()+"--"+book2.getPrice()+"--"+book2.getStorage());
}
//調(diào)用buy方法,傳入order對(duì)象以及book集合
buy(order,book);
}
/**
* @buy方法役纹,提供購(gòu)買處理
* @param order
* @param book
*/
public static void buy(Order order,ArrayList<Book> book) {
System.out.println("請(qǐng)輸入購(gòu)買圖書(shū)編號(hào):");
String bookID = sc.next();
System.out.println("請(qǐng)輸入購(gòu)買該圖書(shū)數(shù)量:");
int num = sc.nextInt();
//對(duì)傳入的book集合進(jìn)行遍歷
for(int i=0;i<book.size();i++) {
//如果輸入的圖書(shū)編號(hào)與集合中的某個(gè)元素對(duì)象的圖書(shū)編號(hào)相同
if(bookID.equals(book.get(i).getBookID())) {
//且此元素對(duì)象的庫(kù)存大于購(gòu)買數(shù)偶摔,購(gòu)買數(shù)大于0
if(book.get(i).getStorage()>=num && num>0) {
//則在order對(duì)象的items屬性列表中,新建一個(gè)OrderItem類型元素對(duì)象促脉,將相對(duì)應(yīng)的圖書(shū)名稱辰斋、價(jià)格、購(gòu)買數(shù)量作為其屬性
order.items.add(new OrderItem(book.get(i).getBookName(),book.get(i).getPrice(),num));
//將book集合相應(yīng)對(duì)象的庫(kù)存減去購(gòu)買數(shù)量
book.get(i).setStorage(book.get(i).getStorage()-num);
}else if(num>book.get(i).getStorage()){
//如果輸入的購(gòu)買數(shù)量大于庫(kù)存瘸味,則輸出庫(kù)存不足
System.out.println("庫(kù)存不足");
break;
}else {
//否則無(wú)消費(fèi)
System.out.println("無(wú)消費(fèi)宫仗!");
}
}
}
System.out.println("是否繼續(xù)購(gòu)買!y/n:");
String anwser = sc.next();
//如果判斷是否繼續(xù)購(gòu)買
if(anwser.equalsIgnoreCase("y")) {
//是旁仿,則調(diào)用buy自身
buy(order,book);
}else {
//否藕夫,調(diào)用order訂單方法
order(order,book);
}
}
/**
* @order方法,提供打印訂單處理
* @param order
* @param book
*/
public static void order(Order order,ArrayList<Book> book) {
System.out.println(" 圖書(shū)訂單 ");
System.out.println("圖書(shū)訂單號(hào):"+order.orderID);
System.out.println("圖書(shū)名稱 購(gòu)買數(shù)量 圖書(shū)單價(jià)");
System.out.println("--------------------------");
//對(duì)order對(duì)象中的items集合屬性進(jìn)行判斷,是否為空
if(order.items.isEmpty()) {
//為空則
System.out.println("無(wú)任何消費(fèi)汁胆!");
}else {
//不為空則遍歷items集合屬性
for (OrderItem M : order.items) {
//輸出items集合中元素對(duì)象的屬性
System.out.println(M.getBookName()+" "+M.getNum()+" "+M.getPrice());
//匯總購(gòu)買總額
order.total +=M.getNum()*M.getPrice();
}
}
System.out.println("--------------------------");
System.out.println("訂單總額: "+order.total);
System.out.println("日期:"+order.date);
}
}
---------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
/**
* 訂單類
* @author 萬(wàn)星明
* @version 1.0
* @time
*/
public class Order {
/**
* 訂單號(hào)(orderId)梭姓、訂單總額(total)、訂單日期(date)嫩码、訂單項(xiàng)列表(items)
*/
//創(chuàng)建一個(gè)隨機(jī)數(shù)器
Random r = new Random();
//隨機(jī)一個(gè)四位數(shù)的整數(shù)型值,并將其轉(zhuǎn)為字符串罪既,與10086連接起來(lái)铸题,賦值給訂單號(hào)orderID屬性
public String orderID = "10086"+String.valueOf(r.nextInt(8999)+1000);
double total;
Date date = new Date();
//這是一個(gè)集合列表屬性
ArrayList<OrderItem> items = new ArrayList<OrderItem>();
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public ArrayList<OrderItem> getItems() {
return items;
}
public void setItems(ArrayList<OrderItem> items) {
this.items = items;
}
}
---------------------------------------------------------------------------------------------------
/**
* 圖書(shū)訂單項(xiàng)類
* @author 萬(wàn)星明
* @version 1.0
* @time
*/
public class OrderItem {
/**
* 圖書(shū)名稱(bookName)、圖書(shū)單價(jià)(price)琢感、購(gòu)買數(shù)量(num)
*/
private String bookName;
private double price;
private int num;
public OrderItem() {
}
public OrderItem(String bookName, double price, int num) {
super();
this.bookName = bookName;
this.price = price;
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
---------------------------------------------------------------------------------------------------
/**
* 圖書(shū)類
* @author 萬(wàn)星明
* @version 1.0
* @time
*/
public class Book {
/**
* 圖書(shū)編號(hào)(bookId)丢间、圖書(shū)名稱(bookName)、圖書(shū)單價(jià)(price)驹针、庫(kù)存數(shù)量(storage)
*/
private String bookID;
private String bookName;
private double price;
private int storage;
public Book() {
}
public Book(String bookID, String bookName, double price, int storage) {
super();
this.bookID = bookID;
this.bookName = bookName;
this.price = price;
this.storage = storage;
}
public String getBookID() {
return bookID;
}
public void setBookID(String bookID) {
this.bookID = bookID;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStorage() {
return storage;
}
public void setStorage(int storage) {
this.storage = storage;
}
}
?