java构造计算机类,其中包含其配置信息:处理器、主板、内存、显示器、硬盘等设备,各个设备均有型号(字符串),特别的,处理器有主频(小数)和内核数(整数)、显示器有尺寸(整型)、内存和硬盘有容量数据(GB为单位)。请你尝试构造合适的类和类的关系来表示计算机,并为该计算机类添加计算价格(各设备价格之和)、打印配置信息等方法。重写相关类的equals方法,使得两个配置完全相同的计算机为相同的计算机。重写相关类的toString函数,打印计算机的配置信息。 提示: 为计算机类和各个组成配件都构造类,分别重写toString和equals方法 在计算机类中调用各个配件的equals和toString方法
时间: 2024-01-22 15:17:30 浏览: 262
以下是Java代码实现:
```java
public class Computer {
private Processor processor;
private Motherboard motherboard;
private Memory memory;
private Display display;
private HardDisk hardDisk;
public Computer(Processor processor, Motherboard motherboard, Memory memory, Display display, HardDisk hardDisk) {
this.processor = processor;
this.motherboard = motherboard;
this.memory = memory;
this.display = display;
this.hardDisk = hardDisk;
}
public double calculatePrice() {
return processor.getPrice() + motherboard.getPrice() + memory.getPrice()
+ display.getPrice() + hardDisk.getPrice();
}
public void printConfigInfo() {
System.out.println("Processor: " + processor.toString());
System.out.println("Motherboard: " + motherboard.toString());
System.out.println("Memory: " + memory.toString());
System.out.println("Display: " + display.toString());
System.out.println("HardDisk: " + hardDisk.toString());
}
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Computer computer = (Computer) obj;
return processor.equals(computer.processor) && motherboard.equals(computer.motherboard)
&& memory.equals(computer.memory) && display.equals(computer.display)
&& hardDisk.equals(computer.hardDisk);
}
@Override
public String toString() {
return "Computer Configuration:\n" +
"Processor: " + processor.toString() + "\n" +
"Motherboard: " + motherboard.toString() + "\n" +
"Memory: " + memory.toString() + "\n" +
"Display: " + display.toString() + "\n" +
"HardDisk: " + hardDisk.toString() + "\n" +
"Total Price: $" + calculatePrice();
}
}
class Processor {
private String model;
private double frequency;
private int cores;
private double price;
public Processor(String model, double frequency, int cores, double price) {
this.model = model;
this.frequency = frequency;
this.cores = cores;
this.price = price;
}
public double getPrice() {
return price;
}
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Processor processor = (Processor) obj;
return Double.compare(processor.frequency, frequency) == 0 && cores == processor.cores
&& model.equals(processor.model);
}
@Override
public String toString() {
return model + " " + frequency + "GHz " + cores + " cores $" + price;
}
}
class Motherboard {
private String model;
private String chipset;
private double price;
public Motherboard(String model, String chipset, double price) {
this.model = model;
this.chipset = chipset;
this.price = price;
}
public double getPrice() {
return price;
}
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Motherboard motherboard = (Motherboard) obj;
return model.equals(motherboard.model) && chipset.equals(motherboard.chipset);
}
@Override
public String toString() {
return model + " " + chipset + " $" + price;
}
}
class Memory {
private String model;
private int capacity;
private double price;
public Memory(String model, int capacity, double price) {
this.model = model;
this.capacity = capacity;
this.price = price;
}
public double getPrice() {
return price;
}
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Memory memory = (Memory) obj;
return capacity == memory.capacity && model.equals(memory.model);
}
@Override
public String toString() {
return model + " " + capacity + "GB $" + price;
}
}
class Display {
private String model;
private int size;
private double price;
public Display(String model, int size, double price) {
this.model = model;
this.size = size;
this.price = price;
}
public double getPrice() {
return price;
}
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Display display = (Display) obj;
return size == display.size && model.equals(display.model);
}
@Override
public String toString() {
return model + " " + size + " inch $" + price;
}
}
class HardDisk {
private String model;
private int capacity;
private double price;
public HardDisk(String model, int capacity, double price) {
this.model = model;
this.capacity = capacity;
this.price = price;
}
public double getPrice() {
return price;
}
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
HardDisk hardDisk = (HardDisk) obj;
return capacity == hardDisk.capacity && model.equals(hardDisk.model);
}
@Override
public String toString() {
return model + " " + capacity + "GB $" + price;
}
}
```
使用示例:
```java
Processor processor = new Processor("Intel Core i7", 3.2, 8, 300);
Motherboard motherboard = new Motherboard("ASUS Prime", "Z490", 200);
Memory memory = new Memory("Corsair Vengeance", 16, 100);
Display display = new Display("Dell UltraSharp", 27, 300);
HardDisk hardDisk = new HardDisk("Seagate Barracuda", 1000, 80);
Computer computer1 = new Computer(processor, motherboard, memory, display, hardDisk);
Computer computer2 = new Computer(processor, motherboard, memory, display, hardDisk);
System.out.println(computer1.calculatePrice()); // 输出 980.0
computer1.printConfigInfo(); // 输出计算机的配置信息
System.out.println(computer1.equals(computer2)); // 输出 true
System.out.println(computer1.toString()); // 输出计算机的配置信息及总价
```
阅读全文