购物车系统案例(用集合实现)

购物车案例(用集合实现)

作者:希有

项目需求

实现一个购物车的功能:用户输入商品信息,计算所购商品总金额,打印购物车小票

项目开发

步骤
  1. 商品类

    package day07.Collection;
    
    public class Product {
    	private int id;
    	private String name;
    	private float price;
    	private int count;
    	public Product() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    	public Product(int id, String name, float price, int count) {
    		super();
    		this.id = id;
    		this.name = name;
    		this.price = price;
    		this.count = count;
    	}
    	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 float getPrice() {
    		return price;
    	}
    	public void setPrice(float price) {
    		this.price = price;
    	}
    	public int getCount() {
    		return count;
    	}
    	public void setCount(int count) {
    		this.count = count;
    	}
    	@Override
    	public String toString() {
    		return "Product [id=" + id + ", name=" + name + ", price=" + price + ", count=" + count + "]";
    	}
    }
    
  2. 购物车类

    package day07.Collection;
    
    import java.text.DecimalFormat;
    import java.util.List;
    
    public class OrderCar {
    	private String orderId;
    	private List<Product> product;
    
    	public OrderCar() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    
    	public OrderCar(String orderId, List<Product> product) {
    		super();
    		this.orderId = orderId;
    		this.product = product;
    	}
    
    	public String getOrderId() {
    		return orderId;
    	}
    
    	public void setOrderId(String orderId) {
    		this.orderId = orderId;
    	}
    
    	public List<Product> getProduct() {
    		return product;
    	}
    
    	public void setProduct(List<Product> product) {
    		this.product = product;
    	}
    
    	@Override
    	public String toString() {
    		return "OrderCar [orderId=" + orderId + ", product=" + product + "]";
    	}
    
    	// 求总金额
    	public void totalMoney() {
    		System.out.print("总金额:");
    		float money = 0f;
    		if(product != null) {
    			for (int i = 0; i < product.size(); i++) {
    				Product products = product.get(i);
    				money += products.getPrice()*products.getCount();
    			}
    			DecimalFormat decimalFormat = new DecimalFormat("¥##,###.00");
    			String _money = decimalFormat.format(money);
    			System.out.println(_money);
    
    		}else {
    			System.out.println("您没有选购商品!");
    		}
    		
    	}
    	
    	//展现用户购物清单
    	public void displayItem() {
    		System.out.println("*购物清单*");
    		System.out.println("订单编号:"+orderId);
    		System.out.println("id\t名称\t价格\t数量");
    		System.out.println("-----------------------------");
    		for (int i = 0; i < product.size(); i++) {
    			Product products = product.get(i);
    			DecimalFormat decimalFormat = new DecimalFormat("¥,###.00");
    			String price = decimalFormat.format(products.getPrice());
    			System.out.println(products.getId()+"\t"+products.getName()+"\t"+
    					price+"\t"+products.getCount());
    					
    		}
    		System.out.println("-----------------------------");
    		
    		totalMoney();
    	}
    }
    
    
  3. 测试类Case_GoShopping.java

    package day07.Collection;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    /**
     * 实现一个购物车的功能
     * @author xiyou
     *
     */
    public class Case_GoShopping {
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		//获取毫秒值类型的订单id
    		long orderId = System.currentTimeMillis();
    		//创建一个购物车对象
    		OrderCar orderCar = new OrderCar(String.valueOf(orderId), null);
    		//定义一个商品集合
    		ArrayList<Product> products = new ArrayList<>();
    		
    		String iscontinue = "y";
    		//循环接收用户继续输入的商品信息
    		while(iscontinue.equalsIgnoreCase("y")) {
    			System.out.println("请输入您购买的商品信息(包括id,名称,价格,数量):");
    			String product = scanner.next();
    			//将商品信息切分后存入_product对象中
    			String[] split = product.split(",");
    			
    			Product _product = new Product(Integer.parseInt(split[0]), split[1], Float.parseFloat(split[2]), Integer.parseInt(split[3]));
    			products.add(_product);
    			
    			System.out.println("是否继续购物?(y/n)");
    			iscontinue = scanner.next();
    			//将用户购买的商品信息放入购物车orderCar里面
    			orderCar.setProduct(products);
    		}
    		orderCar.displayItem();
    	}
    	
    }
    
    
    1. 效果

    项目总结

    1. 获取毫秒值的方法是: System.currentTimeMillis();
    2. 对总金额进行格式化的方法:new DecimalFormat(“¥##,###,##”);
    3. 在计算总金额的时候注意判断购物车没有商品的情况,否则会出现空指针异常, if(product != null)
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读