Collection : List&Map的应用实例之极简版购物车的商品信息管理,可能比较粗糙,我也只是跟着视频做的,大家权当看着玩了。应该还有很大的挖掘完善提升的空间,欢迎大家评论。
效果大致可以这样:
首先定义一个Book类:
/**
*
*/
package 购物车作业;
/**
* Aurora 2020年5月11日
* Book: 用于封装商品|书籍的信息
*/
public class Book {
//商品编号 唯一的
private int bookId;
//商品名字
private String name;
//商品价格
private float price;
//作者
private String author;
//出版社
private String chuBanShe;
//库存
private int storeNum;
//购买数量
private int buyNum;
public Book() {
super();
// TODO Auto-generated constructor stub
}
/**
* @param bookId
* @param name
* @param price
* @param author
* @param chuBanShe
* @param storeNum
*/
public Book(int bookId, String name, float price, String author, String chuBanShe, int storeNum) {
super();
this.bookId = bookId;
this.name = name;
this.price = price;
this.author = author;
this.chuBanShe = chuBanShe;
this.storeNum = storeNum;
}
/**
* @return the bookId
*/
public int getBookId() {
return bookId;
}
/**
* @param bookId the bookId to set
*/
public void setBookId(int bookId) {
this.bookId = bookId;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
public float getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(float price) {
this.price = price;
}
/**
* @return the author
*/
public String getAuthor() {
return author;
}
/**
* @param author the author to set
*/
public void setAuthor(String author) {
this.author = author;
}
/**
* @return the chuBanShe
*/
public String getChuBanShe() {
return chuBanShe;
}
/**
* @param chuBanShe the chuBanShe to set
*/
public void setChuBanShe(String chuBanShe) {
this.chuBanShe = chuBanShe;
}
/**
* @return the storeNum
*/
public int getStoreNum() {
return storeNum;
}
/**
* @param storeNum the storeNum to set
*/
public void setStoreNum(int storeNum) {
this.storeNum = storeNum;
}
/**
* @return the buyNum
*/
public int getBuyNum() {
return buyNum;
}
/**
* @param buyNum the buyNum to set
*/
public void setBuyNum(int buyNum) {
this.buyNum = buyNum;
}
}
然后再是主类:
package 购物车作业;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
* Aurora 2020年5月11日
*
* 模拟商城中 购物车功能的实现的
*/
public class ShopCarApp {
// 定义List集合,用于存放所有的商品信息
private static List<Book> bookList = new ArrayList<>();
//定义Map集合,用于存放用户购物车中商品信息,key:书籍编号|id value: 书籍信息(book)
private static Map<Integer,Book> shopCar = new HashMap<>();
static {
// 给‘仓库’填充商品信息,便于进入首页的时候,可以展示商品信息
bookList.add(new Book(1, "疯狂java讲义", 99.0f, "李刚", "疯狂软件", 100));
bookList.add(new Book(2, "疯狂html讲义", 90.0f, "李刚", "疯狂软件", 90));
bookList.add(new Book(3, "疯狂ios讲义", 98.0f, "李刚", "疯狂软件", 80));
bookList.add(new Book(4, "疯狂python讲义", 89.0f, "李刚", "疯狂软件", 90));
}
// 程序入口方法
public static void main(String[] args) {
System.out.println("=========================================欢迎访问AU商城=========================================");
System.out.println("编号\t\t书名\t\t\t作者\t\t价格\t\t出版社\t\t库存");
// 从仓库取出所有的商品信息
for (int i = 0; i < bookList.size(); i++) {
Book book = bookList.get(i);
System.out.println(book.getBookId() + "编号\t\t" + book.getName() + "\t\t" + book.getAuthor() + "\t\t"
+ book.getPrice() + "\t\t" + book.getChuBanShe() + "\t\t" + book.getStoreNum());
}
System.out.println("请输入操作命令: ");
System.out.println(" 添加商品信息至购物车(add)、删除购物车中商品信息(delete)、查询购物车中商品信息(car)、");
System.out.println(" 更新购物车中商品信息(update)、返回首页(index)、提交订单(order)");
System.out.println("请输入:");
Scanner scanner = new Scanner(System.in);
String zhiLing = scanner.next();
// 定义调用方法,根据用户不同指令进行相应的操作
ShopCarApp.userOperas(zhiLing);
}
// 定义方法,根据用户的指令进行相对应的操作
public static void userOperas(String zhiLing) {
switch (zhiLing) {
case "add":
//添加商品至购物车
ShopCarApp.addBookToShopCar();
break;
case "delete":
//删除购物车中的商品
ShopCarApp.deleteBookToShopCar();
break;
case "car":
//查看购物车
ShopCarApp.showShopCar();
break;
case "update":
//更新购物车中的商品
ShopCarApp.updateShopCarBookInfo();
break;
case "index":
//返回首页
ShopCarApp.main(null);
break;
case "order":
//下订单 将购物车清空,给用户提示:
ShopCarApp.orderCommit();
break;
default:
//用户已输入指令不正确,给出提示:让用户从新输入
System.out.println("您输入的指令不正确!");
//返回首页
ShopCarApp.main(null);
break;
}
}
public static void orderCommit() {
// TODO Auto-generated method stub
//判断购物车是否为空,若购物车为空则无需下单
if(ShopCarApp.shopCar.isEmpty()) {
System.out.println("您的购物车为空~!为您跳转至首页 !!");
//返回首页
ShopCarApp.main(null);
}else {
//
System.out.println("您的订单生成完毕,请准备收获!");
//将购物车清空
ShopCarApp.shopCar.clear();
//返回首页
ShopCarApp.main(null);
}
}
//更新购物车中的商品
public static void updateShopCarBookInfo() {
// TODO Auto-generated method stub
System.out.println("请输入需要更新的商品的编号|ID:");
Scanner scanner = new Scanner(System.in);
//获取用户想要添加至购物车的商品ID
int id = scanner.nextInt();
if(ShopCarApp.shopCar.containsKey(id)){
//商品存在,则提示用户输入需要购买的数量
System.out.println("请输入购买数量:");
int buyNum=scanner.nextInt();
//根据id获取商品信息 更新购买数量 以及 库存
Book book=ShopCarApp.shopCar.get(id);
//更新库存 之前购买的+之前的库存-现在购买的==现在的库存
book.setStoreNum(book.getBuyNum()+book.getStoreNum()-buyNum);
//更新购买数量
book.setBuyNum(buyNum);
//跳转至购物车页面
ShopCarApp.showShopCar();
}else {
//商品在购物车中不存在
System.out.println("购物车中不存在该商品");
updateShopCarBookInfo();
}
}
public static void deleteBookToShopCar() {
// TODO Auto-generated method stub
System.out.println("请输入需要删除的商品的编号|ID:");
Scanner scanner = new Scanner(System.in);
//获取用户想要添加至购物车的商品ID
int id = scanner.nextInt();
//判断是否存在于购物车
if(ShopCarApp.shopCar.containsKey(id)) {
//将购物车中的商品数量还原至仓库
Book book=ShopCarApp.shopCar.get(id);
//还原库存
book.setStoreNum(book.getStoreNum()+book.getBuyNum());
//将商品信息从购物车Map中删除
ShopCarApp.shopCar.remove(id);
//继续跳转至查看购物车页面
ShopCarApp.showShopCar();
}else {
System.out.println("您输入的商品id不存在!");
deleteBookToShopCar();
}
}
//添加商品至购物车
public static void addBookToShopCar() {
System.out.println("请输入商品的编号|ID:");
Scanner scanner = new Scanner(System.in);
//获取用户想要添加至购物车的商品ID
int id = scanner.nextInt();
//判断商品是否存在,如果存在则提示用户请输入购买数量,若不存在,给用户提示
Book book=getBookById(id);
if(book==null) {
//说明用户输入的id不正确,给用户提示:
System.out.println("您输入的商品id不存在,请重新输入!");
addBookToShopCar();
}else {
//商品存在,提示用户:请输入购买数量:
System.out.println("请输入商品购买的数量:");
int buyNum=scanner.nextInt();
//该商品是否已存在与购物车中,若存在,我们需更新购买数量,若不存在则直接放入购物车
if(ShopCarApp.shopCar.containsKey(id)) {
//商品存在于购物车,更新购买数量 之前购买的数量加上现在购买的数量
int newBuyNum=book.getBuyNum()+buyNum;
//将购买数量更新至book对象中
book.setBuyNum(newBuyNum);
}else {
//设置购买数量
book.setBuyNum(buyNum);
//商品不存在于购物车
ShopCarApp.shopCar.put(id, book);
}
//更新库存
book.setStoreNum(book.getStoreNum()-buyNum);
//展示购物车中商品信息
showShopCar();
}
}
//展示商品信息
public static void showShopCar() {
// TODO Auto-generated method stub
//获取购物车中商品信息
Collection<Book> books=ShopCarApp.shopCar.values();
if(books.isEmpty()) {
//没有商品给用户提示:
System.out.println("您的购物车中暂未有商品信息,为您跳转到首页:");
ShopCarApp.main(null);
}else {
System.out.println("=========================================您的购物车中有如下商品:=========================================");
System.out.println("编号\t\t书名\t\t\t\t作者\t\t价格\t\t出版社\t\t购买数量");
for (Book book:books) {
System.out.println(book.getBookId()+"\t\t"+book.getName()+"\t\t\t"+book.getAuthor()+"\t\t"+book.getPrice()+"\t\t"+book.getChuBanShe()+"\t\t"+book.getBuyNum());
}
System.out.println("请输入操作命令: ");
System.out.println(" 添加商品信息至购物车(add)、删除购物车中商品信息(delete)、查询购物车中商品信息(car)、");
System.out.println(" 更新购物车中商品信息(update)、返回首页(index)、提交订单(order)");
System.out.println("请输入:");
Scanner scanner = new Scanner(System.in);
String zhiLing = scanner.next();
// 定义调用方法,根据用户不同指令进行相应的操作
ShopCarApp.userOperas(zhiLing);
}
}
//根据商品ID检索仓库中商品的信息,若有则返回商品对象,若没有返回null
public static Book getBookById(int bookId) {
//遍历仓库
for (int i = 0; i < bookList.size(); i++) {
Book book=bookList.get(i);
if (book.getBookId()==bookId) {
return book;
}
}
return null;
}
}