在线商城 案例实战

商品类

package com.itheima02;
//商品类
public class Shop {
    //封装商品变量
    private String shopname;//商品名称
    private int shopprice;//商品价格
    private int  shopstock;//商品库存
    //无参,带参构造方法
    public Shop() { }
    public Shop(String shopname, int shopprice, int shopstock) {
        this.shopname = shopname;
        this.shopprice = shopprice;
        this.shopstock = shopstock;
    }
    //成员方法:get/set

    public String getShopname() {
        return shopname;
    }

    public void setShopname(String shopname) {
        this.shopname = shopname;
    }

    public int  getShopprice() {
        return shopprice;
    }

    public void setShopprice(int shopprice) {
        this.shopprice = shopprice;
    }

    public int getShopstock() {
        return shopstock;
    }

    public void setShopstock(int shopstock) {
        this.shopstock = shopstock;
    }
}

人类

package com.itheima02;
//人类
public class People {
    //封装人的变量
    private String username;//用户名
    private String password;//密码
    /*private String business;//商家
    private String customer;//客户*/
    private String identity;//身份:商家或者客户

    //无参,带参构造方法
    public People() { }
    public People(String username, String password,String identity) {
        this.username = username;
        this.password = password;
        /*this.business = business;
        this.customer = customer;*/
        this.identity =identity;
    }
    //成员方法:set/get

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

   /* public String getBusiness() {
        return business;
    }

    public void setBusiness(String business) {
        this.business = business;
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }*/

    public String getIdentity() {
        return identity;
    }

    public void setIdentity(String identity) {
        this.identity = identity;
    }
}

主页面

package com.itheima02;

import java.util.ArrayList;
import java.util.Scanner;

//测试类
public class ShopDemo {
    public static void main(String[] args) {
        //创建集合,储存用户注册的用户名和密码及身份
        ArrayList<People> user = new ArrayList<>();
        //创建集合,储存商品的相关信息
        ArrayList<Shop> shop = new ArrayList<>();
        //调用主页面方法
        mainPage(user, shop);
    }

    //创建方法,实现主页面
    public static void mainPage(ArrayList<People> user, ArrayList<Shop> shop) {
        //创建死循环,实现主页面持续出现
        ol:
        while (true) {
            //键盘录入选择
            Scanner sc = new Scanner(System.in);
            //主界面提示
            System.out.println("-------请选择您的操作-------");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.退出");

            int choice = sc.nextInt();
            //switch语句进行选择
            switch (choice) {
                case 1:
                    // System.out.println("登录");
                    //调用登录方法,实现用户登录
                    login(user, shop);
                    break;
                case 2:
                    //System.out.println("注册");
                    //调用方法,实现用户注册
                    register(user);
                    break;
                case 3:
                    System.out.println("正在退出,欢迎下次光临");
                    break ol;
                default:
                    System.out.println("您的输入有误,请重新输入");
                    break;
            }
        }
    }

    //创建方法,实现登录页面
    public static void login(ArrayList<People> user, ArrayList<Shop> shop) {
        //提示,键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的昵称");
        String name = sc.nextLine();

        System.out.println("请输入您的密码");
        String password = sc.nextLine();
        //遍历集合,如果不存在该账号,报错
        //判断集合是否为空
        if (user.size() == 0) {
            System.out.println("该账户不存在,请先注册用户信息");
        }
        //遍历集合
        for (int i = 0; i < user.size(); i++) {
            //判断键盘录入的用户信息和集合中的是否一致,不一致,则报错
            if (user.get(i).getUsername().equals(name) && user.get(i).getPassword().equals(password)) {
                System.out.println("登陆成功");
                //判断,如果用户身份信息为商户,则调用商品管理页面
                if (user.get(i).getIdentity().equals("0")) {
                    //调用商品管理页面
                    management(shop);
                    return;//结束方法
                } else {
                    //调用商品购买页面
                    buyShop(shop);
                    return;//结束方法
                }
               //遍历到最后一个还不对
            } else if(i==user.size()-1){
                System.out.println("用户名或密码错误,登陆失败");
            }
        }
    }

    //创建方法,实现注册界面
    public static void register(ArrayList<People> user) {
        ol:
        while (true) {
            //提示,键盘录入
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您的昵称");
            String name = sc.nextLine();

            System.out.println("请输入您的密码");
            String password = sc.nextLine();

            System.out.println("请输入您的角色(0为商户,1为客户)");
            String player = sc.nextLine();

            //遍历集合,判断用户名是否已经注册过
            for (int i = 0; i < user.size(); i++) {
                if (user.get(i).getUsername().equals(name)) {
                    System.out.println("该用户名已被注册,请重新输入");
                    break ol;
                }
            }
            //判断角色输入是否合法
            switch (player) {
                case "0", "1":
                    break;
                default:
                    System.out.println("您的输入不符合要求,请重新输入");
                    break ol;
            }

            //创建对象,加入集合中
            People identity = new People(name, password, player);
            user.add(identity);
            System.out.println("注册成功");
            break ol;
        }
    }

    //创建方法,实现商品管理页面
    public static void management(ArrayList<Shop> shop) {
        //创建死循环
        ol:
        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.退出登录");
            //键盘录入
            String choice = new Scanner(System.in).next();
            switch (choice) {
                case "1":
                    //调用方法,添加商品
                    addShop(shop);
                    break;
                case "2":
                    //调用方法,查看商品
                    findShop(shop);
                    break;
                case "3":
                    //调用方法,修改商品
                    modifyShop(shop);
                    break;
                case "4":
                    //调用方法,删除商品
                    deleteShop(shop);
                    break;
                case "5":
                    //退出登录
                    System.out.println("正在退出,欢迎下次光临");
                    break ol;
                default:
                    System.out.println("请选择正确的格式");
                    break;
            }
        }
    }

    //创建方法,实现商品添加
    public static void addShop(ArrayList<Shop> shop) {
        //提示,并键盘录入对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入商品名称");
        String shopname = sc.nextLine();

        System.out.println("请输入商品价格");
        int shopprice = sc.nextInt();

        System.out.println("请输入商品库存");
        int shopstock = sc.nextInt();
        //创建商品对象,添加到集合中
        Shop shop1 = new Shop(shopname, shopprice, shopstock);
        shop.add(shop1);
        System.out.println("添加商品成功");

    }

    //创建方法,实现商品查看
    public static void findShop(ArrayList<Shop> shop) {
        //表头
        System.out.println("商品名称\t商品价格\t商品库存");
        //遍历集合,创建对象,输出集合数据
        Shop shop1 = new Shop();
        for (int i = 0; i < shop.size(); i++) {
            shop1 = shop.get(i);

            //商品信息
            System.out.println(shop1.getShopname() + "\t" + shop1.getShopprice() + "\t" + shop1.getShopstock());
        }
    }

    //创建方法,实现商品修改
    public static void modifyShop(ArrayList<Shop> shop) {
        lo:
        while (true) {
            //提示,然后键盘录入
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要修改的商品名");
            String name = sc.nextLine();
            //遍历集合,如果没有相应的名字,报错,并重新录入
            //遍历集合,进行修改
            for (int i = 0; i < shop.size(); i++) {
                //创建对象,接收集合中的对应信息
                if (shop.get(i).getShopname().equals(name)) {
                    System.out.println("请输入要修改的商品价格");
                    shop.get(i).setShopprice(sc.nextInt());
                    System.out.println("请输入要修改的商品库存");
                    shop.get(i).setShopstock(sc.nextInt());
                    //给出提示,修改成功
                    System.out.println("修改成功");
                    break;
                } else if (i == shop.size() - 1) {
                    System.out.println("没有该商品,请重新录入!");
                    continue lo;
                }
            }
            break;
        }

    }

    //创建方法,实现商品删除
    public static void deleteShop(ArrayList<Shop> shop) {
        //创建死循环
        ol:
        while (true) {
            //提示,然后键盘录入
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要删除的商品名");
            String name = sc.nextLine();

            //遍历集合,进行修改
            for (int i = 0; i < shop.size(); i++) {
                //将集合获取到的商品名与键盘录入的进行对比
                if (shop.get(i).getShopname().equals(name)) {
                    //相等则删除商品信息
                    shop.remove(i);
                    //给出提示,删除成功
                    System.out.println("删除成功");
                    break;
                } else if (i == shop.size() - 1) {
                    System.out.println("没有该商品,请重新录入!");
                    continue ol;//跳转到循环开始,重新录入
                }
            }
            break;
        }
    }

    //创建方法,实现商品购买页面
    public static void buyShop(ArrayList<Shop> shop) {
        ol:
        while (true) {
            //购买页面提示
            System.out.println("-------购买商品页面-------");
            System.out.println("1.查看商品");
            System.out.println("2.购买商品");
            System.out.println("3.退出登录");
            //键盘输入
            String choice = new Scanner(System.in).nextLine();
            switch (choice) {
                case "1":
                    //调用方法,查看商品
                    findShop(shop);
                    break;
                case "2":
                    buy(shop);
                    //调用方法,购买商品
                    break;
                case "3":
                    //退出系统
                    System.out.println("正在退出,欢迎下次光临");
                    break ol;
                default:
                    //其他情况
                    System.out.println("您的输入不和要求,请重新输入");
                    break;
            }
        }
    }

    //创建方法,实现购买商品
    public static void buy(ArrayList<Shop> shop) {

        //提示,并键盘录入对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入商品名称");
        String shopname = sc.nextLine();
        //遍历集合,对集合中商品数量进行修改
        ol:
        //创建循环,实现重新输入
        while (true) {
            for (int i = 0; i < shop.size(); i++) {
                //如果商品存在,输入商品数量
                if (shop.get(i).getShopname().equals(shopname)) {
                    System.out.println("请输入商品您要买的商品数量");
                    int shopnumber = sc.nextInt();
                    //判断库存够不够
                    if (shop.get(i).getShopstock() >= shopnumber) {
                        //如果库存足够,则计算总价,并给出剩余库存
                        //计算剩余库存数量,并更新
                        shop.get(i).setShopstock(shop.get(i).getShopstock() - shopnumber);
                        //定义变量result,计算支付总价
                        int result = shopnumber * shop.get(i).getShopprice();
                        //输出购买数量及总支出
                        System.out.println("您已购买 " + shop.get(i).getShopname() + shopnumber + "件,共支付:" + result + "元,谢谢惠顾,欢迎下次光临");
                        break ol;//购买结束,退出循环
                    } else {
                        System.out.println("该商品库存不足,请重新输入");
                        break;
                    }
                } else if (i == shop.size() - 1) {
                    System.out.println("商品不存在!请重新录入!");
                    break;//跳转到循环开始,重新录入
                }
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值