Java学习list集合练习二

题目

设计电商类其中包含商品类商品类属性名称价格介绍厂家生产地址),继承类有食品类电气类服装类玩具类

食品类属性有效时长重量

电气类属性电压功率重量

服装类属性材质面料成分

玩具类属性材质分类适用年龄

 

要求

1、设计商品类封装商品信息

2、设计继承类继承商品类封装继承商品信息

3、设计商城商城中有一个各类商品的集合客户可以看到所有商品也可以根据不同继承类查询商品每类商品不少以五种

4、查询商城中价格超过100元的商品

5、查询生产地是“北京”的商品

6、根据商品价格给商城排序价格从高到低

7、设计购物车购物车包括商品名称和商品数量输入商品名称显示商品信息输入数量加入购物车完成添加后输入“结账”计算所有商品价格

8、设计抽奖功能查询所有商品生成随机数随机数范围0到商品集合长度生成随机数后通过下标获取商品显示商品信息

 

解答

package homework;

public class DQ extends ShangP {
    private int V;
    private int P;
    private double weight;
    public DQ(String name, double price, String introduce, String producer, String address, int v, int p,
            double weight) {
        super(name, price, introduce, producer, address);
        V = v;
        P = p;
        this.weight = weight;
    }
    public int getV() {
package homework;

public class FZ extends ShangP {
    private String quality;
    private String inner;
    private String composition;
    public FZ(String name, double price, String introduce, String producer, String address, String quality,
            String inner, String composition) {
        super(name, price, introduce, producer, address);
        this.quality = quality;
        this.inner = inner;
        this.composition = composition;
    }
    /**
     * @return the quality
     */
    public String getQuality() {
        return quality;
    }
    /**
     * @param quality the quality to set
     */
    public void setQuality(String quality) {
        this.quality = quality;
    }
    /**
     * @return the inner
     */
    public String getInner() {
        return inner;
    }
    /**
     * @param inner the inner to set
     */
    public void setInner(String inner) {
        this.inner = inner;
    }
    /**
     * @return the composition
     */
    public String getComposition() {
        return composition;
    }
    /**
     * @param composition the composition to set
     */
    public void setComposition(String composition) {
        this.composition = composition;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "FZ [name=" + super.getName() + ", price=" + super.getPrice() + ", introduce=" + super.getIntroduce() + ", producer=" + super.getProducer()
        + ", address=" + super.getAddress() + ",quality=" + quality + ", inner=" + inner + ", composition=" + composition + "]";
    }
    
    
    
}
package homework;

public class SW extends ShangP {
    private double hour;
    private double weight;
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "SW [name=" + super.getName() + ", price=" + super.getPrice() + ", introduce=" + super.getIntroduce() + ", producer=" + super.getProducer()
                + ", address=" + super.getAddress() + ",hour=" + hour + ", weight=" + weight + "]";
    }
    public SW(String name, double price, String introduce, String producer, String address, double hour,
            double weight) {
        super(name, price, introduce, producer, address);
        this.hour = hour;
        this.weight = weight;
    }
    public double getHour() {
        return hour;
    }
    public void setHour(double hour) {
        this.hour = hour;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    
}
package homework;

public class WJ extends ShangP {
    private String quality;
    private String kind;
    private int age;
    public WJ(String name, double price, String introduce, String producer, String address, String quality, String kind,
            int age) {
        super(name, price, introduce, producer, address);
        this.quality = quality;
        this.kind = kind;
        this.age = age;
    }
    /**
     * @return the quality
     */
    public String getQuality() {
        return quality;
    }
    /**
     * @param quality the quality to set
     */
    public void setQuality(String quality) {
        this.quality = quality;
    }
    /**
     * @return the kind
     */
    public String getKind() {
        return kind;
    }
    /**
     * @param kind the kind to set
     */
    public void setKind(String kind) {
        this.kind = kind;
    }
    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "WJ [name=" + super.getName() + ", price=" + super.getPrice() + ", introduce=" + super.getIntroduce() + ", producer=" + super.getProducer()
        + ", address=" + super.getAddress() + ",quality=" + quality + ", kind=" + kind + ", age=" + age + "]";
    }
    
    
}
package homework;

public class ShangP implements Comparable<ShangP>{
    
    private String name;
    private double price;
    private String introduce;
    private String producer;
    private String address;
    public ShangP(String name, double price, String introduce, String producer, String address) {
        this.name = name;
        this.price = price;
        this.introduce = introduce;
        this.producer = producer;
        this.address = address;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getIntroduce() {
        return introduce;
    }
    public void setIntroduce(String introduce) {
        this.introduce = introduce;
    }
    public String getProducer() {
        return producer;
    }
    public void setProducer(String producer) {
        this.producer = producer;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "ShangP [name=" + name + ", price=" + price + ", introduce=" + introduce + ", producer=" + producer
                + ", address=" + address + "]";
    }
    @Override
    public int compareTo(ShangP o) {
        if(this.getPrice()>o.getPrice())
            return -1;
        else if(this.getPrice()<o.getPrice())
            return 1;
        else
            return 0;
    }
    
    
}
package homework;

public class gouwuche {
    private String name;
    private int num;
    private double price;
    public gouwuche(String name, int num, double price) {
        super();
        this.name = name;
        this.num = num;
        this.price = price;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the num
     */
    public int getNum() {
        return num;
    }
    /**
     * @param num the num to set
     */
    public void setNum(int num) {
        this.num = num;
    }
    /**
     * @return the price
     */
    public double getPrice() {
        return price;
    }
    /**
     * @param price the price to set
     */
    public void setPrice(double price) {
        this.price = price;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "gouwuche [name=" + name + ", num=" + num + ", price=" + price + "]";
    }
    
    
}
package homework;

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

public class ShangCheng {

    public static void main(String[] args) {

        ArrayList<ShangP> list = liebiao();
        for (ShangP s : list) {
            System.out.println(s.toString());
        }
        System.out.println("**价格高于100的有**");
        for (ShangP n : list) {
            if (n.getPrice() > 100) {
                System.out.println(n);
            }
        }
        System.out.println("**产地是北京的有**");
        for (ShangP n : list) {
            if (n.getAddress().equals("北京")) {
                System.out.println(n);
            }
        }
        System.out.println("**排序**");
        Collections.sort(list);
        for (ShangP n : list) {
            System.out.println(n.toString());
        }

        System.out.println("**请购物**");

        
        double sum = 0;
//        ArrayList<gouwuche> str=new ArrayList<gouwuche>();
//        while (true) {
//            System.out.print("请输入购买物品");
//            String name = new Scanner(System.in).next();
//            System.out.print("请输入购买数量");
//            int num = new Scanner(System.in).nextInt();
//            for (ShangP l : list) {
//                if (l.getName().equals(name)) {
//                    // System.out.println(l);
//                    // sum=l.getPrice();
//                    // sum*=num;
//                    gouwuche gg = new gouwuche(name, num, l.getPrice());
//                    str.add(gg);
//                    System.out.println("是否继续购买(是/结账)");
//                    String s = new Scanner(System.in).next();
//                    if (s.equals("结账")) {
//                        for (gouwuche st:str) {
//                            sum+=st.getNum()*st.getPrice();
//                        }
//
//                        System.out.println("您需要支付" + sum);
//                    }
//                    break;
//                }
//            }
//        }
        
        chou();
    }



    public static void chou() {
        ArrayList<ShangP> list = liebiao();
        int a = (int) (Math.random()*list.size());
        
        System.out.println("抽到了"+list.get(a));
        System.out.println();

    }

    public static ArrayList<ShangP> liebiao() {
        ArrayList<ShangP> list = new ArrayList<ShangP>();
        SW a1 = new SW("食物1", 12, "好", "大龙厂", "浙江", 9, 90);
        SW a2 = new SW("食物2", 12, "好", "大龙厂", "浙江", 9, 90);
        SW a3 = new SW("食物3", 12, "好", "大龙厂", "浙江", 9, 90);
        SW a4 = new SW("食物4", 12, "好", "大龙厂", "浙江", 9, 90);
        SW a5 = new SW("食物5", 12, "好", "大龙厂", "浙江", 9, 90);
        DQ b1 = new DQ("空调1", 10000, "好", "美的", "黑龙江", 220, 2500, 300);
        DQ b2 = new DQ("空调2", 10000, "好", "美的", "黑龙江", 220, 2500, 300);
        DQ b3 = new DQ("空调3", 10000, "好", "美的", "黑龙江", 220, 2500, 300);
        DQ b4 = new DQ("空调4", 10000, "好", "美的", "黑龙江", 220, 2500, 300);
        DQ b5 = new DQ("空调5", 10000, "好", "美的", "黑龙江", 220, 2500, 300);
        FZ c1 = new FZ("巴黎世家1", 1000, "优", "巴黎世家工厂", "广州", "涤纶", "棉麻", "布,水,线");
        FZ c2 = new FZ("巴黎世家2", 1000, "优", "巴黎世家工厂", "广州", "涤纶", "棉麻", "布,水,线");
        FZ c3 = new FZ("巴黎世家3", 1000, "优", "巴黎世家工厂", "广州", "涤纶", "棉麻", "布,水,线");
        FZ c4 = new FZ("巴黎世家4", 1000, "优", "巴黎世家工厂", "广州", "涤纶", "棉麻", "布,水,线");
        FZ c5 = new FZ("巴黎世家5", 1000, "优", "巴黎世家工厂", "北京", "涤纶", "棉麻", "布,水,线");
        WJ d1 = new WJ("泡泡机1", 200, "差", "江南皮革厂", "江苏", "皮革", "益智玩具", 3);
        WJ d2 = new WJ("泡泡机2", 200, "差", "江南皮革厂", "北京", "皮革", "益智玩具", 3);
        WJ d3 = new WJ("泡泡机3", 200, "差", "江南皮革厂", "江苏", "皮革", "益智玩具", 3);
        WJ d4 = new WJ("泡泡机4", 200, "差", "江南皮革厂", "江苏", "皮革", "益智玩具", 3);
        WJ d5 = new WJ("泡泡机5", 200, "差", "江南皮革厂", "江苏", "皮革", "益智玩具", 3);
        list.add(a1);
        list.add(a2);
        list.add(a3);
        list.add(a4);
        list.add(a5);
        list.add(b1);
        list.add(b2);
        list.add(b3);
        list.add(b4);
        list.add(b5);
        list.add(c1);
        list.add(c2);
        list.add(c3);
        list.add(c4);
        list.add(c5);
        list.add(d1);
        list.add(d2);
        list.add(d3);
        list.add(d4);
        list.add(d5);
        return list;
    }
}
    }
    public void setV(int v) {
        V = v;
    }
    public int getP() {
        return P;
    }
    public void setP(int p) {
        P = p;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    @Override
    public String toString() {
        return "DQ [name=" + super.getName() + ", price=" + super.getPrice() + ", introduce=" + super.getIntroduce() + ", producer=" + super.getProducer()
        + ", address=" + super.getAddress() + ",V=" + V + ", P=" + P + ", weight=" + weight + "]";
    }
    
}

 

posted @ 2021-08-19 17:07  热心市民陆女士  阅读(36)  评论(0编辑  收藏  举报