In: Computer Science
Design a phone purchase program in JOptionPane.
Select model ( can be made up 2 models minimum)
Color selection( 2 options minimum)
Storage Selection( 2 options minimum)
Accessories (2 options minimum)
Payment Plan or one time purchase
Prices can be made up
Print out price. Allow user termination. Create a class and test programs.
Code:
import java.util.ArrayList;
import javax.swing.JOptionPane;
class market {
public static void main(String[] args) {
int MenuList;
String myOrder = "No order currently set"; //if no any item purchsed then print this message
do{
System.out.println("---MENU---");
//here there are 3 options availabel 1 is for purchase 2 is for print purchased order 3 is for exit
String[] menu = {"[1]Purchase Phone","[2]print","[3]exit"};
for (int i=0;i<3;i++) { //display 3 options in command prompt
System.out.println(menu[i]);
}
String MenuString = JOptionPane.showInputDialog(null, " Choose number: "); //get selected option
MenuList = Integer.parseInt(MenuString);
int price = 0;
if(MenuList==1) { //if option is 1 then go for purchase program
System.out.println();
String[] model = {"iPhone11","iPhone12"};
String[] color = {"Red","Black","Green"};
String[] storage = {"64GB","128GB"};
String[] acc = {"Airpods","Covor"};
String[] payment = {"Cash","Card"};
myOrder = "";
//here you can choose one by one options first you have to select model then color then storage then accessories then payment option
String Model = (String) JOptionPane.showInputDialog(null,"Select Model", "Welcome " + "!",JOptionPane.QUESTION_MESSAGE, null, model, "iPhone11");
String Color = (String) JOptionPane.showInputDialog(null,"Select Color","",JOptionPane.QUESTION_MESSAGE, null, color, "Black");
String Storage = (String) JOptionPane.showInputDialog(null,"Select Storage","",JOptionPane.QUESTION_MESSAGE, null, storage, "64GB");
String Acc = (String) JOptionPane.showInputDialog(null,"Select Accessories ","",JOptionPane.QUESTION_MESSAGE, null, acc, "Covor");
String pay = (String) JOptionPane.showInputDialog(null,"Select payment method","",JOptionPane.QUESTION_MESSAGE, null, payment, "Cash");
//here it calculate total price of item
if(Model=="iPhone11")
price+=50000;
else
price+=60000;
if(Storage=="64GB")
price+=5000;
else
price+=8000;
if(Acc=="Airpods")
price+=5000;
else
price+=500;
//for display a selected options and total price of item
myOrder="Your model is: "+Model+"\nYour Color is: "+Color+"\nYour Storage is: "+Storage+"\nYour Accessories is: "+Acc+"\nYour Payment method is: "+pay;
myOrder+="\n\nYour total price is: "+price;
} else if(MenuList==2) { //if option is 2 then and any item is purchased then print purchased item otherwise print default message
JOptionPane.showMessageDialog(null,myOrder);
} else if(MenuList==3) {
JOptionPane.showMessageDialog(null,"Exit,Bye"); //if option is 3 selected then exit
} else {
System.out.print("Invalid");
}
}while(MenuList != 3); //it go untill you select option 3
}
}
Output:
It display 3 option. then if you want go for purchase program then enter 1.
Then enter 2 for print select item and price.
Then enter 3 for exit.