JAVA面向对象编程之购物车

一、前期调查

成员及分工

 

姓名分工
吴艺发ShoppingCart、ShoppingMall、itemEntry
叶伟祥Main、Menu、Goods

 

67a84b908c9b46ac913a28331fe86841.png

如图,购物车程序需要有商品条目,商品数量,对商品条目的操作,生成订单等功能

 

二、图示

业务流程图

10febe7ce95148dab5f37ba621607ea9.png

功能结构图

bd119e823e1448048d74cb416352980a.png

 

三、代码展示

1.Main

功能选择

import java.util.List;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Scanner pause = new Scanner(System.in);
        ShoppingMall mall = new ShoppingMall();
        ShoppingCart cart = new ShoppingCart();
        List<Goods> list = mall.showGoods();
        while(true) {
            Menu.beginMenu();
            System.out.println("请输入你的功能选择");
            int choice = input.nextInt();
            switch(choice) {
            case 1://展示商品
                for (Goods temp : list) {
                    System.out.println(temp);
                }
                System.out.println("按回车返回主界面");
                String Pause = pause.nextLine();
                break;
            case 2://按类型查询商品
                System.out.println("请输入你要查询的类型");
                String type = input.next();
                System.out.println(mall.searchByType(type));
                System.out.println("按回车返回主界面");
                Pause = pause.nextLine();
                break;
            case 3://按id查询商品
                System.out.println("请输入你要查询的id");
                System.out.println(mall.searchById(input.nextInt()));
                System.out.println("按回车返回主界面");
                Pause = pause.nextLine();
                break;
            case 4://添加商品到购物车
                System.out.println("请输入你要添加的商品的id,如果输入-1则结束添加商品");
                while(true) {
                    int flag = input.nextInt();
                    if(flag==-1) {
                        System.out.println("按回车返回主界面");
                        Pause = pause.nextLine();
                        break;
                    }
                    else {
                        cart.addToCart(list.get(flag));
                    }
                }
                break;
            case 5://减少购物车商品的数量
                System.out.println("请输入你要减少的商品的id");
                cart.remove(input.nextInt());
                System.out.println("按回车返回主界面");
                Pause = pause.nextLine();
                break;
            case 6://查看购物车
                List<ItemEntry> list1 = cart.showMyCart();
                for (ItemEntry temp1 : list1) {
                    System.out.println(temp1);
                }
                System.out.println("按回车返回主界面");
                Pause = pause.nextLine();
                break;
            case 7://清空购物车
                cart.emptyCart();
                System.out.println("按回车返回主界面");
                Pause = pause.nextLine();
                break;
            case 8://购物车预结算
                List<ItemEntry> list2 = cart.showMyCart();
                list2.get(0).getItem().getPrice();
                int temp=0;
                for (ItemEntry temp1 : list2) {
                    temp=temp1.getPiece()*temp1.getItem().getPrice()+temp;
                }    
                System.out.println("总价为:"+temp);
                System.out.println("按回车返回主界面");
                Pause = pause.nextLine();
                break;
        }
    }
}

 

2.Menu

打印菜单

public class Menu {
    
    public static void beginMenu() {
        System.out.println("购物车");
        System.out.println("1.商品展示");
        System.out.println("2.以类型查询商品");
        System.out.println("3.以编号查询商品");
        System.out.println("4.添加商品至购物车");
        System.out.println("5.减少购物车商品的数量");
        System.out.println("6.查看购物车");
        System.out.println("7.清空购物车");
        System.out.println("8.购物车预结算");
    }
}

3.Goods

商品基本信息

public class Goods {
       private Integer id;
       private String name;
       private int price;
       private String type;
       public Goods(Integer id,String name,int price,String type)
       {
          this.id=id;
          this.name=name;    
          this.price=price;
          this.type=type;
       }//商品的基本信息
       @Override
        public String toString() {
            return "商品" + id + ":名称:" + name +   ", 价格:" + price + ", 类型:"
                    + type ;
        }
        public int getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
        public int getPrice() {
            return price;
        }
        public void setPrice(int price) {
            this.price = price;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
    }

4.ShoppingMall

展示商品、搜索商品

public class ShoppingMall {
   List<Goods> goods=new ArrayList<>();
   {
       goods.add(new Goods(0,"java学习笔记",98,"书籍"));
       goods.add(new Goods(1,"计算机网络",59,"书籍"));
       goods.add(new Goods(2,"iPad 9",2499,"电子产品"));
       goods.add(new Goods(3,"面包",30,"食品"));
       goods.add(new Goods(4,"排球",60,"运动"));
   }
   public  List<Goods> showGoods(){
       return this.goods;
   }
   public Goods searchById(Integer id) {
       int i=0;
       int flag=1;
       for(i=0;i<goods.size();i++) {
           if(goods.get(i).getId()==(id)) {
               return goods.get(i);
           }
       }
       if(flag==1)
           System.out.println("抱歉,没有找到搜索的商品");
    return null;
   }//按id搜索商品
   public List<Goods> searchByType(String type) {
       int i=0;
       List<Goods> list=new ArrayList<>();
       for(i=0;i<goods.size();i++) {
           if(goods.get(i).getType().equals(type))
           {
               list.add(goods.get(i));
           }
       }
       return list;
   }

   }

5.ShoppingCart

加入商品至购物车

减少购物车商品数量

查看购物车

清空购物车

public class ShoppingCart {
    private List<ItemEntry> itemList = new ArrayList<>();

    public void addToCart(Goods e) {

        int index = findById(e.getId());
        if (index == -1) {
            itemList.add(new ItemEntry(e));

        } else {
            itemList.get(index).increase();
        }
    }

    public List<ItemEntry> getItemList() {
        return itemList;
    }
    public List<ItemEntry> emptyCart(){
        itemList.clear();
        return itemList;
    }
    public List<ItemEntry> showMyCart() {
        return itemList;
    }

    public boolean remove(Integer id) {
        if (id == null)
            return false;
        int index = findById(id);
        if (index == -1) {// 未找到
            return false;
        } else {
            ItemEntry entry = this.itemList.get(index);
            if (entry.getPiece() <= 1) {
                this.itemList.remove(index);
            }
            else
            {
                 entry.decrease();
            }
        }
        return true;
    }

    private int findById(Integer id) {
        int index = -1;
        if (itemList.size() > 0) {
            for (int i = 0; i < itemList.size(); i++) {
                if (itemList.get(i).getItem().getId() == id)
                    index = i;
            }
        }
        return index;
    }
}

6.ItemEntry

购物车中商品条目

public class ItemEntry {
        Goods item;
        int piece;

        public ItemEntry(Goods item) {
            this.item = item;
            piece = 1;
        }       

        public void increase() {
            piece++;
        }

        public Goods getItem() {
            return item;
        }

        public void setItem(Goods item) {
            this.item = item;
        }

        public int getPiece() {
            return piece;
        }

        public void setPiece(int piece) {
            this.piece = piece;
        }

        public void decrease() {
            piece--;
        }
        public String toString() {
            return "购物车中有:" + item + ", 数量为:" + piece ;
        }

    }

 

本系统哪里体现了面向对象的封装性?
   在ShoppingCart类,外部的类可以通过其提供的功能,来完成商品放入购物车等操作。外部的类是不能直接访问到ShoppingCart类的items。此举体现了封装性。

项目包结构(package的划分)与关键代码:项目的包结构(为什么要这样设计包结构)
   商品,商城,购物车 功能比较简单,放在同一个包可以加快开发效率
 

 

 

 

 

 

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值