写一个电话类(Phone),具有品牌brand、型号model、价格price三个属性。从键盘上输入若干条电话信息,根据它们的价格由低到高排序。如果价格相同,则按照品牌的字母排序(由a到z)并把电话信息的排序结果写入磁盘文件。要求用户从键盘上输入指定品牌和型号的电话,再从前面写入文件中读取电话信息,要求删除用户指定的品牌型号的电话之后将结果保存为新文件。
请给出具体思路
package com.example.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Phone implements Comparable<Phone>{
private String brand;
private String model;
private int price;
public Phone(String brand, String model, int price) {
this.brand = brand;
this.model = model;
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 int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public int compareTo(Phone o) {
//先按照价格排序
if (this.price>o.price){
return (this.price-o.price);
}
if (this.price<o.price){
return (this.price-o.price);
}
//再按照品牌排序
if (this.brand.compareTo(o.getBrand())>0){
return 1;
}
if (this.brand.compareTo(o.getBrand())>0){
return -1;
}
return 0;
}
@Override
public String toString() {
return "Phone{" +
"brand='" + brand + '\'' +
", model='" + model + '\'' +
", price=" + price +
'}';
}
public static void main(String[] args) {
List<Phone> list = new ArrayList<Phone>();
list.add(new Phone("a","6.6英寸",6000));
list.add(new Phone("b","6.6英寸",7000));
list.add(new Phone("b","6.6英寸",6000));
list.add(new Phone("c","6.6英寸",8000));
Collections.sort(list);
for (Phone phone: list) {
System.out.println(phone);
}
}
}