百战作业35:多维数组应用

  1. 如下表是一张商品信息表:

ID

名称

型号

价格

折扣

1

百战牌鼠标

BZ_001

99.21

0.9

2

键盘侠玩偶

WO_102

403.00

0.7

3

实战java程序设计

BK_001

89.00

0.8

4

高淇牌西装

GQ_XF_12

700.00

0.5

5

大米牌手机

DM_PH_13

900.00

0.3

完成功能:

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

代码实现:

package Assign35;

public class TestArray {
    public static void main(String[] args) {
        GoodsInfo good1 = new GoodsInfo(1,"百战牌鼠标","BZ_001",99.21,0.9);
        GoodsInfo good2 = new GoodsInfo(2,"键盘侠玩偶","WO_102",403.00,0.7);
        GoodsInfo good3 = new GoodsInfo(3,"实战java程序设计","BK_001",89.00,0.9);
        GoodsInfo good4 = new GoodsInfo(4,"高淇牌西装","GQ_XF_12",700.00,0.5);
        GoodsInfo good5 = new GoodsInfo(5,"大米牌手机","DM_PH_13",900.00,0.3);

       GoodsInfo[] goodsinfo = {good1,good2,good3,good4,good5};

       selectByPrice(goodsinfo,100);
       printInfos(goodsinfo);
       // for(GoodsInfo temp:goodsinfo){
        //    System.out.println(temp);
    }
        public static void selectByPrice(GoodsInfo[] goodsinfo,double price){
            System.out.println("=====商品查询(按价格)=======");
            System.out.println("最终购买价大于"+price+"的商品如下:");
            for(GoodsInfo info:goodsinfo) {
                double resultPrice = info.getPrice()*info.getDiscount();
                if(resultPrice>price){
                    System.out.println(info.getName()+",最终购买价:"+resultPrice);
                }
            }
        }
        public static void printInfos(GoodsInfo[] goodsinfo){
            System.out.println("ID\t名称\t\t\t\t型号\t\t价格\t\t折扣");
            for(GoodsInfo gi:goodsinfo) {
                System.out.println(gi.getId()+"\t"+
                        gi.getName()+"\t\t"+
                        gi.getType()+"\t"+
                        gi.getPrice()+"\t"+
                        gi.getDiscount());
            }
        }
}
class GoodsInfo{
    private int id;
    private String name;
    private String type;
    private double price;
    private double discount;

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

    public GoodsInfo(int id, String name, String type, double price, double discount) {
        this.id = id;
        this.name = name;
        this.type = type;
        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 getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    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;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值