代码实例
程序员文章站
2022-03-27 12:22:52
...
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.创建一个方法,遍历数组
5.创建一个方法,查询最终价格,判断是否大于输入价格,大于则打印。
package 多维数组;
import java.util.Date;
import java.util.Scanner;
public class Task {
public static void main(String[] args) {
//创建了5个表格对象数据
Shop s0 = new Shop(1, "百战牌鼠标", "BZ_001", 99.21, 0.9);
Shop s1 = new Shop(2, "键盘侠玩偶", "wo_102", 403.00, 0.7);
Shop s2 = new Shop(3, "实战java程序设计", "bk_001", 89.00, 0.8);
Shop s3 = new Shop(4, "高企牌西装", "gq_xf_12", 700.00, 0.5);
Shop s4 = new Shop(5, "大米牌手机", "DM_ph_13", 900.00, .03);
//创建ss数组,空间5个
Shop[] ss = new Shop[5];
//给数组赋值
ss[0] = s0;
ss[1] = s1;
ss[2] = s2;
ss[3] = s3;
ss[4] = s4;
//遍历数组,用toString打印
for (int i = 0; i < ss.length; i++) {
System.out.println(ss[i]);
}
//遍历数组,用get方法打印
for (int i = 0; i < ss.length; i++) {
System.out.println(ss[i].getId() + ss[i].getName() + ss[i].getTyp() + ss[i].getPrice() + ss[i].getDis());
}
//创建键盘输入对象
Scanner se = new Scanner(System.in);
//无限循环查询价格
for (; ; ) {
System.out.println("请输入最低价格:");
//创建输入最低价格比较
double di = se.nextDouble();
//判断是否没有的属性判断
int tt = 0;
//每个价格和最低价格比较,如果大于则打印出来,如果小于则给tt赋值1
for (int i = 0; i < ss.length; i++) {
double dd = ss[i].getPrice() * ss[i].getDis();
if (dd > di) {
System.out.println(ss[i]);
}else tt=1;
}
//判断tt值
if(tt>0) System.out.println("没有");
//判断是否进入下一次查询还是退出
System.out.println("请输入任意字符进入下一次查询;或者输入exit退出程序");
se.nextLine(); //防止nextLine无效化
String exit = se.nextLine();
if (exit.equals("exit")) {
break;
}
}
}
}
class Shop implements Comparable<Shop>{
private int id;
private String name;
private String typ;
private Double price;
private Double dis;
//重写比较方法,没有用上
@Override
public int compareTo(Shop o) {
return 0;
}
//重写toString方法
@Override
public String toString() {
return "Shop{" +
"id=" + id +
", 名称='" + name + '\'' +
", 型号='" + typ + '\'' +
", 价格=" + price +
", 折扣=" + dis +
'}';
}
//构造器创建
public Shop(int id, String name, String typ, Double price, Double dis) {
this.id = id;
this.name = name;
this.typ = typ;
this.price = price;
this.dis = dis;
}
public Shop() {
}
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 getTyp() {
return typ;
}
public void setTyp(String typ) {
this.typ = typ;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getDis() {
return dis;
}
public void setDis(double dis) {
this.dis = dis;
}
}
上一篇: jquery简单实现外部链接用新窗口打开的方法_jquery
下一篇: ps怎么调节渐变色背景