#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Exclamation What is wrong with my code????


    Simple Banking program. Loops when selecting a procedure. How can I resolve this?

    import java.lang.*;
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
    import javax.swing.ImageIcon;
    import java.awt.Font;
    import java.awt.Color;
    import java.text.*;
    import java.util.Random; // need to load the necessary Class
    import javax.swing.*;
    //import javax.text.NumberFormat;

    public class Lab3b


    {


    static double balance = 0;
    static double newBalance = 0;
    static double adjustment = 0;



    public static void main (String[] args)
    {
    doIt();
    System.exit(0);

    } //main


    public static void doIt()
    {
    setUI();
    openingMessage();
    getChoice();
    int choice;
    boolean moreBankingBusiness;
    moreBankingBusiness = true;
    while (moreBankingBusiness) // easier to understand?
    {
    choice = getChoice();
    choice++; // I don't like working with zero
    switch (choice)
    {
    case 1:
    makeDeposit();
    break;

    case 2:
    makeWithdrawal();
    break;

    case 3:
    getBalance();
    break;

    default:
    moreBankingBusiness = false;
    }// switch

    balance = newBalance;
    }//while
    endingMessage();
    } //doIt code


    public static double getDouble()
    {
    double val;

    while (true)
    {
    String s = JOptionPane.showInputDialog ( "Enter Amount");
    if (s == null)
    {
    JOptionPane.showMessageDialog(null, "If you wish to Exit please select" +
    "the Exit Icon");
    break;
    }
    else
    if (s.equals(""))
    {
    JOptionPane.showMessageDialog
    (null, "You must make an entry in the InputBox");
    }
    try
    {
    val = Double.parseDouble (s);
    return val;
    }
    catch (Exception e)
    {
    JOptionPane.showMessageDialog(null, "Enter an amount in dollars");
    }//catch
    }//while
    return 0.0;
    }//getDouble




    public static void setUI()

    {

    UIManager um = new UIManager();
    um.put("OptionPane.messageForeground", Color.blue);
    um.put("Panel.background", Color.white);
    Font font1 = new Font("dialog",Font.BOLD,24);
    Font font2 = new Font("dialog",Font.BOLD,18);
    um.put("Label.font", font1);
    um.put("Button.font", font2);
    um.put("TitledBorder.font",font2);
    um.put("TextField.font",font1);

    }// setui

    public static int getChoice()
    {
    final int QUESTION_MESSAGE = 3;

    int mc = JOptionPane.QUESTION_MESSAGE;

    String[] opts = { "Deposit" , "Withdrawal","Check Balance","Exit" };
    int user = JOptionPane.showOptionDialog (null, "Select Your Banking Procedure",
    "Banking Options or Exit", 0, mc, null, opts, opts[1]);
    if (user >= 0 && user <=2)
    {
    return user;
    }
    else
    {
    user = -1;
    return user;
    }
    } //userChoice
    //int ch = JOptionPane.showOptionDialog (null, "Choose a Number",
    //"Number Options or Exit", 0, mc, null, opts, opts[1]);


    private static void makeDeposit()
    {
    adjustment = getDouble();
    JOptionPane.showInputDialog( "Enter the Deposit Amount" );
    newBalance = balance + adjustment;
    JOptionPane.showMessageDialog(null, "*** CCAC NATIONAL BANK ***\n\n" +
    "Old Balance is: " + balance + "\n" +
    "Adjustment is: +" + adjustment + "\n" +
    "New Balance is: " + newBalance + "\n");
    balance = newBalance;
    }

    private static void makeWithdrawal()
    {
    adjustment = Double.parseDouble(JOptionPane.showInputDialog( "Enter the Withdrawal Amount" ));
    newBalance = balance - adjustment;
    JOptionPane.showMessageDialog
    (null, "*** CCAC NATIONAL BANK ***\n\n" +
    "Old Balance is: " + balance + "\n" +
    "Adjustment is: -" + adjustment + "\n" +
    "New Balance is: " + newBalance + "\n");
    balance = newBalance;
    }

    private static void getBalance()
    {
    JOptionPane.showMessageDialog
    (null, "***CCAC NATIONAL BANK ***\n\n" +
    "Your Current Balance Balance is: " + balance );
    }

    public static void openingMessage()

    {
    JOptionPane.showMessageDialog(null," Thank You for banking with CCAC National Bank \n Please Continue ");


    }//opening message

    public static void endingMessage()

    {
    ImageIcon icon = new ImageIcon("borat.jpg");

    JOptionPane.showMessageDialog (null," Goodbye! ", "Exit Window",JOptionPane.INFORMATION_MESSAGE, icon);
    }//ending message




    }// end
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,535
    Rep Power
    1906
    Please use code tags when posting code, it will make the code easier to read.

    What do you mean with "Loops when selecting a procedure", where in the code is the issue?
    You should explain what you expect it to do.

IMN logo majestic logo threadwatch logo seochat tools logo