|
| 1 | +package com.codedifferently.lesson16.PersonalComputer; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +public class Server { |
| 7 | + public enum ServerStatus { |
| 8 | + OFFLINE, |
| 9 | + ONLINE, |
| 10 | + SLEEP, |
| 11 | + SHUTDOWN |
| 12 | + } |
| 13 | + |
| 14 | + private String brand; |
| 15 | + private String model; |
| 16 | + private String cpu; |
| 17 | + private int ramGB; |
| 18 | + private int storageGB; |
| 19 | + private int nic; |
| 20 | + private boolean isPoweredOn; |
| 21 | + private List<String> installedPrograms; |
| 22 | + private ServerStatus status; // use the enum here |
| 23 | + |
| 24 | + public Server( |
| 25 | + String brand, String model, String cpu, int ramGB, int storageGB, int nic, String installedPrograms) { |
| 26 | + this.brand = brand; |
| 27 | + this.model = model; |
| 28 | + this.cpu = cpu; |
| 29 | + this.ramGB = ramGB; |
| 30 | + this.nic = nic; |
| 31 | + this.storageGB = storageGB; |
| 32 | + this.isPoweredOn = false; |
| 33 | + this.installedPrograms = new ArrayList<>(); |
| 34 | + this.status = ServerStatus.OFFLINE; |
| 35 | + } |
| 36 | + |
| 37 | + public class ComputerAlreadyOnException extends Exception { |
| 38 | + public ComputerAlreadyOnException(String message) { |
| 39 | + super(message); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public void powerOn() throws ComputerAlreadyOnException { |
| 44 | + if (!isPoweredOn) { |
| 45 | + isPoweredOn = true; |
| 46 | + status = ServerStatus.ONLINE; |
| 47 | + } else { |
| 48 | + throw new ComputerAlreadyOnException("Computer is already powered on."); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public String getBrand() { |
| 53 | + return brand; |
| 54 | + } |
| 55 | + |
| 56 | + public String getModel() { |
| 57 | + return model; |
| 58 | + } |
| 59 | + |
| 60 | + public String getCPU() { |
| 61 | + return cpu; |
| 62 | + } |
| 63 | + |
| 64 | + public int getRAMGB() { |
| 65 | + return ramGB; |
| 66 | + } |
| 67 | + |
| 68 | + public int getSTORAGEGB() { |
| 69 | + return storageGB; |
| 70 | + } |
| 71 | + |
| 72 | + public boolean isPoweredOn() { |
| 73 | + return isPoweredOn; |
| 74 | + } |
| 75 | + |
| 76 | + public ServerStatus getStatus() { |
| 77 | + return status; |
| 78 | + } |
| 79 | + |
| 80 | + public void listPrograms() { |
| 81 | + System.out.println("Installed Programs:"); |
| 82 | + for (String program : installedPrograms) { |
| 83 | + System.out.println("- " + program); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public void installProgram(String programName) { |
| 88 | + if (!installedPrograms.contains(programName)) { |
| 89 | + installedPrograms.add(programName); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public List<String> getInstalledPrograms() { |
| 94 | + return installedPrograms; |
| 95 | + } |
| 96 | +} |
0 commit comments