Java——计算机类

Description

构造计算机类,其中包含其配置信息:处理器、主板、内存、显示器、硬盘等设备,各个设备均有型号(字符串),特别的,处理器有主频(小数)和内核数(整数)、显示器有尺寸(整型)、内存和硬盘有容量数据(GB为单位)。请你尝试构造合适的类和类的关系来表示计算机,并为该计算机类添加计算价格(各设备价格之和)、打印配置信息等方法。重写相关类的equals方法,使得两个配置完全相同的计算机为相同的计算机。重写相关类的toString函数,打印计算机的配置信息。
在main函数中

Input

两个计算机对象,包含
CPU:型号、主频、内核
主板:型号
内存:容量
显示器:尺寸
硬盘:容量

Output

两个对象是否相等
两个对象的配置信息

Sample Input

Corei7 2.8 4
GIGABYTE-B250M-D3H
xiede-DDR3 8
SamsungC27F39 27
SEAGATE-ST1000DM010 2048
Corei7 2.8 4
GIGABYTE-B250M-D3H
xiede-DDR3 8
SamsungC27F39 27
SEAGATE-ST1000DM010 2048

Sample Output

true
Computer1:
CPU:
Model: Corei7
Frequency: 2.8
Number of Cores: 4
Mainboard:
Model: GIGABYTE-B250M-D3H
Memory:
Model: xiede-DDR3
Size: 8
Screen:
Model: SamsungC27F39
Size: 27
Harddisk:
Model: SEAGATE-ST1000DM010
Size: 2048
Computer2:
CPU:
Model: Corei7
Frequency: 2.8
Number of Cores: 4
Mainboard:
Model: GIGABYTE-B250M-D3H
Memory:
Model: xiede-DDR3
Size: 8
Screen:
Model: SamsungC27F39
Size: 27
Harddisk:
Model: SEAGATE-ST1000DM010
Size: 2048

HINT

为计算机类和各个组成配件都构造类,分别重写toString和equals方法
在计算机类中调用各个配件的equals和toString方法
回车用\n
小数用String.format("%.1f",1.234)
import java.util.*;

public class Main{

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        String cpumodel1 = scan.next();

        double fren1 = scan.nextDouble();

        int numofc1 = scan.nextInt();

        CPU cpu1 = new CPU(cpumodel1 , fren1 , numofc1);

        

        String mainmodel1 = scan.next();

        Mainboard mainboard1 = new Mainboard(mainmodel1);

        

        String memomodel1 = scan.next();

        int memosize1 = scan.nextInt();

        Memory memory1 = new Memory(memomodel1, memosize1);

        

        String sceemodel1 = scan.next();

        int sceesize1 = scan.nextInt();

        Screen screen1 = new Screen(sceemodel1 , sceesize1);

        

        String hardmodel1 = scan.next();

        int hardsize1 = scan.nextInt();

        Harddisk harddisk1 = new Harddisk(hardmodel1, hardsize1);

        

        Computer computer1 = new Computer(cpu1, mainboard1, memory1, screen1, harddisk1);

                

        String cpumodel2 = scan.next();

        double fren2 = scan.nextDouble();

        int numofc2 = scan.nextInt();

        CPU cpu2 = new CPU(cpumodel2 , fren2 , numofc2);

        

        String mainmodel2 = scan.next();

        Mainboard mainboard2 = new Mainboard(mainmodel2);

        

        String memomodel2 = scan.next();

        int memosize2 = scan.nextInt();

        Memory memory2 = new Memory(memomodel2, memosize2);

        

        String sceemodel2 = scan.next();

        int sceesize2 = scan.nextInt();

        Screen screen2 = new Screen(sceemodel2 , sceesize2);

        

        String hardmodel2 = scan.next();

        int hardsize2 = scan.nextInt();

        Harddisk harddisk2 = new Harddisk(hardmodel2, hardsize2);

        

        Computer computer2 = new Computer(cpu2, mainboard2, memory2, screen2, harddisk2);

        System.out.println(computer1.equals(computer2));

        System.out.println("Computer1:" + "\n" + computer1);

        System.out.println("Computer2:" + "
" + computer2);

        

    }

}

class Computer {//计算机类添加计算价格(各设备价格之和)、打印配置信息等方法。

    private int prize;//?????what the fuck??

    

    public CPU cpu;

    public Mainboard mainboard;

    public Memory memory;

    public Screen screen;

    public Harddisk harddisk;

    

    public Computer(CPU cpu , Mainboard mainboard , Memory memory , Screen screen , Harddisk harddisk) {

        this.cpu = cpu;

        this.mainboard = mainboard;

        this.memory = memory;

        this.screen = screen;

        this.harddisk = harddisk;

    }

    public String toString()

    {

        return cpu.toString() + mainboard.toString() + memory.toString() + screen.toString() + harddisk.toString();

    }

    public boolean equals(Object o) {

        if(o == null) {

            return false;

        }

        else if(o instanceof Computer) {

            Computer com = (Computer)o;

            if(this.cpu.equals(com.cpu) && this.mainboard.equals(com.mainboard) && this.memory.equals(com.memory) && this.screen.equals(com.screen) && this.harddisk.equals(com.harddisk)) {

                return true;

            }

        }

        return false;

        

    }

}

class CPU {

    private String model;//均有

    private double Frequncy;//主频

    private int NumOfCore;

    

    public CPU(String model, double Frequncy, int NumOfCore) {

        this.model = model;

        this.Frequncy = Frequncy;

        this.NumOfCore = NumOfCore;

    }

    public boolean equals(Object o) {

        if(o == null)

        {

            return false;

        }

        else if(o instanceof CPU) {

            CPU cpu = (CPU)o;

            if(this.model.equals(cpu.model) && this.Frequncy == cpu.Frequncy && this.NumOfCore == cpu.NumOfCore) {

                return true;

            }

        }

        return false;

    }

    public String toString(){

        return "CPU:" + "
" + "Model: " + this.model+"
"+"Frequency: " + String.format("%.1f", this.Frequncy)+"
" + "Number of Cores: " + this.NumOfCore+"
";

    }

}

class Mainboard {

    private String model;//均有

    

    public Mainboard(String model) {

        this.model = model;

    }

    public String toString() {

        return "Mainboard:" + "
" + "Model: " + this.model + "
";

    }

    public boolean equals(Object o) {

        if(o == null) {

            return false;

        }

        else if(o instanceof Mainboard){

            Mainboard main = (Mainboard)o;

            if(this.model.equals(main.model))

            return true;

        }

        return false;

    }

}

class Memory {

    private String model;//均有

    private int size;

    

    public Memory(String model, int size) {

        this.model = model;

        this.size = size;

    }

    public String toString() {

        return "Memory:" + "
" + "Model: " + this.model + "
" + "Size: " + this.size + "
";

    }

    public boolean equals(Object o) {

        if(o == null) {

            return false;

        }

        else if(o instanceof Memory) {

            Memory m = (Memory)o;

            if(this.model.equals(m.model) && this.size == m.size) {

                return true;

            }

        }

        return false;

    }

}

class Screen {

    private String model;//均有

    private int size;

    

    public Screen(String model, int size) {

        this.model = model;

        this.size = size;

    }

    public String toString() {

        return "Screen:" + "
" + "Model: " + this.model + "
" + "Size: " + this.size + "
";

    }

    public boolean equals(Object o) {

        if(o == null) {

            return false;

        }

        else if(o instanceof Screen) {

            Screen s = (Screen)o;

            if(this.model.equals(s.model) && this.size == s.size) {

                return true;

            }

        }

        return false;

    }

}

class Harddisk {

    private String model;//均有.

    private int size;

    

    public Harddisk(String model, int size) {

        this.model = model;

        this.size = size;

    }

    

    public String toString() {

        return "Harddisk:" + "
" + "Model: " + this.model + "
" + "Size: " + this.size ;

    }

    public boolean equals(Object o) {

        if(o == null) {

            return false;

        }

        else if(o instanceof Harddisk) {

            Harddisk h = (Harddisk)o;

            if(this.model.equals(h.model) && this.size == h.size) {

                return true;

            }

        }

        return false;

    }

}

猜你喜欢

转载自blog.csdn.net/Yolanda_Salvatore/article/details/83241008