如今即将上大二的我,回头做了一下大一上半学期的宠物商店的一个小项目,那时候刚接触java就被要求做个这东西出来,当时做了真的特别久。现在做起来觉得自己进步不少,挺简单的。废话不多说,直接上代码,嘿嘿。

整个程序主要用到ArrayList集合,对ArrayList进行增删查改。

1.先写一个属性类
package com.animalShop;

public class property {
    private String name;
    private String kind;
    private int age;
    private double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public property(String name, String kind, int age, double price) {
        this.name = name;
        this.kind = kind;
        this.age = age;
        this.price = price;
    }

    @Override
    public String toString() {
        return name + "\t" + kind + "\t" + age + "\t" + price;
    }
}
2.再写一个商店类
package com.animalShop;

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

public class shop {
    //商店品种集合
    ArrayList<property> amlList = new ArrayList<>();
    //购物车集合
    ArrayList<property> stoCarList = new ArrayList<>();

    //添加宠物到商店的方法
    public void addAml() {
        System.out.println("请输入添加的宠物信息(名字->种类->年龄->价格):");
        //创建对象animal属性,键入animal
        property pty = new property(getString(), getString(), getInt(), getDouble());
        //判断是否添加到amlList集合里面
        boolean bool = amlList.add(pty);
        if (bool == true) {
            System.out.println("添加成功!");
        } else {
            System.out.println("添加失败!");
        }
    }

    //显示宠物商店宠物方法
    public void showAml() {
        boolean bool = amlList.isEmpty();
        if (bool != true) {
            System.out.println("序号" + "\t" + "名字" + "\t" + "种类" + "\t" + "年龄" + "\t" + "价格");
            //定义一个序号
            int n = 1;
            //遍历出宠物商店宠物
            for (property property : amlList) {
                System.out.println(n + "\t" + property);
                n++;
            }
            System.out.println(" ");
        } else {
            System.out.println("商店维护中,暂时没有商品哦!");
            System.out.println(" ");
        }
    }

    //购买宠物到购物车的方法
    public void buyAml() {
        boolean bool = amlList.isEmpty();
        if (bool != true) {
            System.out.print("请输入您想要购买的宠物的序号:");
            //键入购买宠物的序号
            int n = getInt();
            //防止下标越界
            if (n <= amlList.size()) {
                stoCarList.add(amlList.get((n - 1)));
                amlList.remove((n - 1));
                System.out.println("购买成功!");
                System.out.println(" ");
            } else {
                System.out.println("购买失败!");
                System.out.println("您想要的宠物不存在,请核实商店现有的宠物!");
                System.out.println(" ");
            }
        } else {
            System.out.println("商店维护中,暂时没有商品可以购买哦!");
            System.out.println(" ");
        }
    }

    //查看购物车方法
    public void showShopCar() {
        boolean bool = stoCarList.isEmpty();
        if (bool != true) {
            System.out.println("-------购物车-------");
            System.out.println("序号" + "\t" + "名字" + "\t" + "种类" + "\t" + "年龄" + "\t" + "价格");
            shopList();
            System.out.println(" ");
        } else {
            System.out.println("您还没有购买过宠物呢!");
            System.out.println(" ");
        }
    }

    //购物车结算方法
    public void totalPrice() {
        boolean bool =stoCarList.isEmpty();
        if (bool != true) {
            System.out.println("-------清单-------");
            System.out.println("序号" + "名字" + "\t" + "种类" + "\t" + "年龄" + "\t" + "价格" + "\t");
            shopList();
            //清空购物车
            stoCarList.clear();
            //判断购物车是否为空
            if (stoCarList.isEmpty()) {
                System.out.println("结算成功!");
                System.out.println(" ");
            } else {
                System.out.println("结算失败!");
                System.out.println(" ");
            }
        }else {
            System.out.println("购物车没有任何宠物哦,请先购买再结算!");
            System.out.println(" ");
        }
    }

    //删除购物车某一个宠物方法
    public void delStoreCar() {
        boolean bool = stoCarList.isEmpty();
        if (bool != true) {
            System.out.print("请输入购物车中要删除的宠物序号:");
            int n = getInt();
            //防止下标越界
            if ((n - 1) <= stoCarList.size()) {
                stoCarList.remove((n - 1));
                System.out.println("删除成功!");
                System.out.println(" ");
            } else {
                System.out.println("删除失败!");
                System.out.println("请检查购物车宠物是否存在!");
                System.out.println(" ");
            }
        } else {
            System.out.println("您还没有购买过宠物,无需删除!");
            System.out.println(" ");
        }
    }

    //删除商店某一个宠物
    public void delAmlList() {
        boolean bool = amlList.isEmpty();
        if (bool != true) {
            System.out.print("请输入商店中要删除的宠物序号:");
            int n = getInt();
            //防止下标越界
            if ((n - 1) <= amlList.size()) {
                amlList.remove((n - 1));
                System.out.println("删除成功!");
                System.out.println(" ");
            } else {
                System.out.println("删除失败!");
                System.out.println("请检查商店宠物是否存在!");
                System.out.println(" ");
            }
        } else {
            System.out.println("商城还未上架过任何宠物!");
            System.out.println(" ");
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        shop shop = new shop();
        while (true) {
            System.out.println("--------欢" + "\t" + "迎" + "\t" + "来" + "\t" + "到" +
                    "\t" + "上" + "\t" + "天" + "\t" + "的" + "\t" + "小" + "\t" + "宝" + "\t" + "贝" + "\t" + "儿" +
                    "\t" + "宠" + "\t" + "物" + "\t" + "商" + "\t" + "店--------");
            System.out.println("请输入序号(1:上架宠物 2:下架宠物 3:打开商城 4:购买 5:打开购物车 6:删除购物车宠物 7:结算 8:离开)");
            switch (sc.nextInt()) {
                case 1:
                    shop.addAml();
                    break;
                case 2:
                    shop.delAmlList();
                    break;
                case 3:
                    shop.showAml();
                    break;
                case 4:
                    shop.buyAml();
                    break;
                case 5:
                    shop.showShopCar();
                    break;
                case 6:
                    shop.delStoreCar();
                    break;
                case 7:
                    shop.totalPrice();
                    break;
                case 8:
                    System.out.println("再见!");
                    return;
            }
        }
    }


    //封装购物清单方法,提高代码的复用性
    public void shopList() {
        double total = 0;
        //序号计数器
        int n = 1;
        //遍历购物车
        for (int i = 0; i < stoCarList.size(); i++) {
            System.out.println(n + "\t" + stoCarList.get(i));
            property price = stoCarList.get(i);
            total += price.getPrice();
            n++;
        }
        System.out.println("总价:" + "\t" + "\t" + "\t" + total);
    }

    /*
     * 这里没有创建一个对象键入,是因为创建一个对象,在键入数据时,会因为对象数据类型冲突,导致InputMismatchException异常
     * */
    public static String getString() {
        Scanner a = new Scanner(System.in);
        return a.next();
    }

    public static int getInt() {
        Scanner b = new Scanner(System.in);
        return b.nextInt();
    }

    public static double getDouble() {
        Scanner c = new Scanner(System.in);
        return c.nextDouble();
    }

}