Map集合实现的纯java购物车

来源:互联网 发布:长春办公软件培训 编辑:程序博客网 时间:2024/06/06 17:11

Product.java

public class Product {
    private Integer productid;
    private String productname;
    private String category;
    private float price;
    private String picture;
    public Product(Integer productid, String productname, String category, float price, String picture) {
        this.productid = productid;
        this.productname = productname;
        this.category = category;
        this.price = price;
        this.picture = picture;
    }
    public Integer getProductid() {
        return productid;
    }
    public void setProductid(Integer productid) {
        this.productid = productid;
    }
    public String getProductname() {
        return productname;
    }
    public void setProductname(String productname) {
        this.productname = productname;
    }
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public String getPicture() {
        return picture;
    }
    public void setPicture(String picture) {
        this.picture = picture;
    }
}


ProductIem.java

public class ProductItem {
    private Product product;
    private Integer count;
    public ProductItem(Product product, Integer count) {
        this.product = product;
        this.count = count;
    }
    public Product getProduct() {
        return product;
    }
    public void setProduct(Product product) {
        this.product = product;
    }
    public Integer getCount() {
        return count;
    }
    public void setCount(Integer count) {
        this.count = count;
    }
    public float getTotalPrice() {
        return this.product.getPrice()*count;
    }
}


ShoppingCart.java

import java.util.HashMap;
import java.util.Scanner;


import javax.swing.plaf.synth.SynthSeparatorUI;


public class ShoppingCart {
    private HashMap<Integer, ProductItem> items = new HashMap<Integer, ProductItem>();
    public static void main(String[] args) {
        ShoppingCart cart = new ShoppingCart();
        System.out.println("初始化产品");
        Product banana = new Product(10000, "香蕉", "水果", 2.50F, "banana.jpg");
        Product apple = new Product(10001, "苹果", "水果", 3.99F, "apple.jpg");
        Product orange = new Product(10002, "桔子", "水果", 1.80F, "orange.jpg");
        Product pen = new Product(10003, "钢笔", "文具", 12.00F, "pen.jpg");
        Product pencil = new Product(10004, "铅笔", "文具", 1.00F, "pencil.jpg");
        cart.addToCart(apple);
        cart.addToCart(apple);
        while(true){
        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.print("请选择[1-6]:");
    System.out.println("请输入你的操作");
        Scanner sc=new Scanner(System.in);
        String x=sc.nextLine();
        switch (x) {
case "1":

System.out.println(cart.showAll());
break;
            case "2":
            System.out.println("添加的水果,请输入");
            x=sc.nextLine();
            //System.out.println(x);
            if(x.equals(banana.getProductname())){
            cart.addToCart(banana);
            }
            if(x.equals(pencil.getProductname())){
            cart.addToCart(pencil);
            }
            if(x.equals(pen.getProductname())){
            cart.addToCart(pen);
            }
            if(x.equals(orange.getProductname())){
            cart.addToCart(orange);
            }
            if(x.equals(apple.getProductname())){
            cart.addToCart(apple);
            //System.out.println(apple.getProductname());
            }
            //System.out.println(cart.showAll());
           
//cart.addToCart(product);
break;
            case "3":
            System.out.println("请输入要删除的东西");
            x=sc.nextLine();
            if(x.equals(banana.getProductname())){
            cart.delFromCart(banana);
            }
            if(x.equals(pencil.getProductname())){
            cart.delFromCart(pencil);
            }
            if(x.equals(pen.getProductname())){
            cart.delFromCart(pen);
            }
            if(x.equals(orange.getProductname())){
            cart.delFromCart(orange);
            }
            if(x.equals(apple.getProductname())){
            cart.delFromCart(apple);
            }
//cart.delFromCart()
break;
            case "4":
            System.out.println("请输入要修改的东西名和数量");
            x=sc.nextLine();
            System.out.println("请输入要修改的东西名的数量");
            int s=sc.nextInt();
            if(x.equals(banana.getProductname())){
            cart.modifyCart(banana,s);
            }
            if(x.equals(pencil.getProductname())){
            cart.modifyCart(pencil,s);
            }
            if(x.equals(pen.getProductname())){
            cart.modifyCart(pen,s);
            }
            if(x.equals(orange.getProductname())){
            cart.modifyCart(orange,s);
            }
            if(x.equals(apple.getProductname())){
            cart.modifyCart(apple,s);
            //System.out.println(apple.getProductname());
            }
            //modifyCart(x,s);
break;
            case "5":
            System.out.println(cart.getTotalPrice());
break;
            case "6":
System.exit(0);;
break;
default:
break;
}
      /*  System.out.println("###############################################");
        System.out.println("添加苹果");
        cart.addToCart(apple);
        System.out.println(cart.showAll());
        System.out.println("当前购物车总价:" + cart.getTotalPrice());
        System.out.println("***********************************************");
        System.out.println("添加香蕉");
        cart.addToCart(banana);
        System.out.println(cart.showAll());
        System.out.println("当前购物车总价:" + cart.getTotalPrice());
        System.out.println("***********************************************");
        System.out.println("修改香蕉数量");
        cart.modifyCart(banana, 5);
        System.out.println(cart.showAll());
        System.out.println("当前购物车总价:" + cart.getTotalPrice());
        System.out.println("***********************************************");
        System.out.println("删除苹果");
        cart.delFromCart(apple);
        System.out.println(cart.showAll());
        System.out.println("当前购物车总价:" + cart.getTotalPrice());
        System.out.println("***********************************************");
        System.out.println("添加铅笔");
        cart.addToCart(pencil);
        System.out.println(cart.showAll());
        System.out.println("当前购物车总价:" + cart.getTotalPrice());
        System.out.println("***********************************************");*/
        }
    }
    public void addToCart(Product product) {
        this.items.put(product.getProductid(), new ProductItem(product, 1));
    }
    public void delFromCart(Product product) {
        this.items.remove(product.getProductid());
    }
    public  String showAll() {
        String detail = "";
        for (Integer key : this.items.keySet()) {
            detail += this.items.get(key).getProduct().getProductname() + ":" + this.items.get(key).getCount() + "件,单价 " + this.items.get(key).getProduct().getPrice() + ",小计" + this.items.get(key).getTotalPrice() + "\n";
        }
        return detail;
    }
    public boolean modifyCart(Product product, Integer count) {
        if (this.items.containsKey(product.getProductid())) {
            this.items.get(product.getProductid()).setCount(count);
            return true;
        }
        return false;
    }
    public float getTotalPrice() {
        float total = 0;
        for (Integer key : this.items.keySet()) {
            total += this.items.get(key).getTotalPrice();
        }
        return total;


    }
}


















原创粉丝点击