Java做一个简单的宠物商店,实现添加,购买,结算等功能

如今即将上大二的我,回头做了一下大一上半学期的宠物商店的一个小项目,那时候刚接触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();
    }

}
  • 11
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现某商店宠物店主人领养宠物,可以使用面向对象的设计方法来设计一个Java程序。首先,我们可以创建一个Pet类作为宠物的基类,包含宠物的属性和行为。属性可以包括宠物的类型、年龄、品种等信息,行为可以包括宠物的喂养、玩耍等操作。 接下来,可以创建一个Owner类作为宠物店主人的类,该类可以包含领养宠物的方法。在Owner类中,我们可以定义一个领养宠物的方法,该方法可以接受一个Pet对象作为参数,并将该对象添加到宠物店主人的宠物列表中。 然后,可以创建一个PetShop类作为宠物店的类,该类可以包含宠物店的属性和方法。在PetShop类中,我们可以定义一个宠物店的宠物列表,用于存储所有的宠物。可以添加一个获取所有宠物的方法,该方法将返回宠物店中所有的宠物列表。同时,也可以添加一个根据宠物类型获取宠物列表的方法,该方法可以根据用户输入的宠物类型来返回相应类型的宠物列表。 最后,在主程序中,我们可以创建几个不同类型的宠物对象,并将它们添加到宠物店中。然后,我们可以创建一个宠物店主人对象,调用领养宠物的方法,将宠物店中的宠物领养到宠物店主人的宠物列表中。 总的来说,通过面向对象的设计方法,我们可以创建Pet类、Owner类和PetShop类,实现某商店宠物店主人领养宠物的功能。这样的设计使代码结构清晰,易于扩展和维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值