javaSE进阶练习——商场功能的基本实现

需求:实现一商城,主要有店铺和商品栏,商品栏中有商品。商品的信息包括商品名、商品的描述、价格、库存、上架时间。

tips:
    1.商品使用自定义类,在设置具体属性值时进行判断抛出自定义异常,减少测试类中代码逻辑判断。
    2.商城使用HashMap,键储存店铺,值储存商品栏,商品栏为单列集合,使用ArraList存储商品信息。
    3.利用正则表达式、捕获异常、等对输入进行校验,使用while+continue循环进行重复判断。
    4.遇到复杂的逻辑不要慌,层层递进,在实现整体逻辑的基础,逐步加深局部逻辑。

详细代码如下:

1.测试类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ShoppingSystemTest {
    //    创建商品
    static HashMap<String, ArrayList<Goods>> hm = new HashMap<>();
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E");
    static Scanner sc = new Scanner(System.in);

    static {
        Goods s1 = new Goods("洗发水", "防脱发", 10.5, 100, sdf.format(new Date()));
        Goods s2 = new Goods("香皂", "洗澡", 5, 50, sdf.format(new Date()));
        Goods s3 = new Goods("牙膏", "漱口", 15, 100, sdf.format(new Date()));
        ArrayList<Goods> list = new ArrayList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        hm.put("生活区", list);
    }

    public static void main(String[] args) {
        while (true) {
            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("请输入:");
//        获取用户输入
            String choose;
            while (true){
                 choose = sc.nextLine();
                if(choose.matches("[1-6]")){
                    break;
                }else{
                    System.out.println("请输入1-6数字");
                    continue;
                }
            }

            int ch = Integer.parseInt(choose);
            switch (ch) {
                case 1 -> findAllGoods();
                case 2 -> addGoods();
                case 3 -> deleteGoods();
                case 4 -> findGoods();
                case 5 -> modifiedGoods();
                case 6 -> System.exit(0);
            }
        }
    }
    /*功能5    修改某个商品*/
    private static void modifiedGoods() {
        while (true) {
            String name;
            while (true) {
                try {
                    System.out.println("请输入店铺名称");
                    name = sc.nextLine();
                    if(!name.matches(".+")){
                        throw new EmptyNameException("店铺名称为空");
                    }
                    break;
                } catch (EmptyNameException e) {
                    System.out.println("店铺名称不能为空");
                    continue;
                }
            }
//        在HashMap中查询店铺名
            if (hm.containsKey(name)) {
                //存在 获取店铺的商品栏
                ArrayList<Goods> goodsList = hm.get(name);
                System.out.println("输入商品名");
                String nameGoods=sc.nextLine();
                int count=0;
                for (int i = 0; i < goodsList.size(); i++) {
                    count++;
                  Goods goods =goodsList.get(i);
                    if( goods.getName().equals(nameGoods)){//找到该商品
                        String input;
                        while (true) {
                            System.out.println("按1修改商品名称");
                            System.out.println("按2修改商品描述");
                            System.out.println("按3修改商品价格");
                            System.out.println("按4修改商品库存");
                            System.out.println("按5修改商品上架日期");
                             input=sc.nextLine();
                            if(input.matches("[1-5]")){
                                break;
                            }
                            else {
                                System.out.println("请输入1-5");
                                continue;}
                        }
                        switch (input){
                            case "1":
                                while (true) {
                                    try {
                                        System.out.println("输入商品新名称");
                                        String newName = sc.nextLine();
                                        goods.setName(newName);
                                        break;
                                    } catch (EmptyNameException e) {
                                        e.printStackTrace();
                                        continue;
                                    }
                                }
                                break;
                            case "2":
                                System.out.println("请输入商品新的描述");
                                String desGoods = sc.nextLine();
                                if (desGoods.matches(".+")) {
                                    goods.setDes(desGoods);
                                } else {
                                    goods.setDes("无");
                                }
                                break;
                            case "3":
                                while (true) {
                                    try {
                                        System.out.println("请输入新价格(元)");
                                        String inputPrice = sc.nextLine();
                                        if (inputPrice.matches("[0-9]+")) {
                                            Double priceGoods = Double.parseDouble(inputPrice);
                                            goods.setPrice(priceGoods);
                                            break;
                                        } else {
                                            System.out.println("输入不合法");
                                            continue;
                                        }
                                    } catch (ErrorPriceException e) {
                                        e.printStackTrace();
                                        continue;
                                    }
                                }
                                break;
                            case "4":
                                while (true) {
                                    try {
                                        System.out.println("请输入商品新库存数");
                                        //判断是否输入的数字
                                        String inputNumber = sc.nextLine();
                                        if (inputNumber.matches("[0-9]+")) {
                                            int countGoods = Integer.parseInt(inputNumber);
                                            goods.setCount(countGoods);
                                            break;
                                        } else {
                                            System.out.println("请输入阿拉伯数字");
                                            continue;
                                        }
                                    } catch (CountException e) {
                                        e.printStackTrace();
                                        continue;
                                    } }
                                break;
                            case "5":
                                System.out.println("输入上新时间,(yyyy年MM月dd日 HH:mm:ss E)");
                                String newTimeStr=sc.nextLine();
//                                字符串转日期格式
                                try {
                                    Date newTime = sdf.parse(newTimeStr);
                                    goods.setTime(sdf.format(newTime));
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                                break;
                        }

                    }
                }
                if(count==goodsList.size()-1){  System.out.println("没有该商品");}
                break;
            }
            else{
                    System.out.println("店铺不存在,重新输入店铺y,退出n");
                    String input=sc.nextLine();
                    if("y".equalsIgnoreCase(input)){
                     continue;
                  }else if("n".equalsIgnoreCase(input)){
                        System.out.println("退出");
                        System.exit(0);
                    }
                    else {
                        System.out.println("没有这种选项");
                        continue;
                    }
               }
         }
    }

    /*功能4    查询某个商品*/
    private static void findGoods() {
        System.out.println("请输入你想要的商品名");
        String input=sc.nextLine();
//        循环所有店铺,再循环商品栏,查看有无该商品名是
        Set<String> keySet = hm.keySet();
        int count=1;
        for (String name : keySet) {
            //     name为店铺名,goodsList为该店铺对应的商品栏
           ArrayList<Goods> goodsList=hm.get(name);
            for (Goods goods : goodsList) {//遍历商品栏查找对应商品
                if(goods.getName().equals(input)) {//找到对应商品,输出
                    System.out.println("商品所在店铺为" + name);
                    System.out.println( goods);
                    count=0;
                }
             }
          }
        if(count==1){System.out.println("没有该商品");}

    }

    /*功能3    删除商品*/
    private static void deleteGoods() {
        //        首先获取店铺名
        String name;
        while (true) {
            try {
                System.out.println("请输入店铺名称");
                name = sc.nextLine();
                if(!name.matches(".+")){
                    throw new EmptyNameException("店铺名称为空");
                }
                break;
            } catch (EmptyNameException e) {
                System.out.println("店铺名称不能为空");
                continue;
            }
        }
//        在HashMap中查询店铺名
        if (hm.containsKey(name)) {
            //存在,获取商品集合
            ArrayList<Goods> goodsList = hm.get(name);
            System.out.println("需要删除的商品名");
            String nameGoods=sc.nextLine();
            // 在商品ArrayList集合中找该商品
            int count=0;
            for (int i = 0; i < goodsList.size(); i++) {
                count++;
                Goods goods = goodsList.get(i);//循环获取单个商品对象
                if((goods.getName()).equals(nameGoods)){//判断商品名称是否需要删除
                    goodsList.remove(i);
                    System.out.println("删除" + nameGoods);
                    break;
                }
            }
            if(count==goodsList.size()-1){System.out.println("没找到商品");}
        }else{
            System.out.println("没有该店铺");
            System.exit(0);
        }
    }

    /*功能2    上架商品*/
    private static void addGoods() {
//        首先获取店铺名
        String name;
        while (true) {
            try {
                System.out.println("请输入你要上新商品的店铺名称");
                 name = sc.nextLine();
                if(!name.matches(".+")){
                    throw new EmptyNameException("店铺名称为空");
                }
                break;
            } catch (EmptyNameException e) {
                System.out.println("店铺名称不能为空");
                continue;
            }
        }
//        在HashMap中查询店铺名
        if (hm.containsKey(name)) {
            //存在
            ArrayList<Goods> goodsList = hm.get(name);
            Goods goods = new Goods();
            while (true) {
                try {
                    System.out.println("请输入新商品的名称");
                    String nameGoods = sc.nextLine();
                    goods.setName(nameGoods);//设置try...catch
                    break;
                } catch (EmptyNameException e) {
                    e.printStackTrace();
                    continue;
                }
            }
            System.out.println("请输入商品描述");
            String desGoods = sc.nextLine();
            if (desGoods.matches(".+")) {
                goods.setDes(desGoods);
            } else {
                goods.setDes("无");
            }
            while (true) {
                try {
                    System.out.println("请输入价格(元)");
                    String input = sc.nextLine();
                    if (input.matches("[0-9]+")) {
                        Double priceGoods = Double.parseDouble(input);
                        goods.setPrice(priceGoods);
                        break;
                    } else {
                        System.out.println("请输入阿拉伯数字");
                        continue;
                    }
                } catch (ErrorPriceException e) {
                    e.printStackTrace();
                    continue;
                }
            }
            while (true) {
                try {
                    System.out.println("请输入个数");
                    //判断是否输入的数字
                    String input = sc.nextLine();
                    if (input.matches("[0-9]+")) {
                        int countGoods = Integer.parseInt(input);
                        goods.setCount(countGoods);
                        break;
                    } else {
                        System.out.println("请输入阿拉伯数字");
                        continue;
                    }
                } catch (CountException e) {
                    e.printStackTrace();
                    continue;
                } }

//            自动生成上新时间
            Date time = new Date();
            goods.setTime(sdf.format(time));

//            添加商品信息无误,添加至商品
            goodsList.add(goods);
            System.out.println("添加成功");
        } else {
            while (true) {
                System.out.println("不存在,是否添加新店铺(Y/N)");
                String input=sc.nextLine();
                if("y".equalsIgnoreCase(input)){
                      hm.put(name,new ArrayList<>());//设置店铺名称为HashMap的键,同时创建Arraylist容器用于存储商品
                    System.out.println("添加成功");
                    break;
                }
                else if("n".equalsIgnoreCase(input)){
                    System.out.println("退出");
                    System.exit(0);
                }else{
                    System.out.println("没有该选项");
                    continue;}
            }
        }
    }

    /*功能1    查看全部商品*/
    private static void findAllGoods() {
        hm.forEach((String s, ArrayList<Goods> goodslist) -> {
//                s为店铺名,goodslist为商品架,商品在商品架里
            System.out.println("+++++++" + s + "+++++++");
            for (Goods goods : goodslist) {
                System.out.println("商品名称:" + goods.getName());
                System.out.println("描述:" + goods.getDes());
                System.out.println("价格:" + goods.getPrice()+"(元)");
                System.out.println("库存:" + goods.getCount());
                System.out.println("上架时间:" + goods.getTime());
                System.out.println("--------------------------------------");
            }
        });
    }
}

2.自定义商品类

public class Goods {
//    商品类
    private String name;
    private String des;
    private double price;
    private int count;
    private String time;
//    构造方法
    public Goods() {
    }
    public Goods(String name, String des, double price, int count, String time) {
        this.name = name;
        this.des = des;
        this.price = price;
        this.count = count;
        this.time = time;
    }
//    set/get
//    商品名
    public String getName() {
        return name;
    }
    public void setName(String name) {
        if(name.matches(".+")){
            this.name = name;}
        else{
            throw new EmptyNameException("商品名不能为空");
        }

    }
    //    商品描述
    public String getDes() {
        return des;
    }
    public void setDes(String des) {
            this.des = des;}

    //    商品价格
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        if(price<0){
            throw new ErrorPriceException("价格不能为负");
        }
        else{
        this.price = price;}
    }
    //    商品个数
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        if(count<=0){
            throw new CountException("个数不能小于0");
        }
        else{
            this.count = count;
        }
    }
    //    商品时间
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
//    重新tostring

    @Override
    public String toString() {
        return "{" +
                "'" + name + '\'' +
                ", 描述:'" + des + '\'' +
                ", 价格:" + price +
                ", 库存:" + count +
                ",上架时间:" + time +
                '}';
    }
}

3.自定义异常

public class ErrorPriceException extends RuntimeException{
    public ErrorPriceException() {
    }

    public ErrorPriceException(String message) {
        super(message);
    }
}
public class EmptyNameException extends RuntimeException {
    public EmptyNameException() {
    }
    public EmptyNameException(String message) {
        super(message);
    }
}
public class CountException extends RuntimeException {
    public CountException() {
    }

    public CountException(String message) {
        super(message);
    }
}

摘要信息: 电子商城系统主要功能包括:用户注册、用户登录、用户查看商城商品、用户购买商品、用户查看购物车并且清空购物车、用户找回账户以及密码、管理员登录、管理员注册、管理员查看用户信息、管理员删除用户信息、管理员删除商品信息、管理员添加商品信息、管理员修改商品信息、退出登录等。 图形可视化界面运行下能够显示系统启动进度条,删除、添加、注册等相关操作时能够弹出窗口加以提示,退出登录或系统时能够做到单击确认退出按钮才退出登录或系统,能够对用户加以提示。 注册用户或是管理员、添加商品信息、购买商品、删除商品或是用户信息、修改商品信息等相关操作时能够判断有无该用户或是商品。 用户在购买商品的时候同时更新商品的库存数量,管理员删除管理员时可以做到同时删除用户的购买信息。 主要内容: 一、项目名称   基于JAVASE的电子商城系统 二、功能要求 1、用户注册 2、用户登录 3、用户查看商品列表 4、用户购买商品 5、用户查看购买商品列表 6、用户清空购物车 7、用户找回账户和密码 8、管理员登录 9、管理员注册 10、查看用户信息 11、查看商品信息 12、删除用户信息 13、删除商品信息 14、添加商品信息 15、修改商品信息 16、退出用户登录 17、退出管理员登录 18、退出商城 三、需求分析 该系统的用户是商城消费者和商城管理者,根据客户的要求,可以注册、登录、购物、查看购物车信息、找回账户和密码,管理员可以对商品信息进行增加、修改、删除操作,可以对用户进行查看和删除。 四、设计思想 1、在控制台与图形可视化界面下运行 2、使用Mysql数据库存取用户登录信息和商品信息 3、使用List存取商品购买信息 4、把程序分为多个,多个之间的互相调用。 5、用户或是管理员进行注册、登录时能够提供校验码。 6、用户或是管理员获取数据库信息时能够与数据库进行交互。 7、用户购物要做到简洁明了。 8、用户只需要身份证号码和邮箱地址就能找回账户和密码。 9、注册、删除、修改等操作要有信息提示。 10、用户、管理员进行操作时能够做到操作提示与用户名提示。 11、退出登录或是退出系统时能够做到让用户有所考虑。 五、具体实现   1、技术思路: 界面:基于控制台与图形可视化界面(Swing)实现用户的输入和输出。 程序流程:在函数利用循环与递归 ,读取用户输入,调用模块实现各个子功能。 2、功能子模块划分: ① 注册模块 ② 登录模块 ③ 查看商品模块(查看商品列表,购买商品) ④ 查看购买商品信息 ⑤ 管理员登录(添加管理员信息,对商品信息进行查看、增加、 修改、删除,对用户信息进行查看、删除,删除用户信息 时能够做到同时删除消费记录) ⑥ 退出系统 六、运行截图 1、商城系统启动进度条(进度条能够做到动态加载): 2、商城主界面: 3、用户注册界面(填写注册信息不符合要求时能够弹窗提示、并 且判断用户名是否重复等): 4、用户登录界面(账户、密码不一致时能弹窗提示): 5、用户服务选择界面(能够提示用户名): 6、购买商品界面(能够判断输入的商品序号是否正确): 7、查看购物车界面(能够一键清空购物车): 8、查看商城商品信息界面: 9、管理员登录界面: 10、找回账户与密码界面: 11、管理员服务选择界面: 12、添加管理员界面: 13、查看用户信息界面: 14、删除用户信息界面: 15、删除商品信息界面: 16、添加商品信息界面: 17、修改商品信息界面: 18、退出账号、管理员登录、商城系统时要有提示:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值