定义一个类表示上表中的商品创建5个对象存储上面表的信息创建一个数组,存储这5个对象。在创建一个方法,遍历(toString())这个数组,打印表的信息。创建一个方法:查

该博客展示了如何在Java中定义一个商品类`Commodity`,包括ID、名称、型号、价格和折扣属性。接着,创建了5个商品对象,并将它们存储在一个数组中。此外,还定义了两个方法:`selectByPrice`用于查找最终购买价大于指定价格的商品,`printComms`用于打印商品信息。博客内容涵盖了类的实例化、数组操作以及条件判断在实际编程中的应用。
摘要由CSDN通过智能技术生成
  1. 定义一个类表示上表中的商品
  2. 创建5个对象存储上面表的信息
  3. 创建一个数组,存储这5个对象。
  4. 在创建一个方法,遍历(toString())这个数组,打印表的信息。
  5. 创建一个方法:查询最终购买价,大于指定数字的所有商品
import java.util.Scanner;

class Commodity {
    private int id; //ID
    private String name; //名称
    private String model; //型号
    private int price; //价格
    private double discount; //折扣

    public Commodity(int id, String name, String modle, int price, double discount) {
        this.id = id;
        this.name = name;
        this.model = modle;
        this.price = price;
        this.discount = discount;
    }

    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 int getPrice() {
        return price;
    }

    public void setPice(int price) {
        this.price = price;
    }

    public double getDiscount() {
        return discount;
    }

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

    @Override
    public String toString() {
        return "Commodity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", model='" + model + '\'' +
                ", price=" + price +
                ", discount=" + discount +
                '}';
    }
}

public class TwoDimensionalArray {
    public static void main(String[] args) {
        Commodity comm1 = new Commodity(01, "苹果手机", "IPhone13 Pro Max", 10000, 0.9);
        Commodity comm2 = new Commodity(02, "华为手机", "P40", 8000, 0.8);
        Commodity comm3 = new Commodity(03, "苹果电脑", "iMac", 12000, 0.7);
        Commodity comm4 = new Commodity(04, "华为电脑", "L480", 13000, 0.8);
        Commodity comm5 = new Commodity(05, "小米手机", "红米60", 7000, 0.9);

        Commodity[] commoditie = {comm1, comm2, comm3, comm4, comm5};

        selectByprice(commoditie, 5000);
        printComms(commoditie);
    }

    public static void selectByprice(Commodity[] commodities, int price) {
        System.out.println("************商品查询*************");
        System.out.println("最终购买价大于" + price + "的商品如下");
        for (Commodity dity : commodities) {
            double resultPrice = dity.getPrice() * dity.getDiscount();
            if (resultPrice > price) {
                System.out.println(dity.getName() + "最终购买价" + resultPrice);
            }
        }
    }

    public static void printComms(Commodity[] commodities) {
        System.out.println("ID\t名称\t\t\t\t型号\t\t价格\t\t折扣");
        for (Commodity idity : commodities) {
            System.out.println(idity.getId() + "\t" +
                    idity.getName() + "\t\t" +
                    idity.getModel() + "\t" +
                    idity.getPrice() + "\t" +
                    idity.getDiscount());
        }
        for (Commodity ity : commodities) {
            System.out.println(ity.toString());
        }
        System.out.println("请输入你想要的价格:");
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        System.out.println("高于这个价格的有");

        for (Commodity commodity : commodities) {
            if (commodity.getPrice() * commodity.getDiscount() > a) {
                System.out.println(commodity.toString());
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

by啊军

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值