22
Июл
2021

Перечисляемые типы Enum

Всем привет! Изучаю Java, столкнулся с проблемой, не понимаю как для перечисляемых типов поставить вес, и вывести общий вес всех компонентов ПК. Написал код, создал перечисляемые типы для каждого компонента, а вот как быть с весом не понимаю. Код ниже(кроме enum-ов):

public class Computer {
    private final int processorFrequency;
    private final int numberOfCores;
    private final String processorManufacturer;
    private final int ramAmount;
    private final int memoryAmount;
    private final double screenDiagonal;
    private final boolean backlight;
    private final String vendor;
    private final String name;

    public Computer(int processorFrequency, int numberOfCores, String processorManufacturer,
                    int ramAmount, int memoryAmount, double screenDiagonal,
                    boolean backlight, String vendor, String name) {
        this.processorFrequency = processorFrequency;
        this.numberOfCores = numberOfCores;
        this.processorManufacturer = processorManufacturer;
        this.ramAmount = ramAmount;
        this.memoryAmount = memoryAmount;
        this.screenDiagonal = screenDiagonal;
        this.backlight = backlight;
        this.vendor = vendor;
        this.name = name;
    }

    public Computer setProcessorFrequency(int processorFrequency) {
        return new Computer(processorFrequency, numberOfCores, processorManufacturer,
                ramAmount, memoryAmount, screenDiagonal, backlight, vendor, name);
    }

    public int getProcessorFrequency(){return processorFrequency;}

    public double chooseProcessorWeight(ProcessorType processorType){
        switch (processorType){
            case BUFFERPROCESSOR:
                return 1;
            case PREPROCESSOR:
                return 5;
            case CISC:
                return 10;
            case RISC:
                return 15;
            case CLONE:
                return 20;
        }
        return 0;
    }

    public Computer setNumberOfCores(int numberOfCores) {
        return new Computer(processorFrequency, numberOfCores, processorManufacturer,
                ramAmount, memoryAmount, screenDiagonal, backlight, vendor, name);
    }

    public int getNumberOfCores(){return numberOfCores;}

    public Computer setProcessorManufacturer(String processorManufacturer) {
        return new Computer(processorFrequency, numberOfCores, processorManufacturer,
                ramAmount, memoryAmount, screenDiagonal, backlight, vendor, name);
    }

    public String getProcessorManufacturer(){return processorManufacturer;}

    public Computer ramAmount(int ramAmount) {
        return new Computer(processorFrequency, numberOfCores, processorManufacturer,
                ramAmount, memoryAmount, screenDiagonal, backlight, vendor, name);
    }

    public double getRamAmount(){return ramAmount;}

    public String chooseRamType(RamType ramType){
        switch (ramType){
            case SDRAM:
                return "SDRAM";
            case DDR:
                return "DDR";
            case DDR2:
                return "DDR3";
            case DDR3:
                return "SDRAM";
            case DDR4:
                return "DDR4";
        }
        return null;
    }

    public Computer setMemoryAmount(int memoryAmount) {
        return new Computer(processorFrequency, numberOfCores, processorManufacturer,
                ramAmount, memoryAmount, screenDiagonal, backlight, vendor, name);
    }

    public int getMemoryAmount(){return memoryAmount;}

    public String chooseHddType(HddType hddType){
        switch (hddType){
            case HDD:
                return "HDD";
            case SSD:
                return "SSD";
        }
        return null;
    }

    public Computer setScreenDiagonal(double screenDiagonal) {
        return new Computer(processorFrequency, numberOfCores, processorManufacturer,
                ramAmount, memoryAmount, screenDiagonal, backlight, vendor, name);
    }

    public double getScreenDiagonal(){return screenDiagonal;}

    public String chooseScreenType(ScreenType screenType){
        switch (screenType){
            case IPS:
                return "IPS";
            case TN:
                return "TN";
            case VA:
                return "VA";
        }
        return null;
    }

    public Computer isBacklight(boolean backlight) {
        return new Computer(processorFrequency, numberOfCores, processorManufacturer,
               ramAmount, memoryAmount, screenDiagonal, backlight, vendor, name);
    }

    public boolean hasBacklight(){return backlight;}

    public String chooseKeyboardType(KeyboardType keyboardType){
        switch (keyboardType){
            case MEMBRANE:
                return "МЕМБРАННАЯ КЛАВИАТУРА";
            case CONDUCTIVE:
                return "С ЭЛЕКТРОПРОВОДЯЩЕЙ РЕЗИНОЙ";
            case SCISSORSWITCH:
                return "НОЖНИЧНАЯ МЕМБРАННАЯ КЛАВИАТУРА";
        }
        return null;
    }

    public Computer setVendor(String vendor) {
        return new Computer(processorFrequency, numberOfCores, processorManufacturer,
                ramAmount, memoryAmount, screenDiagonal, backlight, vendor, name);
    }

    public String getVendor(){return vendor;}

    public Computer setName(String name) {
        return new Computer(processorFrequency, numberOfCores, processorManufacturer,
                ramAmount, memoryAmount, screenDiagonal, backlight, vendor, name);
    }

    public String getName(){return name;}

    public String toString(){return "Производитель ПК: " + vendor + "\n" + "Имя ПК: " + name + "\n" +
            "Частота процессора: " + processorFrequency + "\n" + "Число ядер: " + numberOfCores + "\n" +
        "Производитель процессора: " + processorManufacturer + "\n" + "Оперативная память: " +
            ramAmount + "\n" + "Объём памяти: " + memoryAmount + "\n" + "Диагональ экрана: " +
            screenDiagonal + "\n" + "Наличие подсветки: " + backlight;}
}

И класс Main:

public class Main {
    public static void main(String[] args) {
        Computer computer = new Computer(1000, 4, "China",
                8, 4, 21, true, "Intel", "ПК Пети");
        computer.chooseProcessorWeight(ProcessorType.CISC);
        computer.chooseRamType(RamType.DDR);
        computer.chooseHddType(HddType.HDD);
        computer.chooseScreenType(ScreenType.VA);
        computer.chooseKeyboardType(KeyboardType.CONDUCTIVE);
        System.out.println(computer);
        System.out.println("Вес процессора " + computer.chooseProcessorWeight(ProcessorType.CISC)
                + " грамм;");
        System.out.println("Тип оперативной памяти " + computer.chooseRamType(RamType.DDR));
        System.out.println("Тип жесткого диска " + computer.chooseHddType(HddType.HDD));
        System.out.println("Тип экрана " + computer.chooseScreenType(ScreenType.VA));
        System.out.println("Тип клавиатуры " + computer.chooseKeyboardType(KeyboardType.CONDUCTIVE));
    }
}

Источник: https://ru.stackoverflow.com/questions/1307618/%D0%9F%D0%B5%D1%80%D0%B5%D1%87%D0%B8%D1%81%D0%BB%D1%8F%D0%B5%D0%BC%D1%8B%D0%B5-%D1%82%D0%B8%D0%BF%D1%8B-enum

Тебе может это понравится...

Добавить комментарий