集合(设计商城)

 父类:商品类

package Test5;
//3、设计商城,商城中有一个各类商品的集合,客户可以看到所有商品,也可以根据不同继承类查询商品,每类商品不少以五种
//4、查询商城中价格超过100元的商品
//5、查询生产地是“北京”的商品
//6、根据商品价格给商城排序,价格从高到低
//7、设计购物车,购物车包括商品名称和商品数量,输入商品名称,显示商品信息,输入数量,加入购物车,完成添加后,输入“结账”,计算所有商品价格。
//8、设计抽奖功能,查询所有商品,生成随机数,随机数范围0到商品集合长度,生成随机数后,通过下标获取商品,显示商品信息
public class commodity implements Comparable<commodity> {
    //商品类属性(名称、价格、介绍、厂家、生产地址)
    private String name;
    private double money;
    private String introduce;
    public String getName() {
        return name;
    }

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

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public String getIntroduce() {
        return introduce;
    }

    public void setIntroduce(String introduce) {
        this.introduce = introduce;
    }

    public String getHome() {
        return Home;
    }

    public void setHome(String home) {
        Home = home;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    private String Home;

    public commodity(String name, double money, String introduce, String home, String adress) {
        this.name = name;
        this.money = money;
        this.introduce = introduce;
        Home = home;
        this.adress = adress;
    }

    private String adress;

    @Override
    public String toString() {
        return "commodity{" +
                "name='" + name + '\'' +
                ", money=" + money +
                ", introduce='" + introduce + '\'' +
                ", Home='" + Home + '\'' +
                ", adress='" + adress + '\'' +
                '}';
    }

    @Override
    public int compareTo(commodity o) {
        if(this.money>o.getMoney()){
            return 1;
        }else if(this.money<o.getMoney()){
            return -1;
        }else{
            return 0;
        }
    }
}
package Test5;
//设计电商类继承类有食品类、电气类、服装类、玩具类。
//食品类属性:有效时长、重量

//食品类
public class food extends commodity{
    public double getTime() {
        return time;
    }

    public void setTime(double time) {
        this.time = time;
    }

    public double getWight() {
        return wight;
    }

    public void setWight(double wight) {
        this.wight = wight;
    }

    @Override
    public String toString() {
        return  super.toString()+ "food{" +
                "time=" + time +
                ", wight=" + wight +
                '}';
    }

    private double time;

    public food(String name, double money, String introduce, String home, String adress, double time, double wight) {
        super(name, money, introduce, home, adress);
        this.time = time;
        this.wight = wight;
    }

    private double wight;
}

 

package Test5;
//玩具类属性:材质、分类、适用年龄
public class toy extends commodity{
    private String caizhi;

    public String getCaizhi() {
        return caizhi;
    }

    public void setCaizhi(String caizhi) {
        this.caizhi = caizhi;
    }

    public String getFenlei() {
        return fenlei;
    }

    public void setFenlei(String fenlei) {
        this.fenlei = fenlei;
    }

    public int getAge() {
        return age;
    }

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

    private String fenlei;

    public toy(String name, double money, String introduce, String home, String adress, String caizhi, String fenlei, int age) {
        super(name, money, introduce, home, adress);
        this.caizhi = caizhi;
        this.fenlei = fenlei;
        this.age = age;
    }

    @Override
    public String toString() {
        return super.toString()+"toy{" +
                "caizhi='" + caizhi + '\'' +
                ", fenlei='" + fenlei + '\'' +
                ", age=" + age +
                '}';
    }

    private int age;//适用年龄
}

 

 

package Test5;
//服装类属性:材质、面料、成分
public class cloth extends commodity{
    public cloth(String name, double money, String introduce, String home, String adress, String caizhi, String mianliao, String chengfen) {
        super(name, money, introduce, home, adress);
        this.caizhi = caizhi;
        this.mianliao = mianliao;
        this.chengfen = chengfen;
    }

    public String getCaizhi() {
        return caizhi;
    }

    public void setCaizhi(String caizhi) {
        this.caizhi = caizhi;
    }

    public String getMianliao() {
        return mianliao;
    }

    public void setMianliao(String mianliao) {
        this.mianliao = mianliao;
    }

    public String getChengfen() {
        return chengfen;
    }

    @Override
    public String toString() {
        return super.toString()+"cloth{" +
                "caizhsi='" + caizhi + '\'' +
                ", mianliao='" + mianliao + '\'' +
                ", chengfen='" + chengfen + '\'' +
                '}';
    }

    public void setChengfen(String chengfen) {
        this.chengfen = chengfen;
    }

    private String caizhi;
    private String mianliao;
    private String chengfen;
}

 

 

package Test5;
//电气类属性:电压、功率、重量
public class electrical extends commodity{
    private double voltage;//电压

    public double getVoltage() {
        return voltage;
    }

    public void setVoltage(double voltage) {
        this.voltage = voltage;
    }

    public double getGonglv() {
        return gonglv;
    }

    public void setGonglv(double gonglv) {
        this.gonglv = gonglv;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    private double  gonglv;//功率

    public electrical(String name, double money, String introduce, String home, String adress, double voltage, double gonglv, double width) {
        super(name, money, introduce, home, adress);
        this.voltage = voltage;
        this.gonglv = gonglv;
        this.width = width;
    }

    private  double  width;

    @Override
    public String toString() {
        return super.toString()+"electrical{" +
                "voltage=" + voltage +
                ", gonglv=" + gonglv +
                ", width=" + width +
                '}';
    }
}

 测试类:

package Test5;

import java.util.*;

public class shopping {
    public static void main(String[] args) {
        commodity f1 = new food("火腿", 5, "鸡肉味", "北京", "超市A", 24, 5);
        commodity f2 = new food("薯片", 8, "烧烤味", "长春", "超市B", 56, 10);
        commodity f3 = new food("哇哈哈", 2, "草莓味", "西安", "超市C", 72, 20);
        commodity f4 = new food("面包", 10, "肉松味", "哈尔滨", "超市D", 14, 25);
        commodity f5 = new food("饮料", 2, "农夫山泉", "重庆", "超市E", 2, 20);
        commodity t1 = new toy("泰迪熊", 102, "记忆棉", "北京", "玩具店A", "棉花", "玩偶", 8);
        commodity t2 = new toy("玩具枪", 79, "金属", "上海", "玩具店B", "棉花", "男童玩具", 13);
        commodity t3 = new toy("拼图", 15, "木材", "重庆", "玩具店C", "原木", "益智游戏", 6);
        commodity t4 = new toy("积木", 20, "木材", "辽宁", "玩具店D", "原木", "益智游戏", 8);
        commodity t5 = new toy("解压包子", 3, "软质", "郑州", "玩具店E", "棉花", "解压玩具", 19);
        commodity c1 = new cloth("棉花A", 180, "A", "北京", "衣服店A", "棉A", "丝光棉A", "A");
        commodity c2 = new cloth("棉花B", 159, "B", "广州", "衣服店B", "棉B", "丝光棉B", "B");
        commodity c3 = new cloth("棉花C", 132, "C", "杭州", "衣服店C", "棉C", "丝光棉C", "C");
        commodity c4 = new cloth("棉花D", 77, "D", "深圳", "衣服店D", "棉D", "丝光棉D", "D");
        commodity c5 = new cloth("棉花E", 230, "E", "上海", "衣服店E", "棉E", "丝光棉E", "E");
        commodity e1 = new electrical("冰箱", 1900, "保存食物", "北京", "家具店A", 2000, 300, 100);
        commodity e2 = new electrical("空调", 2300, "降温", "上海", "家具店B", 1800, 400, 200);
        commodity e3 = new electrical("微波炉", 500, "加热食物", "深圳", "家具店C", 500, 100, 80);
        commodity e4 = new electrical("烤箱", 300, "加热食物", "重庆", "家具店D", 700, 200, 90);
        commodity e5 = new electrical("扫地机器人", 19900, "清洁", "深圳", "家具店E", 1000, 600, 60);
        List<commodity> co = new ArrayList<commodity>();
        co.add(f1);
        co.add(f2);
        co.add(f3);
        co.add(f4);
        co.add(f5);
        co.add(t1);
        co.add(t2);
        co.add(t3);
        co.add(t4);
        co.add(t5);
        co.add(c1);
        co.add(c2);
        co.add(c3);
        co.add(c4);
        co.add(c5);
        co.add(e1);
        co.add(e2);
        co.add(e3);
        co.add(e4);
        co.add(e5);
        System.out.println("*******************************");
        System.out.println("该商城的所有商品有:");
        for(commodity c:co){
            System.out.println(c);
        }
        System.out.println("*******************************");

        System.out.println("生产地是北京的商品有:");
        for(commodity c:co){
            if("北京".equals(c.getHome())){
                System.out.println(c);
            }
        }
        System.out.println("*******************************");
        System.out.println("商城中价格超过100元的商品有:");
        for(commodity c:co){
            if(c.getMoney()>100){
                System.out.println(c.getName());
            }
        }
        //6、根据商品价格给商城排序,价格从高到低
        System.out.println("*******************************");
        System.out.println("根据商品价格从高到低排序:");
        Collections.sort(co);
        for(commodity c:co){
            System.out.println(c);
        }
        //7、设计购物车,购物车包括商品名称和商品数量,输入商品名称,显示商品信息,输入数量,加入购物车,完成添加后,输入“结账”,计算所有商品价格。
        System.out.println("请输入商品名称:");
        String sc=new Scanner(System.in).next();
        int sum=0;
        int k=0;
        double v=0;
        for (int i = 0; i <co.size() ; i++) {
            if(sc.equals(co.get(i).getName())){
                System.out.println(co.get(i));
                v=co.get(i).getMoney();
                System.out.println("请输入商品数量:");
                k=new Scanner(System.in).nextInt();
                System.out.println("加入购物车!");
            }
        }
        System.out.println("请输入结账或者退出购买!");
        String s=new Scanner(System.in).next();
        if("结账".equals(s)){
            System.out.println("商品价格为:"+k*v);
        }
        //8、设计抽奖功能,查询所有商品,生成随机数,随机数范围0到商品集合长度,生成随机数后,通过下标获取商品,显示商品信息
        System.out.println("抽奖环节:");
        Random random=new Random();
        int num=random.nextInt(co.size());
        boolean isok=false;
        for (int i = 0; i <co.size() ; i++) {
            if(co.get(i)==co.get(num)){
                isok=true;
            }
        }
        if(isok==true){
            System.out.println(co);
        }else{
            System.out.println("抽奖失败!");
        }
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值