Write a menu driven application to maintain the smart phone technical specifications
for various brands using JAVA. Use constructors, getter and setter functions. Your
application must contain the following functionalities.
For each mobile your application must maintain the details such as productName,
operatingSystem, displaySize, memory, etc.
Get the product details from admin
In the menu give the user options to add, edit, delete or display the smart phone details
public class SmartPhone {
private int productNumber;
private String operatingSystem;
private double displaySize;
private int memory;
public SmartPhone() {
this.productNumber = -1;
this.operatingSystem = "Unknown";
this.displaySize = -1;
this.memory = -1;
}
public SmartPhone(int productNumber, String operatingSystem, double displaySize, int memory) {
this.productNumber = productNumber;
this.operatingSystem = operatingSystem;
this.displaySize = displaySize;
this.memory = memory;
}
public int getProductNumber() {
return productNumber;
}
public void setProductNumber(int productNumber) {
this.productNumber = productNumber;
}
public String getOperatingSystem() {
return operatingSystem;
}
public void setOperatingSystem(String operatingSystem) {
this.operatingSystem = operatingSystem;
}
public double getDisplaySize() {
return displaySize;
}
public void setDisplaySize(double displaySize) {
this.displaySize = displaySize;
}
public int getMemory() {
return memory;
}
public void setMemory(int memory) {
this.memory = memory;
}
@Override
public String toString() {
return "SmartPhone{" +
"productNumber=" + productNumber +
", operatingSystem='" + operatingSystem + '\'' +
", displaySize=" + displaySize +
", memory=" + memory +
'}';
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input;
int index;
ArrayList<SmartPhone> smartPhones = new ArrayList<>();
smartPhones.add(new SmartPhone(1, "Symbian", 2, 30));
smartPhones.add(new SmartPhone(2, "Android", 4, 60));
smartPhones.add(new SmartPhone(3, "Windows", 3.5, 45));
while (true) {
System.out.println("\n1. Add\n" +
"2. Edit\n" +
"3. Delete\n" +
"4. Display\n" +
"0. Exit");
input = in.nextLine();
switch (input) {
case "1":
SmartPhone smartPhone = new SmartPhone();
System.out.println("Product number:");
smartPhone.setProductNumber(Integer.parseInt(in.nextLine()));
System.out.println("Operating system:");
smartPhone.setOperatingSystem(in.nextLine());
System.out.println("Display size:");
smartPhone.setDisplaySize(Double.parseDouble(in.nextLine()));
System.out.println("Product number:");
smartPhone.setMemory(Integer.parseInt(in.nextLine()));
smartPhones.add(smartPhone);
break;
case "2":
System.out.println("Enter index to update:");
index = Integer.parseInt(in.nextLine());
if (index >= 0 && index < smartPhones.size()) {
while (true) {
System.out.println("a. Product number\n" +
"b. Operating system\n" +
"c. Display size\n" +
"d. Product number\n" +
"s. Skip");
input = in.nextLine();
if (input.equalsIgnoreCase("a")) {
System.out.println("Product number:");
smartPhones.get(index).setProductNumber(Integer.parseInt(in.nextLine()));
} else if (input.equalsIgnoreCase("b")) {
System.out.println("Operating system:");
smartPhones.get(index).setOperatingSystem(in.nextLine());
} else if (input.equalsIgnoreCase("c")) {
System.out.println("Display size:");
smartPhones.get(index).setDisplaySize(Double.parseDouble(in.nextLine()));
} else if (input.equalsIgnoreCase("d")) {
System.out.println("Product number:");
smartPhones.get(index).setMemory(Integer.parseInt(in.nextLine()));
} else if (input.equalsIgnoreCase("s")) {
break;
}
if (input.charAt(0) >= 'a' && input.charAt(0) <= 'd') {
System.out.println(index + " <- Updated");
}
}
}
break;
case "3":
System.out.println("Enter index to delete:");
index = Integer.parseInt(in.nextLine());
if (index >= 0 && index < smartPhones.size()) {
smartPhones.remove(index);
System.out.println(index + " <- Deleted");
}
break;
case "4":
for (int i = 0; i < smartPhones.size(); i++) {
System.out.println(i + " " + smartPhones.get(i));
}
break;
case "0":
System.exit(0);
}
}
}
}
Comments
Leave a comment