public class Computer {
private String brand;
private double price;
private String size;
public Computer() {
}
public Computer(String brand, double price, String size) {
this.brand = brand;
this.price = price;
this.size = size;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
@Override
public String toString() {
return "Computer{" +
"brand='" + brand + '\'' +
", price=" + price +
", size='" + size + '\'' +
'}';
}
}
public class Test {
//主方法
public static void main(String[] args) {
//创建一个集合
ArrayList<Computer> list = new ArrayList<Computer>();
Computer c1= new Computer("Apple", 11980.0, "13寸");
Computer c2= new Computer("Dell", 5200.0, "15.5寸");
Computer c3= new Computer("Acer", 4999.0, "15寸");
//将对象添加到集合中
list.add(c1);
list.add(c2);
list.add(c3);
for (int i = 0; i <list.size() ; i++) {
Computer computer = list.get(i);
if(computer.getPrice()>5000){
System.out.println(computer.getBrand()+","+computer.getPrice()+","+computer.getSize());
}
}
}
}
|
|