/*1.定义计算机类(Computer),要求有如下属性:
a)brand(品牌)
b)model(型号)
c)screenSize(屏幕大小)
d)color(颜色)
e)price(价格)*/
/*2.定义一个List集合,存储以下”Computer”信息:
a)联想 X240 12 黑 7500
b)华硕 T200 14.5 银 5600
c)华硕 XT230 15 黑 7700
d)惠普 P330 15 白 6600
3.要求:遍历集合,查找出所有”黑色”,并且”价格高于7000”的电脑,
将查找结果存储到一个新List中,并遍历打印.*/
public class Test {
public static void main(String[] args) {
List<computer> a = new ArrayList<>();
a.add(new computer("联想","X240",12,"黑",7500));
a.add(new computer("华硕","X200",14.5,"银",5600));
a.add(new computer("华硕","X230",15,"黑",7700));
a.add(new computer("惠普","P330",15,"白",6600));
List<computer> na= new ArrayList<>();
for (computer i : a) {
if(i.getColour().equals("黑") && i.getPrice()>7000){
na.add(i);
}
}
System.out.println("新集合:");
for (computer c : na) {
System.out.println(c);
}
}
}
public class computer {
private String brand;
private String model;
private double size;
private String colour;
private double price;
public computer() {
super();
// TODO Auto-generated constructor stub
}
public computer(String brand, String model, double size, String colour,
double price) {
super();
this.brand = brand;
this.model = model;
this.size = size;
this.colour = colour;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "computer [brand=" + brand + ", model=" + model + ", size="
+ size + ", colour=" + colour + ", price=" + price + "]";
}
} |
|