In: Computer Science
The problem I'm having with this code is that when you choose the 24 month payment plan and hit 2 to print your result is that there are way to many decimal places.
is there a way to resolve this problem? I have tried formatting and can not figure out my setback. Thank you for any help you can give me.
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.ImageIcon;
import java.awt.Color;
class market {
public static void main(String[] args) {
int MenuList;
UIManager UI=new UIManager();
UI.put("OptionPane.background", new Color(255, 0, 0));
UI.put("Panel.background", Color.green);
UI.put("OptionPane.background",Color.yellow);
ImageIcon Aphone = new ImageIcon("Aphone.png");
JOptionPane.showMessageDialog(null,"Pick Your Aphone!","Pat's
Worthless Cellular",JOptionPane.INFORMATION_MESSAGE,Aphone);
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, "What item
would you like to purchase, choose number: ",
"Pat's Worthless Cellular",
JOptionPane.PLAIN_MESSAGE );
//get selected option
MenuList = Integer.parseInt(MenuString);
double price = 0;
if(MenuList==1) { //if option is 1 then go for purchase
program
System.out.println();
String[] model = {"Aphone1","Aphone2"};
String[] color = {"Red","Black","Green"};
String[] storage = {"64GB","128GB"};
String[] acc = {"Airpods","Cover"};
String[] payment = {"24 Month Payment Plan","Cash"};
myOrder = "";
String.format("%.2f", price);
//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,
"Aphone1");
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,
"Cover");
String pay = (String) JOptionPane.showInputDialog(null,"Select
payment method","",JOptionPane.QUESTION_MESSAGE, null, payment,
"Cash");
//here it calculate total price of item
if(Model=="Aphone1")
price+=239.99;
else if(Model=="Aphone2")
price+=399.99;
else
price+=0.00;
if(Storage=="64GB")
price+=59.99;
else if(Storage=="128GB")
price+=99.99;
else
price+=0.00;
if(Acc=="Airpods")
price+=199.99;
else if(Acc=="Cover")
price+=39.99;
else
price+=0.00;
if(pay=="Cash")
price+=0.00;
else
price/=24.00;
DecimalFormat dec = new DecimalFormat("%.2f");
//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
}
}
For the option of 24 month payment plan ==>the price was set to ==>price= price/24.00 ==>before this statement the precision was upto 2 decimal places but after the execution of above statement mentioned the price variable has precision of more than 15 decimal places ............before this statement the execution of statements like price +=99.99 ==>this does not change the precision as only 2 decimal addition is taking place so for these statement the precision remains same ..
One possible way could be round off the price to 2 decimal places by multiplying the price by 100 and then convert it to int which will remove the extra decimal places ...and again convert back the int to by dividing it by 100 ..
the working code is shown below
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package market;
/**
*
* @author vijay
*/
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.text.DecimalFormat;
public class Market {
public static void main(String[] args) {
int MenuList;
UIManager UI=new UIManager();
UI.put("OptionPane.background", new Color(255, 0, 0));
UI.put("Panel.background", Color.green);
UI.put("OptionPane.background",Color.yellow);
ImageIcon Aphone = new ImageIcon("Aphone.png");
JOptionPane.showMessageDialog(null,"Pick Your Aphone!","Pat's Worthless Cellular",JOptionPane.INFORMATION_MESSAGE,Aphone);
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, "What item would you like to purchase, choose number: ",
"Pat's Worthless Cellular",
JOptionPane.PLAIN_MESSAGE );
//get selected option
MenuList = Integer.parseInt(MenuString);
double price = 0;
if(MenuList==1) { //if option is 1 then go for purchase program
System.out.println();
String[] model = {"Aphone1","Aphone2"};
String[] color = {"Red","Black","Green"};
String[] storage = {"64GB","128GB"};
String[] acc = {"Airpods","Cover"};
String[] payment = {"24 Month Payment Plan","Cash"};
myOrder = "";
String.format("%.2f", price);
//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, "Aphone1");
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, "Cover");
String pay = (String) JOptionPane.showInputDialog(null,"Select payment method","",JOptionPane.QUESTION_MESSAGE, null, payment, "Cash");
//here it calculate total price of item
if(Model=="Aphone1")
price+=239.99;
else if(Model=="Aphone2")
price+=399.99;
else
price+=0.00;
if(Storage=="64GB")
price+=59.99;
else if(Storage=="128GB")
price+=99.99;
else
price+=0.00;
if(Acc=="Airpods")
price+=199.99;
else if(Acc=="Cover")
price+=39.99;
else
price+=0.00;
if(pay=="Cash")
price+=0.00;
else
price/=24.00;
DecimalFormat dec = new DecimalFormat("%.2f");
//Round off 2 decimal places
price=price*100; //multiply the price by 100
int price_round=(int)price; //typecast it to int
price=((double)price_round)/100; //typecase int back to double and divide it by 100
//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
}
}
Screenshot