执行shop类的main方法,按照要求,补充GoodServiceImpl类中的两个方法,完成排序和下单功能
//商品
public class Goods {
private int id;//商品编号
private String name; //名称
private double price; //单价
private int stock; //库存
public Goods(int id, String name, double price, int stock) {
this.id = id;
this.name = name;
this.price = price;
this.stock = stock;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", stock=" + stock +
'}';
}
}
import java.util.List;
//商品接口
public interface GoodService {
//按照单价升序,返回商品列表
List<Goods> findAll();
//下单购买,返回账户余额 (id-商品编号,num-数量)
double buy(int id,int num) throws Exception;
}
import java.util.List;
import java.util.Scanner;
//模拟前端商城
public class Shop {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws Exception {
start();
}
public static void start() throws Exception {
//1、多态方式,创建商品服务对象
GoodService service = new GoodServiceImpl();
while (true) {
System.out.println("欢迎光临,请选择你的操作");
System.out.println("1-查看商品");
System.out.println("2-购买商品");
int option = scanner.nextInt();
switch (option) {
case 1:
//2、调用商品服务对象,查询所有商品,并循环展示
List<Goods> list = service.findAll();
for (Goods goods : list) {
System.out.println(goods.toString());
}
break;
case 2:
buyGoods();
break;
default:
System.out.println("选择错误");
}
}
}
public static void buyGoods() throws Exception {
//1多态方式,创建商品服务对象
GoodService service = new GoodServiceImpl();
System.out.println("购买商品并下单,请输入您要购买的商品编号:");
int id = scanner.nextInt();
System.out.println("请输入购买商品的数量:");
int num = scanner.nextInt();
//调用商品服务对象,购买商品,使用自定义异常,控制库存数量和用户金额
double money = service.buy(id, num);
System.out.println("购买之后,账户余额:" + money);
}
}
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
//下单的实现类
public class GoodServiceImpl implements GoodService{
//商品列表
private List<Goods> list = new ArrayList<>();
//账户余额
private double account = 10000;
public GoodServiceImpl(){
list.add(new Goods(1,"小米手机",1999.0,10));
list.add(new Goods(2,"Ipad",3998.0,10));
list.add(new Goods(3,"PS5",2000.0,10));
list.add(new Goods(4,"笔记本电脑",7999.0,10));
list.add(new Goods(5,"保温杯",100.0,10));
}
//按照单价升序,返回商品列表
@Override
public List<Goods> findAll() {
list.sort((Goods o1, Goods o2) -> o1.getPrice() - o2.getPrice() == 0 ? 0 : o1.getPrice() - o2.getPrice() > 0 ? 1 : -1);
return list;
}
//下单购买,返回账户余额
@Override
public double buy(int id, int num) throws Exception {
if (id < 1 || id >5) {
throw new Exception("你输出的id不存在");
}
if (list.get(id - 1).getStock() < num) {
throw new Exception("库存不足");
}
BigDecimal decimal = BigDecimal.valueOf(list.get(id - 1).getPrice()).multiply(new BigDecimal(num));
if (BigDecimal.valueOf(account).subtract(decimal).doubleValue() < 0) {
throw new Exception("余额不足");
}
return BigDecimal.valueOf(account).subtract(decimal).doubleValue();
}
}