题目
定义一个移动硬盘类FlashDisk,包含硬盘的编号、名称、容量(GB为单位)、接口类型(是指USB2.0或者USB3.0)、价格等属性,包含相应的构造方法和相应的setter和getter方法,以及重写toString方法。
提示:实现Comparable接口
1.输入若干U盘信息保存到集合TreeSet对象中;
2.输出TreeSet对象中的U盘信息全部输出;
提示:测试数据
U001 朗科 1024GB USB3.0 789元
U002 scandisk 500GB USB2.0 480元
U003 爱国者 2024GB USB3.0 1230元
U004 Sony 500GB usb2.0 950元
U004 TranShow 1024GB usb3.0 1300元
FlashDisk类
/**
* @Auther: 茶凡
* @ClassName FlashDisk
* @Description TODO
* @date 2023/6/8 16:43
* @Version 1.0
*/
public class FlashDisk implements ComparableFlashDisk>{
private String NO;
private String name;
private int capacity;
private String interfaceType;
private int price;
public FlashDisk(String NO, String name, int capacity, String interfaceType, int price) {
this.NO = NO;
this.name = name;
this.capacity = capacity;
this.interfaceType = interfaceType;
this.price = price;
}
public FlashDisk() {}
public String getNO() { return NO; }
public void setNO(String NO) { this.NO = NO; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getCapacity() { return capacity; }
public void setCapacity(int capacity) { this.capacity = capacity; }
public String getInterfaceType() { return interfaceType; }
public void setInterfaceType(String interfaceType) { this.interfaceType = interfaceType; }
public int getPrice() { return price; }
public void setPrice(int price) { this.price = price; }
@Override
public String toString() {
String formattedString = String.format("%-6s %-10s %6dGB %10s %6d元", NO, name,capacity,interfaceType,price);
return formattedString;
}
@Override
public int compareTo(FlashDisk o) {
return this.NO.compareTo(o.getNO());
}
}
测试
/**
* @Auther: 茶凡
* @ClassName Test
* @Description TODO
* @date 2023/6/8 16:54
* @Version 1.0
*/
public class Test {
public static void main(String[] args) {
TreeSetFlashDisk> flashDisks = new TreeSet>();
flashDisks.add(new FlashDisk("U001", "朗科", 1024, "USB3.0", 789));
flashDisks.add(new FlashDisk("U002", "scandisk", 500, "USB2.0", 480));
flashDisks.add(new FlashDisk("U003", "爱国者", 2024, "USB3.0", 1230));
flashDisks.add(new FlashDisk("U004", "Sony", 500, "usb2.0", 950));
flashDisks.add(new FlashDisk("U005", "TranShow", 1024, "usb3.0", 1300));
for (FlashDisk flashDisk : flashDisks) {
System.out.println(flashDisk);
}
}
}
测试结果
U001 朗科 1024GB USB3.0 789元
U002 scandisk 500GB USB2.0 480元
U003 爱国者 2024GB USB3.0 1230元
U004 Sony 500GB usb2.0 950元
U005 TranShow 1024GB usb3.0 1300元
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net