码迷,mamicode.com
首页 > 其他好文 > 详细

识别类

时间:2018-09-18 20:44:43      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:map   get   value   框架   构造   extend   art   相关信息   使用   

识别类

题目:

用户在网上商城搜索并浏览商品,一旦看到中意的商品将商品放入购物车,可放多,可在购物车中增删商品,获得商品价格,最后点击结算生成订单

思维导图展示:

技术分享图片

类:用户、商城、商品、购物车、订单

方法:放入、增删、结算、浏览、搜索’

相关代码:

用户的定义

public class Customer   //顾客姓名、性别、电话、地址的基本信息
{
  String name;
  String sex;
  String phone_number;
  String adress;
  
 //构造方法,没有返回值
  Customer(String name1,String sex1,String phone_number1,String adress1) 
  {
     name = name1;
     sex=sex1;
     phone_number=phone_number1;
     adress=adress1;
     System.out.println(" 信息已填入,用户注册成功!");
  }

  //普通方法,必须有返回值
  void change()
  {
     System.out.println("请输入所要修改的信息:");

  }

商城的功能:

建立商品类数组,商品浏览就是数组数据的全部输出,搜索就是进行数组的遍历找到符合的关键字信息输出

商品信息:

public class Goods   //商品名、价格、剩余数量
{
  String name;
  String price;
  int number;
  
  Goods(String name1,String price1,int number1) 
  {
     name = name1;
     price=price1;
     number=number1;
     System.out.println(" 商品信息已录入");
  }

 void add{
    System.out.println("是否确定把该商品放入购物车?");
    输入选项进行购物车添加情况

}


购物车的设定、使用、增删:

设定商品类数组,利用extends继承,实现在购物车浏览商品信息,遍历数组找到并改变所要删减或增加的商品。

//加入购物车的商品价钱
public class CartItem {
    
        private Goods goods;
        private int quantity;
    
        //价钱应该等于商品的数量*价格
        private double price;
    
        
        //商品的价钱*数量
        public double getPrice() {
            return goods.getPrice() * this.quantity;
        }
    
        public Goods getGoods() {
            return goods;
        }
    
        public void setGoods(Goods goods) {
            this.goods = goods;
        }
    
        public int getQuantity() {
            return quantity;
        }
    
        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }
        
        public void setPrice(double price) {
            this.price = price;
        }
    }
//购物车
public class Cart {
    
        private Map<String, CartItem> goodsMap = new LinkedHashMap<>();
    
        //代表着购物车的总价
        private double price;
    
    
        //把购物项(用户传递进来的商品)加入到购物车里边去
        public void addGoods(Goods goods) {
    
            //获取得到购物项
            CartItem cartItem = goodsMap.get(goods.getId());
    
            //判断购物车是否存在该购物项,如果不存在
            if (cartItem == null) {
    
                //创建这个购物项对象
                cartItem = new CartItem();
    
                //将用户传递过来的商品作为购物项
                cartItem.setGoods(goods);
    
                //把该购物项的数量设置为1
                cartItem.setQuantity(1);
    
                //把购物项加入到购物车去
                goodsMap.put(goods.getId(), cartItem);
            } else {
    
                //如果存在该购物项,将购物项的数量+1
                cartItem.setQuantity(cartItem.getQuantity() + 1);
            }
        }

订单的罗列结算:

    
//购物车的总价
        public double getPrice() {
    
            double totalPrice = 0;
    
            for (Map.Entry<String, CartItem> me : goodsMap.entrySet()) {
    
                //得到每个购物项
                CartItem cartItem = me.getValue();
    
                //将每个购物项的钱加起来
                totalPrice += cartItem.getPrice();
            }
    
            return totalPrice;
        }
    
    
        public Map<String, CartItem> getBookMap() {
            return bookMap;
        }
    
        public void setBookMap(Map<String, CartItem> bookMap) {
            this.bookMap = bookMap;
        }
    
    
        public void setPrice(double price) {
            this.price = price;
        }
    }

相关图的展示:

相关信息的链接:

Java类的定义及其实例化:https://blog.csdn.net/yongchaocsdn/article/details/53572983

Java类的定义、声明及使用:https://blog.csdn.net/miao_9/article/details/62057732

java:https://www.cnblogs.com/Java3y/p/8467778.html

https://blog.csdn.net/axi295309066/article/details/53066363

java Map集合框架之LinkedHashMap:https://blog.csdn.net/u011659172/article/details/50634308

识别类

标签:map   get   value   框架   构造   extend   art   相关信息   使用   

原文地址:https://www.cnblogs.com/linxiaolu/p/9668968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!