数组的作业,尚学堂高琪老师的作业

1. 定义一个类表示上表中的商品。
2. 创建5 个对象存储上面表的信息。
3. 创建一个数组,存储这5 个对象。
4. 再创建一个方法,遍历(toString())这个数组,打印表的信息。
5. 创建一个方法:查询最终购买价,大于指定数字的所有商品。

ID 名称 型号 价格 折扣
1 百战牌鼠标 BZ_001 99.21 0.9
2 键盘侠玩偶 WO_102 403 0.7
3 实战java程序设计 BK_001 89 0.8
4 高棋牌西装 GQ_XF_12 700 0.5
5 大米牌手机 DM_PH_13 900 0.3

作为一个初学者,学的进度跨度比较大,所以导致了方法,数组等混淆了。经过一段时间的思考,才把这个作业给完成了!

public class objact1 {
    public static void main(String[] args) {
        emp emp0 = new emp(1,"百战鼠标平","BZ_001",99.21,0.90);
        emp emp1 = new emp(2,"键盘侠玩偶","WO_102",403.00,0.70);
        emp emp2 = new emp(3,"实战java程序","BK_001",89.00,0.80);
        emp emp3 = new emp(4,"高棋牌西装","GQ_XF_12",700.00,0.50);
        emp emp4 = new emp(5,"大米牌手机","DM_PH_13",900.00,0.30);
        emp[] emps ={emp0,emp1,emp2,emp3,emp4};
        for(int i = 0;i<emps.length;i++){
                System.out.println(emps[i]);

        }
        shuru(emps);
    }
    
    public static void shuru(emp[] emps ){
        System.out.println("输入挑选商品的价格");
        Scanner scanner = new Scanner(System.in);
        double C_price = scanner.nextDouble();
        System.out.println("____大于"+C_price+"的商品有____");
        for (emp EMP:emps){
            if (C_price < EMP.getPrice()){
                System.out.println(EMP.getName());
            }
        }

    }
}
class emp{
    
    emp(){
    }
    
    public emp(int ID, String name, String model, double price, double discount) {
        this.ID = ID;
        this.name = name;
        this.model = model;
        this.price = price;
        this.discount = discount;
    }
    
    private int ID;
    private String name;
    private String model;
    private double price;
    private double discount;
    @Override
    public String toString() {
        return getID()+"\t"+getName()+"\t"+getDiscount()+"\t"+getPrice()+"\t"+getModel();
    }

    public int getID() {
        return ID;
    }
    
    public void setID(int ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

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

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public double getPrice() {
        return price;
    }

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

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }
}

因为这个是面向对象,数组,for循环,导入包等。如果有和我一样的初学者,欢迎探讨。

猜你喜欢

转载自blog.csdn.net/chenjiaxiaohei/article/details/121904054