Prince Sadie
Prince Sadie

Reputation: 43

Java Currency Denomination Issue

I'm trying to teach myself Java and running into a little hiccup just two chapters into the book I got :P This is one from one of the exercises:

"Write a class that calculates and displays the conversion of an entered number of dollars into currency denominations---20s, 10s, 5s, and 1s."

I'm going on four hours of reading from 0 knowledge of coding thus far so hopefully this doesn't sound too simple a question to answer. I'm sure there's a much more efficient way of writing this entire thing, but my question pertains to how I can terminate the entire thing if the user answers "yes" or continue on with the revised version if they answer "no"?

Also any advice or guidance you guys can give me on learning Java would be much appreciated! Thanks for taking the time to read this

import javax.swing.JOptionPane;
public class Dollars
{
    public static void main(String[] args)
    {
        String totalDollarsString;
        int totalDollars;
        totalDollarsString = JOptionPane.showInputDialog(null, "Enter amount to be     converted", "Denomination Conversion", JOptionPane.INFORMATION_MESSAGE);
    totalDollars = Integer.parseInt(totalDollarsString);
    int twenties = totalDollars / 20;
    int remainderTwenty = (totalDollars % 20);
    int tens = remainderTwenty / 10;
    int remainderTen = (totalDollars % 10);
    int fives = remainderTen / 5;
    int remainderFive = (totalDollars % 5);
    int ones = remainderFive / 1;
    JOptionPane.showMessageDialog(null, "Total Entered is $" + totalDollarsString + "\n" + "\nTwenty Dollar Bills: " + twenties + "\nTen Dollar Bills: " + tens + "\nFive Dollar Bills: " + fives + "\nOne Dollar Bills: " + ones);
    int selection;
    boolean isYes, isNo;
    selection = JOptionPane.showConfirmDialog(null,
        "Is this how you wanted the total broken down?", "Select an Option", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    isYes = (selection == JOptionPane.YES_OPTION);
        JOptionPane.showMessageDialog(null, "You responded " + isYes + "\nThanks for your response!");
        isNo = (selection == JOptionPane.NO_OPTION);
        int twenties2 = totalDollars / 20;
        int tens2 = totalDollars / 10;
        int fives2 = totalDollars / 5;
        int ones2 = totalDollars / 1;
        JOptionPane.showMessageDialog(null, "Total Entered is $" + totalDollarsString + "\n" + "\nTwenty Dollar Bills: " + twenties2 + "\nTen Dollar Bills: " + tens2 + "\nFive Dollar Bills: " + fives2 + "\nOne Dollar Bills: " + ones2);
}
}

Upvotes: 4

Views: 4106

Answers (1)

sarcan
sarcan

Reputation: 3165

First of all, you don't really seem to need the two booleans for isYes and isNo. Basically you asking the user whether he wants a different solution, that is one true/false value (or rather: isNo is the same as !isYes since the option pane will only return one of the values YES_OPTION and NO_OPTION).

What you'll want to do next is go to your 'refined' version if the user indicates that the first output was not what he wanted:

int selection = JOptionPane.showConfirmDialog(null,
        "Is this how you wanted the total broken down?", "Select an Option", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (selection == JOptionPane.NO_OPTION) {            
  int twenties2 = totalDollars / 20;
  int tens2 = totalDollars / 10;
  int fives2 = totalDollars / 5;
  int ones2 = totalDollars / 1;
  JOptionPane.showMessageDialog(null, "Total Entered is $" + totalDollarsString + "\n" + "\nTwenty Dollar Bills: " + twenties2 + "\nTen Dollar Bills: " + tens2 + "\nFive Dollar Bills: " + fives2 + "\nOne Dollar Bills: " + ones2);
}

If the user selects 'Yes', your main method is done anyway, so there's no need to do anything in this case.

Upvotes: 1

Related Questions