Adding an Accumulator to Summarize an Order

I need to add an accumulator to count Books, Movies, and CDs

Page 1 of 1

2 Replies - 2010 Views - Last Post: 03 April 2010 - 08:48 AM Rate Topic: -----

#1 Guest_Willbo*


Reputation:

Adding an Accumulator to Summarize an Order

Posted 02 April 2010 - 09:24 PM

Hello Everybody,

I'm writing a program that is used to allow a person to shop for Books, Movies, and CDs while adding a rate of tax and displaying a subtotal (price before tax) and a total. I have gotten through the majority of the program but cannot figure out how to properly add an accumulator to create a proper summary at the end that shows how many of whatever item was bought and what the average price for the amount of items is plus the tax (based on the tax rate given at the beginning). Im working with JOptionPane so it uses GUI. Do I need multiple methods for each item counted? For example a method for books, a method for movies, and a method for CDs then a method for Tax and Total Etc?

Thanks for the advice!

Here's an Example of what the Summary is supposed to look like:

Order Summary:
2 Books(s) $ 28.28 / Average $ 14.14
0 Movie(s) $ 0.00 / Average $ 0.00
1 CD(s) $ 21.30 / Average $ 21.30

Subtotal: $ 49.58
Tax: $ 1.24
Total: $ 50.82
Average Cost Per Item: $ 16.94

Would you like to place another order? (y/n) n



import javax.swing.JOptionPane;


public class Program3 {

public static void main(String[] args) {
        	Double subTotal = 0.0;
        	int selection = 0;

        	JOptionPane.showMessageDialog(null, "This is order processing software.");

        	do { // for orders
                	Double taxPercent = getTax();

                	do { // for purchases
                        	int select = getMainSelection(); // get user category selection

                        	int purchase = getPurchase(select); // get users purchases

                        	subTotal += purchase; // store in total variable
                	} while (JOptionPane.showConfirmDialog(null, "Would you like another item (y/n)") == JOptionPane.YES_OPTION);

                	Double taxTotal = getTaxAmt(subTotal, taxPercent);
                	Double overallPrice = calcPrice(subTotal, taxTotal); // calculate total price

        	} while (JOptionPane.showConfirmDialog(null, "Another order? (y/n)") == JOptionPane.YES_OPTION);
        	
        	JOptionPane.showMessageDialog(null, I need to know how to do the accumulators correctly so the Summary can go where this message is! Thanks for the help!!");
        	
        	
        	System.out.println("end of program");
}
/* ----------------------------------------------------------------------------------
      Methods to call are all below this line, actual program execution is above this line **/

private static Double getTaxAmt(Double subTotal, Double taxPercent) {

        	return null;
}

private static Double calcPrice(Double subTotal, Double taxTotal) {

        	return null;
}

private static int getPurchase(int mainSelection) {
        	int purchase = 0;

        	if (mainSelection == 1) { // books
                	purchase = Integer.parseInt(JOptionPane.showInputDialog(
                                	"\n 1. Harry Potter                 $ 19.99" +
                                	"\n 2. Inkheart                     $ 8.29"  +
                                	"\n 3. The Hobbit                   $ 12.50" +
                                	"\n 4. Howl's Moving Castle         $ 12.50" +
                                	"\n Choose an item:"));
        	}
        	else if(mainSelection == 2) { // movies
                	purchase = Integer.parseInt(JOptionPane.showInputDialog(
                                	"\n 1. The Little Mermaid                $ 16.89" +
                                	"\n 2. Aladdin                           $ 14.33" +
                                	"\n 3. The Lion King                     $ 14.62" +
                                	"\n 4. Beauty and the Beast              $ 17.70" +
                                	"\n Choose an item:"));
        	}
        	else if(mainSelection == 3){ // music
                	purchase = Integer.parseInt(JOptionPane.showInputDialog(
                                	"\n 1. Hanson / Middle of Nowhere            $ 19.99" +
                                	"\n 2. Iron Maiden / Live After Death        $ 23.19" +
                                	"\n 3. Metallica / Metallica                 $ 15.00" +
                                	"\n 4. Lady Gaga / The Fame Monster          $ 29.15" +
                                	"\n Choose an item:"));
        	}
        	else if(mainSelection != 1 && mainSelection != 2 && mainSelection !=3 && mainSelection !=4){
                	purchase = Integer.parseInt(JOptionPane.showInputDialog(
                                	"\n1. Harry Potter                 $ 19.99" +
                                	"\n2. Inkheart                     $ 8.29"  +
                                	"\n3. The Hobbit                   $ 12.50" +
                                	"\n4. Howl's Moving Castle         $ 12.50" +
                                	" Choose an item: "));
        	}
          
        	return purchase;
}


private static int getMainSelection() {
        	int mainSelection = Integer.parseInt(JOptionPane.showInputDialog(" 1. Books" +
                        	  		"\n 2. Movies" +
                        	  		"\n 3. Music" +
                        			"\n Choose one:"));
        	while (mainSelection < 0 || mainSelection > 3) { // validate
                	mainSelection = Integer.parseInt(JOptionPane.showInputDialog(" 1. Books" +
                                	"\n 2. Movies" +
                                	"\n 3. Music" +
                                	"\n Choose one:"));
        	}

        	return mainSelection;
}

private static double getTax() {
        	Double tax = Double.parseDouble(JOptionPane.showInputDialog("What is the tax rate for this transaction (in percent)?"));

        	while (tax < 0.0 || tax > 100.0) { // validate
                	tax = Double.parseDouble(JOptionPane.showInputDialog("What is the tax rate for this transaction (in percent)?"));
        	}

        	return tax;




}




}
   


Is This A Good Question/Topic? 0

Replies To: Adding an Accumulator to Summarize an Order

#2 Guest_Willbo*


Reputation:

Re: Adding an Accumulator to Summarize an Order

Posted 02 April 2010 - 09:26 PM

This may be a little easier to read without the code box. :)

import javax.swing.JOptionPane;


public class Program3 {

public static void main(String[] args) {
        	Double subTotal = 0.0;
        	int selection = 0;

        	JOptionPane.showMessageDialog(null, "This is order processing software.");

        	do { // for orders
                	Double taxPercent = getTax();

                	do { // for purchases
                        	int select = getMainSelection(); // get user category selection

                        	int purchase = getPurchase(select); // get users purchases

                        	subTotal += purchase; // store in total variable
                	} while (JOptionPane.showConfirmDialog(null, "Would you like another item (y/n)") == JOptionPane.YES_OPTION);

                	Double taxTotal = getTaxAmt(subTotal, taxPercent);
                	Double overallPrice = calcPrice(subTotal, taxTotal); // calculate total price

        	} while (JOptionPane.showConfirmDialog(null, "Another order? (y/n)") == JOptionPane.YES_OPTION);
        	
        	JOptionPane.showMessageDialog(null, I need to know how to do the accumulators correctly so the Summary can go where this message is! Thanks for the help!!");
        	
        	
        	System.out.println("end of program");
}
/* ----------------------------------------------------------------------------------
      Methods to call are all below this line, actual program execution is above this line **/

private static Double getTaxAmt(Double subTotal, Double taxPercent) {

        	return null;
}

private static Double calcPrice(Double subTotal, Double taxTotal) {

        	return null;
}

private static int getPurchase(int mainSelection) {
        	int purchase = 0;

        	if (mainSelection == 1) { // books
                	purchase = Integer.parseInt(JOptionPane.showInputDialog(
                                	"\n 1. Harry Potter                 $ 19.99" +
                                	"\n 2. Inkheart                     $ 8.29"  +
                                	"\n 3. The Hobbit                   $ 12.50" +
                                	"\n 4. Howl's Moving Castle         $ 12.50" +
                                	"\n Choose an item:"));
        	}
        	else if(mainSelection == 2) { // movies
                	purchase = Integer.parseInt(JOptionPane.showInputDialog(
                                	"\n 1. The Little Mermaid                $ 16.89" +
                                	"\n 2. Aladdin                           $ 14.33" +
                                	"\n 3. The Lion King                     $ 14.62" +
                                	"\n 4. Beauty and the Beast              $ 17.70" +
                                	"\n Choose an item:"));
        	}
        	else if(mainSelection == 3){ // music
                	purchase = Integer.parseInt(JOptionPane.showInputDialog(
                                	"\n 1. Hanson / Middle of Nowhere            $ 19.99" +
                                	"\n 2. Iron Maiden / Live After Death        $ 23.19" +
                                	"\n 3. Metallica / Metallica                 $ 15.00" +
                                	"\n 4. Lady Gaga / The Fame Monster          $ 29.15" +
                                	"\n Choose an item:"));
        	}
        	else if(mainSelection != 1 && mainSelection != 2 && mainSelection !=3 && mainSelection !=4){
                	purchase = Integer.parseInt(JOptionPane.showInputDialog(
                                	"\n1. Harry Potter                 $ 19.99" +
                                	"\n2. Inkheart                     $ 8.29"  +
                                	"\n3. The Hobbit                   $ 12.50" +
                                	"\n4. Howl's Moving Castle         $ 12.50" +
                                	" Choose an item: "));
        	}
          
        	return purchase;
}


private static int getMainSelection() {
        	int mainSelection = Integer.parseInt(JOptionPane.showInputDialog(" 1. Books" +
                        	  		"\n 2. Movies" +
                        	  		"\n 3. Music" +
                        			"\n Choose one:"));
        	while (mainSelection < 0 || mainSelection > 3) { // validate
                	mainSelection = Integer.parseInt(JOptionPane.showInputDialog(" 1. Books" +
                                	"\n 2. Movies" +
                                	"\n 3. Music" +
                                	"\n Choose one:"));
        	}

        	return mainSelection;
}

private static double getTax() {
        	Double tax = Double.parseDouble(JOptionPane.showInputDialog("What is the tax rate for this transaction (in percent)?"));

        	while (tax < 0.0 || tax > 100.0) { // validate
                	tax = Double.parseDouble(JOptionPane.showInputDialog("What is the tax rate for this transaction (in percent)?"));
        	}

        	return tax;




}

}


This post has been edited by pbl: 03 April 2010 - 08:48 PM
Reason for edit:: Code tags added

Was This Post Helpful? 0

#3 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Adding an Accumulator to Summarize an Order

Posted 03 April 2010 - 08:48 AM

For future reference, ALWAYS use the code box. It is MUCH easier to read. As the code box reveals, you a missing a quote, thus throwing off the entire program.

// missing a quote here:
JOptionPane.showMessageDialog(null, I need to know how to do the accumulators correctly so the Summary can go where this message is! Thanks for the help!!");



It needs to be this:
JOptionPane.showMessageDialog(null, "I need to know how to do the accumulators correctly so the Summary can go where this message is! Thanks for the help!!");


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1