zoukankan      html  css  js  c++  java
  • 面向对象设计作业 (购物车) *完成进度:76%(11.31 新增简陋UI)

    面向对象设计作业——购物车

    小组成员:刘炜 基本代码/博客 UML
    曾纪 登陆界面代码/调查
    钟恒 树状图+调查

    1.购物车调查——京东


    *选择京东是因为京东购物车的功能较为简便但十分完善,十分适合作为参考的对象。
    *考虑到自身技术问题,没有去考察京东的登陆界面,而购物车相关的用户登陆功能还没有实现。

    2.系统功能描述:基本的购物车功能,包括增加,减少商品数量,修改商品的信息,清空购物车。

    3.购物车基本功能树状图:

    4.UML类图

    5.购物车运行展示(11.31 新增ui)

    (1)基础界面

    *UI展示(未完成)

    (2)删除商品

    (3)添加/减少商品(修改)

    (4)清空购物车

    6.代码展示:

    UI界面(未完成)

    package UI;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class goodui extends JFrame{
        public void createJFrameWindow(){
            JFrame jf = new JFrame();           
            jf.setBounds(600, 300, 400, 310);   
            jf.setVisible(true);                
            jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            JLabel jl = new JLabel("购物车1.0.2");
            JButton jb = new JButton("退出");
            JButton jk = new JButton("删除商品");
            JButton jj = new JButton("修改商品信息");
            JButton jm = new JButton("清空购物车");
            jb.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    jf.dispose();               
                }
            });
            jf.setLayout(null);                 
            jl.setBounds(155, 30, 120, 30);    
            jb.setBounds(280, 200, 100, 50);    
            jk.setBounds(190, 200, 100, 50);
            jj.setBounds(100, 200, 100, 50);
            jm.setBounds(10, 200, 100, 50);
            Container c = jf.getContentPane();
            c.add(jl);
            c.add(jb);
            c.add(jk);
            c.add(jj);
            c.add(jm);
            jf.setVisible(true);                
            jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        }
    }
    

    商品类-基础属性

    package ShoppingCart;
    
    public class Product {
    	private int id;// 商品id
    	private String name;// 商品name
    	private double price;// 单价price
    
    	public Product(int id, String name,double price) {
    		this.id = id;
    		this.name = name;
    		this.price = price;
    	}
    
    	@Override
    	public String toString() {
    		return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
    	}
    
    	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;
    	}
    
    	
    }
    

    购物车类

    package ShoppingCart;
    
    public class ProductItem {
    	private Product product;//购买的商品
    	private int count;//商品数量
    
    	public ProductItem() {
    	}
     
    	public ProductItem(Product product, int count) {
    		this.product = product;
    		this.count = count;
    	}
     
    	public Product getProduct() {
    		return product;
    	}
    	public void setProduct(Product product) {
    		this.product = product;
    	}
    	public int getCount() {
    		return count;
    	}
    	public void setCount(int count) {
    		this.count = count;
    	}
    	public double totalMoney(){//总价统计
    		double price=product.getPrice();//获取商品单价
    		return price*count;
    	}
    
    
    }
    

    购物车相关功能实现

    package ShoppingCart;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.Map;
    public class Shoppingcart {
    private Map<Integer,ProductItem> map=new LinkedHashMap<Integer,ProductItem>();
    	
    	public void addProduct(Product p){//添加商品
    		int productId=p.getId();
    		if(map.containsKey(productId)){ //判断商品编号是否存在
    			ProductItem productItem=map.get(productId);
    			productItem.setCount(productItem.getCount()+1); //数量增加
    		}else{
    			map.put(productId, new ProductItem(p,1));//新增商品
    		}
    	}
    	public void showAll(){//查看全部订单信息
    		Collection<ProductItem> productItems = map.values();//获取键值
    		Iterator<ProductItem> iterator = productItems.iterator();
    		while(iterator.hasNext()){
    			ProductItem productItem = iterator.next();
    			Product product = productItem.getProduct();
    			System.out.println("商品编号:"+product.getId()+" 名称:"
    			+product.getName()+" 单价:"+product.getPrice()+" 数量:"+productItem.getCount()
    			+" 小计:"+productItem.totalMoney());
    		}
    	}
    	public boolean deleteProduct(int productId){//删除商品
    		if(map.containsKey(productId)){
    			map.remove(productId);
    			return true;
    		}
    		return false;
    	}
    	public boolean modifyProduct(int productId,int count){//修改商品数量
    		if(map.containsKey(productId)){
    			if(count>=1){
    				ProductItem productItem = map.get(productId);
    				productItem.setCount(count);
    				return true;
    			}else if(count==0){//删除某商品
    				deleteProduct(productId);
    				return true;
    			}	
    		}
    		return false;
    	}
    	
    	public void clearCart(){//清空购物车
    		map.clear();
    	}
    	
    	public double totalAllMoney(){//商品总价值
    		double total=0;
    		Collection<ProductItem> productItems = map.values();
    		Iterator<ProductItem> iterator = productItems.iterator();
    		while(iterator.hasNext()){
    			ProductItem productItem = iterator.next();
    			double money=productItem.totalMoney();
    			total+=money;
    		}
    		return total;
    	}
    
    
    }
    
    

    菜单

    package ShoppingCart;
    import java.util.Scanner;
    public class Test {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		Shoppingcart cart = new Shoppingcart();
    		Product p1 = new Product(1, "PS5", 9599);
    		Product p2 = new Product(2, "XSX", 4999);
    		Product p3 = new Product(3, "switch", 2599);// test
    		System.out.println("------------购物车-----------");
    		cart.addProduct(p1);
    		cart.addProduct(p2);
    		cart.addProduct(p3);
    		cart.showAll();
    		
    		System.out.println("-      [1]删除购物车商品             -");
    		System.out.println("-      [2]添加/减少所需商品                -");
    		System.out.println("-      [3]清空购物车                -");
    		System.out.println("-----输入相应序号执行下一步-----");
    		int num=in.nextInt();
    		if(num==1){
    			System.out.println("------------------------");
    			System.out.println("请输入你要删除的商品编号:");
    			int id=in.nextInt();
    			boolean flag = cart.deleteProduct(id);
    			if (flag) {
    				System.out.println("编号为:" + id + "的商品删除成功!");
    			} else {
    				System.out.println("删除失败");
    			}
    			cart.showAll();
    		}
    		if(num==2){
    			System.out.println("------------------------");
    			System.out.println("请输入你要增加/减少的商品编号:");
    			int id1=in.nextInt();
    			System.out.println("请输入你要修改后的商品数量:");
    			int count=in.nextInt();
    			boolean flag2 = cart.modifyProduct(id1, count);
    			if (flag2) {
    				System.out.println("编号为:" + id1 + "的商品修改成功!");
    			} else {
    				System.out.println("修改失败");
    			}
    			cart.showAll();
    		}
    		if(num==3)
    		{
    			System.out.println("已清空!");
    			cart.clearCart();
    			cart.showAll();
    		}
    	
    		
    		System.out.println("总价格:" + cart.totalAllMoney());
    	}
    
    }
    
    

    7.相关问题

    暂无

  • 相关阅读:
    DNS隧道
    记录上锁(fcntl)
    posix对线程的调整
    MySQL创建存储过程
    MySQL的WHERE语句中BETWEEN与IN的用法和他们的区别
    mysql中distinct
    线程的工作方式-流水线
    可执行程序的生成过程
    线程高级编程
    time函数及其用法
  • 原文地址:https://www.cnblogs.com/MIKEHRO/p/13965890.html
Copyright © 2011-2022 走看看