주제
하드 디스크 번호, 이름, 용량(GB), 인터페이스 유형(USB2.0 또는 USB3.0 참조), 가격 및 해당 구성 방법 및 해당 설정자를 포함한 기타 속성을 포함하는 모바일 하드 디스크 클래스 FlashDisk를 정의합니다. getters.method를 사용하고 toString 메서드를 재정의합니다.
팁: Comparable 인터페이스 구현
1. 일부 U 디스크 정보를 입력하고 컬렉션 TreeSet 개체에 저장
2. 모든 U 디스크 정보를 TreeSet 개체에 출력
팁: 테스트 데이터
U001 Netac 1024GB USB3.0 789위안
U002 스캔 디스크 500GB USB2.0 480위안
U003 패트리어트 2024GB USB3.0 1230위안
U004 소니 500GB usb2.0 950위안
U004 TranShow 1024GB usb3.0 1300위안
플래시디스크 클래스
/**
* @Auther: 茶凡
* @ClassName FlashDisk
* @Description TODO
* @date 2023/6/8 16:43
* @Version 1.0
*/
public class FlashDisk implements Comparable<FlashDisk>{
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) {
TreeSet<FlashDisk> 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 Netac 1024GB USB3.0 789위안
U002 스캔디스크 500GB USB2.0 480위안
U003 Patriot 2024GB USB3.0 1230위안
U004 Sony 500GB usb2.0 950위안
U005 TranShow 1024GB usb3.0 1300위안