购物车项目(命令行界面版)

由于今日上自习,并没有上课,所以也就没有笔记啦,不过今日有时间可以整理一下之前的笔记,感觉之前的内容还没有消化,课程进度确实很快,主要是对于我这种小白,接受起来还是有一定难度。今天先是整理了前两天落下的笔记,博客还没填,感觉博客落下的太多了,差不多整理好笔记之后,也没有去写面试讲稿,虽然面试讲稿很重要,对了,还有模拟题,听说下周六就会阶段考试了,感觉没底。个人感觉面试讲稿真的非常难写,对于我来说,一是不知道怎么组织语言,二是需要大量时间查询资料,但不能拖下去了,明天必须得写,得一题一题填,而且以后早上得读得背诵。今天还独立的完成了老师布置的项目,没有看老师的讲解视频,只是看了界面是什么样、有哪些功能,从下午四点开始写,中间吃了晚饭,看了接近一个小时的电视剧(双峰第二季第一集),初略计算大概用了接近五个小时才写得有点样子,功能差不多能实现,不过健壮性不行,对于输入没有控制,但实现对输入的控制起来应该也不难,不过得花些时间,所以就不准备写了。最后贴上今日的项目代码,嘿嘿,虽然不好,但我尽力了?

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

/**
 * 功能:京东首页
 */
public class Welecome {

    //商品
    static Map<Integer,Products> map=new HashMap<Integer, Products>();

    //购物车
    static Map<Integer,Carts> mapCart=new HashMap<Integer, Carts>();
    //购买的商品编号
    static int number=0;
    //购买商品的数量
    static int count=0;

    public static void main(String[] args) {
        //循环
        while (true){
            //显示菜单
            System.out.println("******欢迎进入京东商城******");
            System.out.println("\t\t1.添加商品");
            System.out.println("\t\t2.查看所有商品");
            System.out.println("\t\t3.查看指定编号商品");
            System.out.println("\t\t4.添加到购物车");
            System.out.println("\t\t5.显示购物车");
            System.out.println("\t\t6.清空购物车");
            System.out.println("\t\t7.退出");
            System.out.println("**************************");

            //提取用户选择
            Scanner sc=new Scanner(System.in);
            System.out.print("请选择菜单:");
            int select=sc.nextInt();

            //已有商品
            Products p1=new Products(1111,"蜘蛛王皮鞋鞋鞋",200,"黑色",42,100);
            Products p2=new Products(3333,"Thinkpad x240",5900,"黑色",12,50);
            Products p3=new Products(2222,"iPhone 7plus",5600,"白色",5,5000);
            map.put(p1.getNumber(),p1);
            map.put(p2.getNumber(),p2);
            map.put(p3.getNumber(),p3);

            //选择匹配
            switch(select){
                case 1:
                    //添加商品
                    Products p=new Products().add();
                    map.put(p.getNumber(),p);
                    System.out.println("添加成功?");
                    break;
                case 2:
                    //展示所有商品信息
                    new Products().show(map);
                    break;
                case 3:
                    //展示按编号查找的商品信息
                    new Products().show(new Products().find());
                    break;
                case 4:
                    //添加商品到购物车
                    new CartOperate().add();
                    break;
                case 5:
                    //展示购物车信息
                    new CartOperate().show();
                    break;
                case 6:
                    //清空购物车
                    mapCart.clear();
                    System.out.println("购物车已清空(浪子回头金不换?)");
                    break;
                case 7:
                    //退出程序
                    System.out.println("即将退出商城,欢迎下次光临,再见?");
                    return;
            }
            System.out.println("-----------------------------------");
        }
    }
}
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * 功能:实现商品
 */
public class Products {
    //商品属性
    private int number;
    private String name;
    private double price;
    private String color;
    private int size;
    private int stock;

    //构造方法
    public Products() {
    }
    public Products(int number, String name, double price, String color, int size, int stock) {
        this.number=number;
        this.name = name;
        this.price = price;
        this.color = color;
        this.size = size;
        this.stock = stock;
    }

    //set和get方法
    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    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;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    @Override
    public String toString() {
        return  number +"\t"+
                name  +"\t"+
                price +"\t\t"+
                color +"\t\t"+
                size +"\t\t"+
                stock;
    }

    //展示商品信息
    public void show(Map<Integer,Products> map){
        //输出商品信息
        System.out.println("编号"+"\t\t商品名称"+"\t\t\t价格"+"\t\t\t颜色"+"\t\t尺寸"+"\t\t库存");
        Set set=map.keySet();
        for(Object obj:set){
            System.out.println(map.get(obj));
        }
    }

    //展示查找商品后信息
    public void show(Integer id){
        if(Welecome.map.containsKey(id)){
            System.out.println("编号"+"\t\t商品名称"+"\t\t\t价格"+"\t\t\t颜色"+"\t\t尺寸"+"\t\t库存");
            System.out.println(Welecome.map.get(id));
        }else{
            System.out.println("很抱歉,目前没有该编号商品");
        }
    }

    //添加商品
    public Products add(){
        Products p=new Products();
        Scanner sc=new Scanner(System.in);

        //获取商品编号
        System.out.print("请输入商品编号:");
        p.number=sc.nextInt();
        //获取商品名称
        System.out.print("请输入商品名称:");
        p.name=sc.next();
        //获取商品价格
        System.out.print("请输入商品价格:");
        p.price=sc.nextDouble();
        //获取商品颜色
        System.out.print("请输入商品颜色:");
        p.color=sc.next();
        //获取商品尺寸
        System.out.print("请输入商品尺寸:");
        p.size=sc.nextInt();
        //获取商品库存
        System.out.print("请输入商品库存:");
        p.stock=sc.nextInt();

        return p;
    }

    //按编号查询商品
    public int find(){
        Scanner sc=new Scanner(System.in);

        //获取商品编号
        System.out.print("请输入商品编号:");
        int id=sc.nextInt();

        return id;
    }
}
/**
 * 功能:实现购物车
 */
public class Carts {
    //购物车属性
    private int number;
    private String name;
    private double price;
    private int counts;
    private double subtotal;

    //构造方法
    public Carts() {
    }
    public Carts(int number, String name, double price,int counts, double subtotal) {
        this.number = number;
        this.name = name;
        this.price = price;
        this.counts=counts;
        this.subtotal = subtotal;
    }

    //set和get方法

    public int getCounts() {
        return counts;
    }

    public void setCounts(int counts) {
        this.counts = counts;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    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;
    }

    public double getSubtotal() {
        return subtotal;
    }

    public void setSubtotal(double subtotal) {
        this.subtotal = subtotal;
    }

    @Override
    public String toString() {
        return number +"\t"+
                name +"\t"+
                price +"\t\t"+
                counts+"\t\t"+
                subtotal;
    }
}
import java.util.Scanner;
import java.util.Set;

import static cd.khue.jd.Welecome.*;

/**
 * 功能:展示购物车信息及添加商品到购物车
 */
public class CartOperate {

    //显示购物车信息
    public void show(){
        //如果购物车为空
        if(mapCart.size()==0){
            System.out.println("亲,您的购物车空空如也");
        }else{
            //总金额
            int sum=0;
            System.out.println("编号"+"\t\t商品名称"+"\t\t\t单价"+"\t\t\t数量"+"\t\t小计");
            //展示购物车信息
            Set set=mapCart.keySet();
            for(Object obj:set){
                //展示购物车内的商品信息
                System.out.println(mapCart.get(obj));
                //总金额累计
                sum+=mapCart.get(obj).getSubtotal();
            }
            //展示总金额
            System.out.println("总计:"+sum+"元?");
        }
    }

    //添加商品到购物车
    public void add(){
        Scanner sc=new Scanner(System.in);
        //录入商品编号
        System.out.print("请输入商品编号:");
        number=sc.nextInt();
        //录入购买数量
        System.out.print("请输入购买的数量:");
        count=sc.nextInt();
        //输出信息
        System.out.println("添加到购物车成功?");
        //将添加的商品添加到购物车
        Carts carts=new Carts(number,
                                map.get(number).getName(),
                                map.get(number).getPrice(),
                                count,
                        count*map.get(number).getPrice());
        //如果购物车已经有某一类商品,就让其计数累加
        if(!mapCart.containsKey(number)){
            mapCart.put(number,carts);
        }else{
            int a=mapCart.get(number).getCounts();
            mapCart.put(number,carts);
            mapCart.get(number).setCounts(a+mapCart.get(number).getCounts());
        }
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值