interface DomesticAppliance String getName; getPrice;class Television implements DomesticAppliance class Refrigerator implements DomesticAppliance class WashingMachine implements DomesticA
interface DomesticAppliance{ String getName(); int getPrice(); } class Television implements DomesticAppliance{ private String name; private String model; private String size; private int price;
public Television(String name, String model, String size, int price){
this.name = name;
this.model = model;
this.size = size;
this.price = price;
}
public String getName(){
return name;
}
public int getPrice(){
return price;
}
public String toString(){
return name + " " + model + " " + size + " " + price;
}
} class Refrigerator implements DomesticAppliance{ private String name; private String model; private int capacity; private String feature; private int price;
public Refrigerator(String name, String model, int capacity, String feature, int price){
this.name = name;
this.model = model;
this.capacity = capacity;
this.feature = feature;
this.price = price;
}
public String getName(){
return name;
}
public int getPrice(){
return price;
}
public String toString(){
return name + " " + model + " " + capacity + "L " + feature + " " + price;
}
} class WashingMachine implements DomesticAppliance{ private String name; private String model; private String feature; private int price;
public WashingMachine(String name, String model, String feature, int price){
this.name = name;
this.model = model;
this.feature = feature;
this.price = price;
}
public String getName(){
return name;
}
public int getPrice(){
return price;
}
public String toString(){
return name + " " + model + " " + feature + " " + price;
}
}
class Shop{
private ArrayList
public Shop(){
appliances = new ArrayList<>();
}
public void addAppliance(DomesticAppliance appliance){
appliances.add(appliance);
}
public ArrayList<DomesticAppliance> getAppliancesInPriceRange(int minPrice, int maxPrice){
ArrayList<DomesticAppliance> result = new ArrayList<>();
for(DomesticAppliance appliance : appliances){
if(appliance.getPrice() >= minPrice && appliance.getPrice() <= maxPrice){
result.add(appliance);
}
}
return result;
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
input.nextLine();
Shop shop = new Shop();
for(int i = 0; i < n; i++){
String[] line = input.nextLine().split("\s+");
String type = line[0];
String name = line[1];
switch(type){
case "电视机":
String model = line[2];
String size = line[3];
int price = Integer.parseInt(line[4]);
shop.addAppliance(new Television(name, model, size, price));
break;
case "电冰箱":
String fridgeModel = line[2];
int capacity = Integer.parseInt(fridgeModel.substring(0, fridgeModel.length()-1));
String feature = line[3];
int fridgePrice = Integer.parseInt(line[4]);
shop.addAppliance(new Refrigerator(name, fridgeModel, capacity, feature, fridgePrice));
break;
case "洗衣机":
String washerModel = line[2];
String washerFeature = line[3];
int washerPrice = Integer.parseInt(line[4]);
shop.addAppliance(new WashingMachine(name, washerModel, washerFeature, washerPrice));
break;
default:
break;
}
}
int minPrice = input.nextInt();
int maxPrice = input.nextInt();
ArrayList
原文地址: https://www.cveoy.top/t/topic/cjHA 著作权归作者所有。请勿转载和采集!