java课堂小作业简易商城的增删改查

import com.cg.entity.Cart;
import com.cg.entity.Shopping;
import com.cg.service.shoppingService;
import com.cg.serviceImpl.shoppingServiceImpl;

import java.util.*;

public class cg {
public static void main(String[] args) {
shoppingService s = new shoppingServiceImpl();
/主菜单/
while(true) {
Scanner input = new Scanner(System.in);
System.out.println(“欢迎来到起风了商城”);
System.out.println("\t1.添加商品");
System.out.println("\t2.查看商品");
System.out.println("\t3.查看单个商品");
System.out.println("\t4.删除单个商品");
System.out.println("\t5.添加购物车");
System.out.println("\t6.查看购物车");
System.out.println("\t7.退出");
System.out.println("
***********************");
System.out.println(“请选择菜单:”);

int n = input.nextInt();
switch (n) {
case 1:
add();
break;
case 2:
findAll();
break;
case 3:
findById();
break;
case 4:
delectById();
break;
case 5:
addCart();
break;
case 6:
showCart();
break;
case 7:
delectById();
break;
default:
System.out.println(“输入有误”);
}
}
}

删除商品

   private static void delectById() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入商品id");
        int id = input.nextInt();
        shoppingService s = new shoppingServiceImpl();
        s.delectById(id);
    }

增加商品

   private static void add() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入商品id");
        int id = input.nextInt();
        System.out.println("请输入商品名称");
        String name = input.next();
        System.out.println("请输入商品价格");
        double price = input.nextDouble();
        shoppingService s = new shoppingServiceImpl();
        Shopping shopping = new Shopping(id,name,price);
        s.add(shopping);
    }

查找单个商品

 private static void findById() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入商品编号");
        int i = input.nextInt();
        shoppingService s = new shoppingServiceImpl();
        Shopping shopping = s.findById(i);
        if(shopping!=null){
            System.out.println("编号\t书名    \t价格");
            System.out.println(shopping.getId()+"\t"+shopping.getName()+"\t"+shopping.getPrice()+"\t");
        }else{
            System.out.println("不存在改商品");
        }
}

查看所有商品

/*展示所有*/
public static void findAll(){
    shoppingService s = new shoppingServiceImpl();
    List<Shopping> all = s.findAll();
    System.out.println("编号\t书名    \t价格");
    for(Shopping shopping: all){
        System.out.println(shopping.getId()+"\t"+shopping.getName()+"\t"+shopping.getPrice()+"\t");
    }
}

添加购物车

 /*添加购物车*/
    public  static void  addCart(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入商品id");
        int id = input.nextInt();
        System.out.println("请输入商品数量");
        int amount = input.nextInt();
        shoppingService s = new shoppingServiceImpl();
        Shopping byId = s.findById(id);
        Cart cart = new Cart();
        cart.setId(id);
        cart.setAmount(amount);
        cart.setName(byId.getName());
        cart.setPrice(byId.getPrice());
        s.addCart(cart);

    }

查看购物车

 /*显示购物车*/
    public  static void  showCart(){
    shoppingService s = new shoppingServiceImpl();
    Map<Integer, Cart> is = s.showCart();
    Collection<Cart> values = is.values();
    System.out.println("编号\t书名    \t价格\t\t总数\t价钱");
    double t = 0;
    for (Cart cart : values){
        System.out.println(cart.getId()+"\t"+cart.getName()+"\t"+cart.getPrice()+"\t"+cart.getAmount()+"\t" +cart.getAmount()*cart.getPrice());
        t+=cart.getAmount()*cart.getPrice();
    }
        System.out.println("总价钱"+t);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值