|
| 1 | +/* |
| 2 | +Rustam Zokirov, U1910049 |
| 3 | +Lab Assignment #3 |
| 4 | +*/ |
| 5 | + |
| 6 | +import java.util.Scanner; // import Scanner class |
| 7 | + |
| 8 | +class Mobile { // Mobile class |
| 9 | + // attributes of class |
| 10 | + private String company, model; // model and company of phone |
| 11 | + private boolean isOn, hasCamera; // booleans isOn and hasCamera |
| 12 | + private int memory, volume; // volume from 0 to 10 |
| 13 | + |
| 14 | + public Mobile(){ // default constructor |
| 15 | + company = "Samsung"; |
| 16 | + model = "Z Fold 2"; |
| 17 | + isOn = true; |
| 18 | + hasCamera = true; |
| 19 | + memory = 512; |
| 20 | + volume = 10; |
| 21 | + } // End |
| 22 | + |
| 23 | + public Mobile(String company,String model, boolean isOn, boolean hasCamera, int memory, int volume){ // parametrized constructor |
| 24 | + this.company = company; |
| 25 | + this.model = model; |
| 26 | + this.isOn = isOn; |
| 27 | + this.hasCamera = hasCamera; |
| 28 | + this.memory = memory; |
| 29 | + this.volume = volume; |
| 30 | + } // End |
| 31 | + |
| 32 | + public void setCompany(String company){ // Setter for company |
| 33 | + this.company = company; |
| 34 | + } |
| 35 | + |
| 36 | + public String getCompany(){ // Getter for company |
| 37 | + return company; |
| 38 | + } |
| 39 | + |
| 40 | + public void setModel(String model){ // Setter for model |
| 41 | + this.model = model; |
| 42 | + } |
| 43 | + |
| 44 | + public String getModel(){ // Getter for model |
| 45 | + return model; |
| 46 | + } |
| 47 | + |
| 48 | + public void setIsOn(boolean isOn){ // Setter for isOn |
| 49 | + this.isOn = isOn; |
| 50 | + } |
| 51 | + |
| 52 | + public boolean getIsOn(){ // Getter for isOn |
| 53 | + return isOn; |
| 54 | + } |
| 55 | + |
| 56 | + public void setHasCamera(boolean hasCamera){ // Setter for hasCamera |
| 57 | + this.hasCamera = hasCamera; |
| 58 | + } |
| 59 | + |
| 60 | + public boolean getHasCamera(){ // Getter for hasCamera |
| 61 | + return hasCamera; |
| 62 | + } |
| 63 | + |
| 64 | + public void setMemory(int memory){ // Setter for volume |
| 65 | + this.memory = memory; |
| 66 | + } |
| 67 | + |
| 68 | + public int getMemory(){ // Getter for volume |
| 69 | + return memory; |
| 70 | + } |
| 71 | + |
| 72 | + public void setVolume(int volume){ // Setter for volume |
| 73 | + this.volume = volume; |
| 74 | + } |
| 75 | + |
| 76 | + public int getVolume(){ // Getter for volume |
| 77 | + return volume; |
| 78 | + } |
| 79 | + |
| 80 | + public void switchOn(){ |
| 81 | + if (!isOn) // Validating for on, off |
| 82 | + setIsOn(true); |
| 83 | + else |
| 84 | + System.out.println("\nMobile phone is already switched on!"); |
| 85 | + } // End |
| 86 | + |
| 87 | + public void switchOff() { |
| 88 | + if (isOn) // Validating for on, off |
| 89 | + setIsOn(false); |
| 90 | + else |
| 91 | + System.out.println("\nMobile phone is already switched off"); |
| 92 | + } // End |
| 93 | + |
| 94 | + public void increaseVolume() { |
| 95 | + if (isOn && volume < 10) {// validating volume (0, 10) and on, off |
| 96 | + volume++; // increasing volume |
| 97 | + System.out.println("Volume is " + getVolume()); // outputting the volume |
| 98 | + } |
| 99 | + else if (!isOn) |
| 100 | + System.out.println("\nMobile phone is switched off!"); |
| 101 | + else |
| 102 | + System.out.println("\nVolume is 10!"); |
| 103 | + }// End |
| 104 | + |
| 105 | + public void decreaseVolume() { |
| 106 | + if (isOn && volume >= 0) { // validating volume (0, 10) and on, off |
| 107 | + volume--; // decreasing volume |
| 108 | + System.out.println("Volume is " + getVolume()); // outputting the volume |
| 109 | + } |
| 110 | + else if (!isOn) |
| 111 | + System.out.println("\nMobile phone is switched off!"); |
| 112 | + else |
| 113 | + System.out.println("\nVolume is 0!"); |
| 114 | + } // End |
| 115 | + |
| 116 | + public void setDetail() { // this function will set all the needed info for object |
| 117 | + Scanner input = new Scanner(System.in); // Create a Scanner object |
| 118 | + |
| 119 | + System.out.println("\n***** Mobile phone details *****"); |
| 120 | + |
| 121 | + System.out.println("Company: "); |
| 122 | + String inputCompany = input.next(); |
| 123 | + setCompany(inputCompany); // setting the company to inputCompany |
| 124 | + |
| 125 | + System.out.println("\nModel: "); |
| 126 | + String inputModel = input.next(); |
| 127 | + setModel(inputModel); // setting the model to inputModel |
| 128 | + |
| 129 | + setIsOn(true); // phone is On |
| 130 | + |
| 131 | + while(true){ // validating the input |
| 132 | + System.out.println("\nHas Camera? (1.Yes 2.No) "); |
| 133 | + int inputHasCamera= input.nextInt(); // taking the input |
| 134 | + if (inputHasCamera == 1) { |
| 135 | + setHasCamera(true); // setting the hasCamera |
| 136 | + break; |
| 137 | + } |
| 138 | + else if (inputHasCamera == 2) { |
| 139 | + setHasCamera(false); // setting the hasCamera |
| 140 | + break; |
| 141 | + } |
| 142 | + else{ |
| 143 | + System.out.println("\nPlease enter values 1 or 2!"); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + while (true){ // validation the memory input |
| 148 | + System.out.println("\nMemory: "); |
| 149 | + int inputMemory = input.nextInt(); |
| 150 | + |
| 151 | + if(inputMemory > 0){ |
| 152 | + setMemory(inputMemory); |
| 153 | + break; |
| 154 | + } |
| 155 | + else |
| 156 | + System.out.println("\nPlease enter correct values!"); |
| 157 | + } |
| 158 | + |
| 159 | + while (true) { // validating the volume |
| 160 | + System.out.println("\nVolume (0, 10): "); |
| 161 | + int inputVolume = input.nextInt(); |
| 162 | + |
| 163 | + if(inputVolume >= 0 && inputVolume <= 10){ |
| 164 | + setVolume(inputVolume); |
| 165 | + break; |
| 166 | + } |
| 167 | + else |
| 168 | + System.out.println("\nPlease enter correct values!"); |
| 169 | + } // End |
| 170 | + } |
| 171 | + |
| 172 | + public void getDetail(){ |
| 173 | + System.out.println("\n***** Mobile phone details *****"); |
| 174 | + System.out.println("Company: " + getCompany()); |
| 175 | + System.out.println("Model: " + getModel()); |
| 176 | + System.out.println("On: " + getIsOn()); |
| 177 | + System.out.println("Has Camera: " + getHasCamera()); |
| 178 | + System.out.println("Memory: " + getMemory()); |
| 179 | + System.out.println("Volume: " + getVolume()); |
| 180 | + } |
| 181 | +} // End |
| 182 | + |
| 183 | +public class MobileTest { // MobileTest class |
| 184 | + public static Scanner input = new Scanner(System.in); // Create a Scanner object |
| 185 | + |
| 186 | + public static void main(String[] args){ // main method |
| 187 | + int option, isExit; // initializing some variables |
| 188 | + Mobile mobile = new Mobile(); // creating object of Mobile |
| 189 | + |
| 190 | + do { |
| 191 | + option = showMenu(); // the option for switch |
| 192 | + switch (option){ |
| 193 | + case 1: |
| 194 | + mobile.setDetail(); |
| 195 | + break; |
| 196 | + case 2: |
| 197 | + mobile.getDetail(); |
| 198 | + break; |
| 199 | + case 3: |
| 200 | + mobile.increaseVolume(); |
| 201 | + break; |
| 202 | + case 4: |
| 203 | + mobile.decreaseVolume(); |
| 204 | + break; |
| 205 | + case 5: |
| 206 | + mobile.switchOn(); |
| 207 | + break; |
| 208 | + case 6: |
| 209 | + mobile.switchOff(); |
| 210 | + break; |
| 211 | + case 0: |
| 212 | + System.out.println("Quitting Program..."); |
| 213 | + System.exit(0); // program ends |
| 214 | + break; |
| 215 | + default: |
| 216 | + System.out.println("\nSorry, please enter valid option!"); |
| 217 | + } // End of switch |
| 218 | + |
| 219 | + System.out.println("\n----------------------------------------------------------------------------"); |
| 220 | + System.out.println("Do you want to continue? (1.Yes 2.No)"); |
| 221 | + isExit = input.nextInt(); // valid-on |
| 222 | + |
| 223 | + } while(isExit == 1); |
| 224 | + |
| 225 | + } // End of main |
| 226 | + |
| 227 | + public static int showMenu() { |
| 228 | + int option = 0; |
| 229 | + // Printing menu to screen |
| 230 | + System.out.println("\n***** MAIN MENU *****"); |
| 231 | + System.out.println("1. Set details"); |
| 232 | + System.out.println("2. Get details"); |
| 233 | + System.out.println("3. Increase volume"); |
| 234 | + System.out.println("4. Decrease volume"); |
| 235 | + System.out.println("5. Switch on"); |
| 236 | + System.out.println("6. Switch off"); |
| 237 | + System.out.println("0. Exit"); |
| 238 | + |
| 239 | + // Getting user option from above menu |
| 240 | + System.out.println("Your choice: "); |
| 241 | + option = input.nextInt(); |
| 242 | + // re |
| 243 | + return option; |
| 244 | + } // End of showMenu |
| 245 | +} // End of class MobileTest |
0 commit comments