书类
package com.wang.pojo;
import java.io.Serializable;
import java.util.Random;
public class Book implements Serializable {
private String isbn;//出版号
private String title;
private String author;
private Double price;
private BookType type;
public Book() {
this.isbn= this.createIsbn();
}
private String createIsbn(){
return new Random().nextInt(10000)+1+"";
}
public Book(String title, String author, Double price, BookType type) {
this();
this.title = title;
this.author = author;
this.price = price;
this.type = type;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public BookType getType() {
return type;
}
public void setType(BookType type) {
this.type = type;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public int compareTo(Book o) {
//实现默认排序
if(this.price > o.price) return 1;
else if(this.price< o.price) return -1;
else return 0;
}
@Override
public String toString() {
return "Book{" +
"isbn='" + isbn + '\'' +
", title='" + title + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type=" + type +
'}';
}
}
书本类型
package com.wang.pojo;
import java.io.Serializable;
public enum BookType implements Serializable {
文学, 哲学, 自然科学, 社会科学
}
书本编号
package com.wang.pojo;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Orders {
private String orderId;//订单号:时间戳
private Book book;//图书
private Integer amount;//购买的数量
private String createId(){
SimpleDateFormat sdf=new SimpleDateFormat("HHmmss");
return sdf.format(new Date());
}
public static void main(String[] args) {
System.out.println(new Orders().createId());
}
public Orders() {
//得到订单号
this.orderId = this.createId();
}
public Orders(String orderId, Book book, Integer amount, Date createTime) {
this();
this.orderId = orderId;
this.book = book;
this.amount = amount;
this.createTime = createTime;
}
private Date createTime;//订单时间
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
接口和继承接口类
package com.wang.service;
import com.wang.pojo.Book;
import java.util.ArrayList;
import java.util.List;
public interface IBookService {
List<Book> BOOK_LIST =new ArrayList<>(10);
//查询所有图书
void findAll();
Book findByIsbn(String isbn);
}
package com.wang.service;
import com.wang.pojo.Book;
import com.wang.pojo.BookType;
import java.util.Collections;
import java.util.Iterator;
import static com.wang.service.IBookService.*;
public class BookService implements IBookService{
public BookService(){
//创建图书
Book b1=new Book("红楼梦","曹雪芹",245.00, BookType.文学);
Book b2=new Book("三国演义","罗贯中",300.00, BookType.文学);
Book b3=new Book("水浒传","施耐庵",108.00, BookType.文学);
Book b4=new Book("西游记","吴承恩",81.00, BookType.文学);
//添加到集合
BOOK_LIST.add(b1);
BOOK_LIST.add(b2);
BOOK_LIST.add(b3);
BOOK_LIST.add(b4);
}
@Override
public void findAll() {
if(BOOK_LIST!=null && BOOK_LIST.size()>0){
for(Book b : BOOK_LIST){
System.out.println(b);
}
}else{
System.out.println("对不起,没有图书可以显示!");
}
}
@Override
public Book findByIsbn(String isbn) {
Book book=null;
for(Iterator<Book> iterator = BOOK_LIST.iterator(); iterator.hasNext();){
Book book1 = iterator.next();
if(book1.getIsbn().equalsIgnoreCase(isbn)){
book= book1;
break;
}
}
return book;
}
}
package com.wang.service;
import com.wang.pojo.Orders;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public interface IOrderServie {
public static final Map<String, Orders> ORDERS=new HashMap<>(100);
void buy(Orders orders);
Double showShoppingCart();
//序列化购物车
void saveShoopingCart() throws IOException;
//反序列化购物车
void loadShoopingCart() throws Exception;
void print() throws FileNotFoundException;
void clear();
}
package com.wang.service;
import com.wang.pojo.Book;
import com.wang.pojo.Orders;
import com.wang.util.DateUtil;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
public class OrderService implements IOrderServie{
private final File file=new File("cart.ser");
@Override
public void buy(Orders orders) {
//遍历订单集合
Orders temp=null;
for(Orders o :ORDERS.values()){
//根据参数的订单获取该订单购买的图书
Book book = orders.getBook();
if(o.getBook().getIsbn().equalsIgnoreCase(book.getIsbn())){
temp = o;//找到购买的图书的订单
break;
}
}
if(temp!=null) { //1:该图书已经存在在购物车中
System.out.println("旧的购买,修改数量");
temp.setAmount(temp.getAmount()+1);
}else { //2:新的购买
//将新订单放到购物车
System.out.println("新的购买");
orders.setAmount(1);
orders.setCreateTime(new Date());//购买时间
ORDERS.put(orders.getOrderId(),orders);
}
}
@Override
public Double showShoppingCart() {
Double sum =0.0;
for(Orders orders: ORDERS.values()){
System.out.println("--------订单号:"+orders.getOrderId()+"----------");
//获取每个订单所购买的图书
Book book = orders.getBook();
System.out.println("图书名称:"+book.getTitle()+"作者:"+book.getAuthor()+
"价格:"+book.getPrice()+"购买的数量:"+ orders.getAmount());
System.out.println("订单时间:" + DateUtil.dateToString(orders.getCreateTime()));
Double total=book.getPrice()* orders.getAmount();
System.out.println("小计:"+total );
sum+= total;
}
System.out.println("您共消费¥"+sum +"元");
return sum;
}
@Override
public void saveShoopingCart() throws IOException {
//首先判断购物车是否为空
if(!IOrderServie.ORDERS.isEmpty()){
ObjectOutputStream out=
new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(ORDERS);
out.close();
}else{
System.out.println("对不起,您没有购物记录,保存失败!");
}
}
@Override
public void loadShoopingCart() throws Exception {
//判断序列化文件是否存在
if(file.exists()){
ObjectInputStream in =new ObjectInputStream(new FileInputStream(file));
// IOrderService.ORDERS = (Map<String, Orders>) in.readObject();
Map<String,Orders> map=(Map<String, Orders>) in.readObject();
//添加到当前的购物车
IOrderServie.ORDERS.putAll(map);
in.close();
}else{
System.out.println("文件未找到,不能进行读取操作!");
}
}
@Override
public void print() throws FileNotFoundException {
SimpleDateFormat sdf=new SimpleDateFormat("HHmmss");
PrintWriter pw=new PrintWriter(sdf.format(new Date())+".txt");
Double sum =0.0;
for(Orders orders: ORDERS.values()){
pw.print("--------订单号:"+orders.getOrderId()+"----------");
//获取每个订单所购买的图书
Book book = orders.getBook();
pw.print("图书名称:"+book.getTitle()+"作者:"+book.getAuthor()+
"价格:"+book.getPrice()+"购买的数量:"+ orders.getAmount());
pw.print("订单时间:" + DateUtil.dateToString(orders.getCreateTime()));
Double total=book.getPrice()* orders.getAmount();
pw.print("小计:"+total );
sum+= total;
}
pw.print("您共消费¥"+sum +"元");
pw.close();
}
@Override
public void clear() {
//清空集合
IOrderServie.ORDERS.clear();
//删除文件
if(file.exists()){
file.delete();
System.out.println("数据删除完毕!");
}
}
}
工具类
package com.wang.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
private static final SimpleDateFormat SDF =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public static Date stringToDate(String str){
//将String转换为Date
try {
return SDF.parse(str);
} catch (ParseException e) {
System.out.println(e.getMessage());
}
return null;
}
public static String dateToString(Date date){
//Date转换String
return SDF.format(date);
}
}
视图类
package com.wang.view;
import com.wang.pojo.Book;
import com.wang.pojo.Orders;
import com.wang.service.BookService;
import com.wang.service.IBookService;
import com.wang.service.IOrderServie;
import com.wang.service.OrderService;
import java.util.Scanner;
public class ShoppingView {
public static void main(String[] args) throws Exception {
Scanner in =new Scanner(System.in);
IBookService bookService =new BookService();
IOrderServie orderService =new OrderService();
int m=0;
do{
System.out.println("1浏览图书 2购买图书 3查看购物车 4 结账 5 读取历史购物记录 6 退出");
m= in.nextInt();
switch (m){
case 5:
orderService.loadShoopingCart();;
break;
case 6:
System.out.println("确认退出吗?");
String ans = in.next();
if(ans.equalsIgnoreCase("y")){
//判断是否存在购物车
System.out.println("是否要保存购物车");
ans=in.next();
if(ans.equalsIgnoreCase("y")){
//序列化购物车集合
orderService.saveShoopingCart();
}
System.exit(0);
}
case 4://结账
//计算出总价格,输入金额,打印小票
Double sum = orderService.showShoppingCart();
System.out.println("您共计消费¥"+sum+"元");
System.out.println("请输入金额:");
Double money=in.nextDouble();
if(money >= sum){
System.out.println("交易成功,找零¥"+(money-sum)+"元,您的商品正在发货.....");
//打印发票
orderService.print();
//清空购物车
orderService.clear();
}else{
System.out.println("对不起,金额不足,交易失败!");
}
break;
case 3://浏览购物车
orderService.showShoppingCart();
break;
case 1:
bookService.findAll();
break;
case 2:
System.out.println("请输入要购买的图书isbn");
String isbn= in.next();
Book book = bookService.findByIsbn(isbn);
if(book!=null){
System.out.println(book);
//创建订单对象
Orders orders =new Orders();
//把购买的图书添加到订单中
orders.setBook(book);
//把订单保存到购物车中
orderService.buy(orders);
}else{
System.out.println("对不起,您输入的图书不存在!");
}
break;
}
}while(m<=6);
}
}