Question

In: Computer Science

The problem I'm having with this code is that when you choose the 24 month payment...

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
}
}

Solutions

Expert Solution

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


Related Solutions

I'm having a problem getting my code to function correct, *num_stu keeps losing its value when...
I'm having a problem getting my code to function correct, *num_stu keeps losing its value when ever another function is called from the class. Can someone proof read this for me and let me know what'd up? Header.h file #include <iostream> using namespace std; class report {    private:        int* num_stu;         int xx;        int* id;        double* gpa;    public:                report(int x) {             num_stu = &x;             xx = x;            ...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do...
I'm having trouble understanding a CS assignment. I would appreciate it if you all code do this for me. The template for the lab is below which you must use. You're only supposed to create/edit the product function. The assignment has to be written in asm(Mips) You will need to create a NOS table and use the structure below and write a function called product. The structure used in this program is: struct Values { short left; short right; int...
Hello there, I'm wondering can you do this problem without arrays? Using the given code on...
Hello there, I'm wondering can you do this problem without arrays? Using the given code on C++ Platform. Let me know ASAP. #include <iostream> #include <time.h> using namespace std; void shoot(bool &targetAlive, double accuracy) { double random = (rand() % 1000) / 1000.; targetAlive = !(random < accuracy); } int startDuel(int initialTurn) { bool personAlive[3] = {true, true, true}; double accuracy[3] = {0.333, 0.5, 1.0}; int turn = initialTurn; // which person has to shoot, initialTurn represents the first person...
***The code is provided below*** When trying to compile the code below, I'm receiving three errors....
***The code is provided below*** When trying to compile the code below, I'm receiving three errors. Can I get some assistance on correcting the issues? I removed the code because I thought I corrected my problem. I used #define to get rid of the CRT errors, and included an int at main(). The code compiles but still does not run properly. When entering the insertion prompt for the call details, after entering the phone number, the program just continuously runs,...
I'm having problems with my java code not correctly spellchecking certain words. 1) Determine if a...
I'm having problems with my java code not correctly spellchecking certain words. 1) Determine if a word entered by the user is spelled correctly. A word is considered correct if it's found in dictionary.txt (see required output to get file). these are the input and output that are suppose to come out i already have the dictionary.txt i just don't know hoe to set it up someone please help me Standard Input                 Files in the same directory glimmer hello...
I'm having a very hard time getting this c++ code to work. I have attached my...
I'm having a very hard time getting this c++ code to work. I have attached my code and the instructions. CODE: #include using namespace std; void printWelcome(string movieName, string rating, int startHour, int startMinute, char ampm); //menu function //constants const double TICKET_PRICE = 9.95; const double TAX_RATE = 0.095; const bool AVAILABLE = true; const bool UNAVAILABLE = false; int subTotal,taxAdded, totalCost; // bool checkAvailability( int tickets, int& seatingLeft){ //checks ticket availability while(tickets > 0 || tickets <= 200) //valid...
I'm having trouble with validating this html code. Whenever I used my validator, it says I...
I'm having trouble with validating this html code. Whenever I used my validator, it says I have a stray end tag: (tbody) from line 122 to 123. It's the last few lines. Thanks and Ill thumbs up whoever can help solve my problem. Here's my code: <!DOCTYPE html> <html lang="en"> <head> <title>L7 Temperatures Fields</title> <!--    Name:    BlackBoard Username:    Filename: (items left blank for bb reasons    Class Section: (blank)    Purpose: Making a table to demonstrate my...
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone....
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone. Make this subclass have a no-arg constructor and a constructor that takes a name, email, and phone. Override the toString method to return the required output. Given Files: public class Demo1 { public static void test(Phone p) { System.out.println("Getter test:"); System.out.println(p.getName()); System.out.println(p.getNumber()); } public static void main(String[] args) { Phone test1 = new SmartPhone(); Phone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); System.out.println(test1); System.out.println(test2); System.out.println(test1);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT