The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Sales Tax Grand Total

8 replies on 1 page. Most recent reply: Apr 19, 2004 6:42 AM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Java Sales Tax Grand Total Posted: Apr 13, 2004 8:49 PM
Reply to this message Reply
Advertisement
Im writing a java program that you enter a subtotal then the subtotals percentage is calculated and a grandtotal aswell all in joptionpane. Whta needs to be done is ya need to

also when ya enter an invalid amount a pane should come up saying it is invalid and alow you to enter a valid amount

include a while loop to continue entry

include a while loop and try/catch statement for data validation

user numberformat class and object methods to format output

include a private static method that parses the subtotal(inputstring) entry and returns a valid amount

now i have coded the whole program but whats not happening is my joptions pane are not ending gracefully alos im having trouble coding the private static method alos the real problem is the setup of the code heres my code so far
package program3;

import java.text.NumberFormat;
import javax.swing.JOptionPane;

/**
* Benjamin Shively
* Java Programming I
* Program 3
* Due Date: April 16, 2004
* Program - Calculates Sales Tax on an Entry and Displays In A JOptionPane The
* Entry, Sales Tax, and Total
*/

public class Program3st {
public static void main(String[] args) {
double subTotal = 0;
double salestax = .0785;
String inputString = " ";
NumberFormat sub = NumberFormat.getCurrencyInstance();
NumberFormat per = NumberFormat.getCurrencyInstance();
NumberFormat gran = NumberFormat.getCurrencyInstance();
per.setMaximumFractionDigits(2);
gran.setMaximumFractionDigits(2);
while (!(inputString.equalsIgnoreCase("x"))) {
inputString = JOptionPane.showInputDialog(null,
"Enter Subtotal: ",
"Benjamin Shively",
JOptionPane.QUESTION_MESSAGE);
subTotal = parseSubTotal(inputString);
inputString = JOptionPane.showInputDialog(null, "SubTotal: \t" +
sub.format(subTotal) +
"\nTax: \t" +
per.format(subTotal * salestax) +
"\nGrand Total: \t" +
gran.format(subTotal * salestax + subTotal) +
"\nPress Enter to continue or 'x' to exit.",
"",
JOptionPane.
INFORMATION_MESSAGE);
}
System.exit(0);
}
private static double parseSubTotal(String inputString) {
double subTotal = 0;
String totalString = " ";
boolean tryagain = true;
while(tryagain){
try {
subTotal = Double.parseDouble(totalString);
}
catch (NumberFormatException e) {
inputString = JOptionPane.showInputDialog(null, "Invalid Entry: \t" +
totalString +
"\n" +
"Please Enter A Number:",
"",
JOptionPane.ERROR_MESSAGE);
}
tryagain = false;
}
return subTotal;
}
}


Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Re: Java Sales Tax Grand Total Posted: Apr 14, 2004 5:44 PM
Reply to this message Reply
I redid my code a bit and heres whats its not doing

When an invalid entry is entered ya get an error msg thats fine but i need to beable to enter a valid entry from the invalid output sceen and have it checked

in my valid out put sceen i need to beable to press enter to return to input when i press enter it just exits


package program3;

import java.text.NumberFormat;
import javax.swing.JOptionPane;

/**
*
* Java Programming I
* Program 3
* Due Date: April 16, 2004
* Program - Calculates Sales Tax on an Entry and Displays In A JOptionPane The
* Entry, Sales Tax, and Total
*/

public class Program3st {
public static void main(String[] args) {
String inputString = " ";
try {
while (!(inputString.equalsIgnoreCase("x"))) {
inputString = JOptionPane.showInputDialog(null,
"Enter Subtotal: ",
"",
JOptionPane.QUESTION_MESSAGE);
double subTotal = parseSubTotal(inputString);
}
}
catch (NullPointerException e) {
System.exit(0);
}
}
private static double parseSubTotal(String inputString) {
double subTotal = 0;
double salestax = .0785;
try {
NumberFormat sub = NumberFormat.getCurrencyInstance();
NumberFormat per = NumberFormat.getCurrencyInstance();
NumberFormat gran = NumberFormat.getCurrencyInstance();
per.setMaximumFractionDigits(2);
gran.setMaximumFractionDigits(2);
subTotal = Double.parseDouble(inputString);
inputString = JOptionPane.showInputDialog(null, "SubTotal: \t" +
sub.format(subTotal) +
"\nTax: \t" +
per.format(subTotal * salestax) +
"\nGrand Total: \t" +
gran.format(subTotal * salestax + subTotal) +
"\nPress Enter to continue or 'x' to exit.",
"",
JOptionPane.
INFORMATION_MESSAGE);
}
catch (NumberFormatException e) {
inputString = JOptionPane.showInputDialog(null, "Invalid Entry: \t" +
inputString +
"\n" +
"Please Enter A Number:",
"",
JOptionPane.ERROR_MESSAGE);
}
catch (NullPointerException e) {
System.exit(0);
}
System.exit(0);
return subTotal;
}
}

any help would be appreciated

Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Re: Help Needed Java Programming Posted: Apr 14, 2004 9:11 PM
Reply to this message Reply
now the only problem im having is when i come to my output screen when i press enter the program ends i need it to return to the inputpane heres my code


package program3;

import java.text.NumberFormat;
import javax.swing.JOptionPane;

/**
*
* Java Programming I
* Program 3
* Due Date: April 16, 2004
* Program - Calculates Sales Tax on an Entry and Displays In A JOptionPane The
* Entry, Sales Tax, and Total
*/

public class Program3st {
public static void main(String[] args) {
try {
String choose = " ";
while (!(choose.equalsIgnoreCase("x"))) {
String inputString = JOptionPane.showInputDialog(null,
"Enter Subtotal: ",
"",
JOptionPane.QUESTION_MESSAGE);
double subTotal = parseSubTotal(inputString);
}
System.exit(0);
}
catch (NullPointerException e) {
System.exit(0);
}
}
private static double parseSubTotal(String inputString) {
double subTotal = 0;
double salestax = .0785;
boolean retry = true;
while(retry) {
try {
NumberFormat sub = NumberFormat.getCurrencyInstance();
NumberFormat per = NumberFormat.getCurrencyInstance();
NumberFormat gran = NumberFormat.getCurrencyInstance();
per.setMaximumFractionDigits(2);
gran.setMaximumFractionDigits(2);
subTotal = Double.parseDouble(inputString);
inputString = JOptionPane.showInputDialog(null, "SubTotal: \t" +
sub.format(subTotal) +
"\nTax: \t" +
per.format(subTotal * salestax) +
"\nGrand Total: \t" +
gran.format(subTotal * salestax + subTotal) +
"\nPress Enter to continue or 'x' to exit.",
"",
JOptionPane.
INFORMATION_MESSAGE);

retry = false;
}
catch (NumberFormatException e) {
inputString = JOptionPane.showInputDialog(null, "Invalid Entry: \t" +
inputString +
"\n" +
"Please Enter A Number:",
"",
JOptionPane.ERROR_MESSAGE);


}
catch (NullPointerException e) {
System.exit(0);
}

}
System.exit(0);
return subTotal;
}
}


thx in advance

Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Re: Java Sales Tax Grand Total Posted: Apr 14, 2004 11:36 PM
Reply to this message Reply
heres my code looks alot better i recoded it still the same problem tho when i come to the output display pane i need to press enter and return to the input pane but it just ends i think im overlooking something

Thx in advance for help

package program3;

import java.text.NumberFormat;
import javax.swing.JOptionPane;

/**
*
* Java Programming I
* Program 3
* Due Date: April 16, 2004
* Program - Calculates Sales Tax on an Entry and Displays In A JOptionPane The
* Entry, Sales Tax, and Total
*/




public class Program3st {
public static void main(String[] args) {
double salestax = .0785;
String choice = " ";
String inputString = " ";
while (!(choice.equalsIgnoreCase("x"))) {
inputString = JOptionPane.showInputDialog(null,
"Enter Subtotal: ",
"",
JOptionPane.QUESTION_MESSAGE);
double subTotal = parseSubTotal(inputString);

NumberFormat sub = NumberFormat.getCurrencyInstance();
NumberFormat per = NumberFormat.getCurrencyInstance();
NumberFormat gran = NumberFormat.getCurrencyInstance();
per.setMaximumFractionDigits(2);
gran.setMa ximumFractionDigits(2);
inputString = JOptionPane.showInputDialog(null, "SubTotal: \t" +
sub.format(subTotal) +
"\nTax: \t" +
per.format(subTotal * salestax) +
"\nGrand Total: \t" +
gran.format(subTotal * salestax + subTotal) +
"\nPress Enter to continue or 'x' to exit.",
"",
JOptionPane.
INFORMATION_MESSAGE);
}
}




private static double parseSubTotal(String validString) {
double subTotal = 0;
boolean retry = true;
while(retry) {
try {
subTotal = Double.parseDouble(validString);
retry = false;
}
catch (NumberFormatException e) {
validString = JOptionPane.showInputDialog(null, "Invalid Entry: \t" +
validString +
"\n" +
"Please Enter A Number:",
"",
JOptionPane.ERROR_MESSAGE);
}
catch (NullPointerException e) {
System.exit(0);
}
}
return subTotal;
}
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Java Sales Tax Grand Total Posted: Apr 15, 2004 6:52 AM
Reply to this message Reply
Try using the java markup tags described in the gray Formatting Your Post box to the right, when you are posting your message.

Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Re: Java Sales Tax Grand Total Posted: Apr 15, 2004 11:10 AM
Reply to this message Reply
Heres my code does everything i want it too just wanted to post it up see if maybe theres any room for improvement

thx for all the help






package program3;

 import java.text.NumberFormat;
import javax.swing.JOptionPane;
 
/**

 */
 
public class Program3st
{
  public static void main(String[] args)
  {      // begin defined variables and Strings (1)
  double subTotal = 0;
  double salestax = .0785;
  String choice = " ";
  String inputString = " ";
             // End variables and Strings (1)
 
  try
               // start try block (1)
  {
  while (!(inputString.equalsIgnoreCase("x")))
                  // begin while loop (1) for continued user entry
  {
  inputString = JOptionPane.showInputDialog(null,
                                              "Enter Subtotal: ",
                                              "",
                                             JOptionPane.QUESTION_MESSAGE);
        subTotal = parseSubTotal(inputString);
          NumberFormat sub = NumberFormat.getCurrencyInstance();
            NumberFormat per = NumberFormat.getCurrencyInstance();
               NumberFormat gran = NumberFormat.getCurrencyInstance();
                 per.setMaximumFractionDigits(2);
                    gran.setMaximumFractionDigits(2);
      inputString = JOptionPane.showInputDialog(null, "SubTotal:   \t" +
                                        sub.format(subTotal) +
                                        "\nTax:   \t" +
                                        per.format(subTotal * salestax) +
                                        "\nGrand Total:  \t" +
                                        gran.format(subTotal * salestax + subTotal) +
                                        "\nPress Enter to continue or 'x' to exit.",
                                        "",
                                        JOptionPane.
                                        INFORMATION_MESSAGE);
   } // end while loop (1)
 
}        // end try block (1)
 
catch (NullPointerException e)
            // begin catch (NP)exception block (1)
{
       System.exit(0);
 
  }            // end catch exception block (1)
 
       System.exit(0);
}
private static double parseSubTotal(String validString)
                    // begin private static method for data validation
{
                       // begin defined variables (2)
double subTotal = 0;
boolean retry = true;
                     // end variables (2)
while(retry)
                        // begin while loop (2) for re-entry
{
try
                           // start try block (2)
{
     subTotal = Double.parseDouble(validString);
           retry = false;
}                             // end try block (2)
 
catch (NumberFormatException e)
                                // begin catch block for (NF)exception
{
       validString = JOptionPane.showInputDialog(null, "Invalid Entry:  \t" +
                                                        validString +
                                                        "\n" +
                                                        "Please Enter A Number:",
                                                        "",
                                                        JOptionPane.ERROR_MESSAGE);
}              // end catch block for (NF)exception
catch (NullPointerException e)
                     // begin catch block for (NP)exception (2)
{
    System.exit(0);
    }                     // end catch block for (NP)exception (2)
}
return subTotal;
  }
}

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Java Sales Tax Grand Total Posted: Apr 16, 2004 5:35 AM
Reply to this message Reply
I'm just curious, is this for a class or are you working through some book on your own?

As far as I can see, your code is fine. But it appears that you are being taught procedural programming in an object-oriented language. If you plan on continuing on in Java, you will have to unlearn what you are doing to learn how to design programs using an object-oriented approach.

If this is a class, then you are stuck, because you want to get a good grade, even if you are being taught the wrong things. But, if this is something that you are doing on your own, you should get a different book - one that focuses on OOP.

twc

Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Re: Java Sales Tax Grand Total Posted: Apr 17, 2004 8:55 PM
Reply to this message Reply
Its for a class......... we are kind of bouncing all around in the course like we did if/else and while loops at the beginning of the course now we are doing it again

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Java Sales Tax Grand Total Posted: Apr 19, 2004 6:42 AM
Reply to this message Reply
My biggest concern is that it appears that you are being taught to design programs in a manner that is inappropriate for the language that you are learning. I realize that at this stage of your education, you cannot know the difference, but it is significant.

Just keep this in mind - putting your whole program in the main method is NOT good. A baby wears a diaper because it doesn't know any better. You are using the main method for your whole program because you don't know any better - yet. Unlike the baby and the diaper, you will always need a main method, but most of my main methods have one or two lines in them. Everything else is factored out to other classes that define objects. You shouldn't need much more than that in a main method.

twc

Flat View: This topic has 8 replies on 1 page
Topic: Problem in JTable.. pls help me.. Previous Topic   Next Topic Topic: File I/O problems

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use