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;
/** * 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 */
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
/** * * 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 */
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
/** * * 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 */
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
/** * * 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); } }
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.
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
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.