Results 1 to 4 of 4
  1. #1
    Try2Live4God is offline Member
    Join Date
    Aug 2007
    Posts
    5
    Rep Power
    0

    Default New to the forum and with a question!!

    Just dropping by to say hello. I am taking a Java class this semester and may be needing help as I am a newb when it comes to Java.

    My professor gave an assignment to create a banking program and i'm running into trouble. The program is basically to prompt the user to enter an option to either deposit, withdraw, view balance or terminate the session. I have to use a switch statement in the while loop. The customer class is where all the calculations take place. Here is what I have so far.


    import java.io.*;
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;

    class Customer
    {
    //attributes
    String customerName;
    int customerAcct;
    double accountBalance, totalDeposits, totalWithdrawals;

    //methods
    void processDeposit(double depositAmount)
    {
    accountBalance += depositAmount;
    totalDeposits +=depositAmount;
    }


    boolean processWithdrawal(double withdrawalAmount)
    {
    if (withdrawalAmount <= accountBalance)
    {
    accountBalance -= withdrawalAmount;
    totalWithdrawals += withdrawalAmount;
    return true;
    }
    else
    {
    String errorMessage = "Insufficient Funds!! ";
    errorMessage += "Your current balance is " + accountBalance;

    JOptionPane.showMessageDialog(null, errorMessage, "Error", JOptionPane.ERROR_MESSAGE);
    return false;
    }
    }


    //constructor
    Customer()
    {
    customerName = JOptionPane.showInputDialog("Please enter the Customer's name");
    customerAcct = Integer.parseInt(JOptionPane.showInputDialog("Plea se enter the Customer's Account Number"));
    accountBalance = Double.parseDouble(JOptionPane.showInputDialog("Pl ease enter the starting balance"));
    totalDeposits= 0;
    totalWithdrawals=0;
    }

    }

    public class Banking
    {
    public Banking() {


    public static void main(String[] args) {

    //Housekeeping
    int depCount = 0, withdrawalCount = 0, choices;
    double startingBalance = 0, deposit = 0, withdrawal = 0;
    boolean validWithdrawal = true;

    DecimalFormat twoDigits = new DecimalFormat("0.00");

    //Processing
    Customer cust = new Customer(); //Instantiate customer object

    startingBalance = cust.accountBalance; //populates the starting balance field with accountBalance from customer class


    choices = Integer.parseInt(JOptionPane.showInputDialog("Menu :\nEnter 1 - For Deposit\nEnter 2 - For Withdrawal\nEnter3 - For Balance\nEnter 9 - To Terminate\n"));


    while (choices != 9)
    {

    switch(choices)
    {
    case 1:
    deposit = Double.parseDouble(JOptionPane.showInputDialog("Pl ease enter a deposit amount."));
    cust.processDeposit(deposit);
    break;

    case 2:

    withdrawal = Double.parseDouble(JOptionPane.showInputDialog("Pl ease enter a withdrawal."));
    cust.validWithdrawal(withdrawal);
    break;

    case 3:

    JOptionPane.showMessageDialog("Your remaining balance is:" + cust.accountBalance);
    break;


    default:
    JOptionPane.showMessageDialog(null,output,"not valid choice",JOptionPane.INFORMATION_MESSAGE);

    }

    }


    String output = "Customer Name : " + cust.customerName + "\n";
    output += "Customer Account # : " + cust.customerAcct + "\n";
    output += "Starting Balance : " + twoDigits.format(startingBalance) + "\n\n";
    output += "Total Deposit : " + twoDigits.format(cust.totalDeposits) + "\n";
    output += "Total Withdrawal : " + twoDigits.format(cust.totalWithdrawals) + "\n\n";
    output += "Closing Balance : " + twoDigits.format(cust.accountBalance) + "\n";

    JOptionPane.showMessageDialog(null, output, "Transaction Summary", JOptionPane.INFORMATION_MESSAGE);

    //System.exit(0);

    }
    }

    }


    It still seems to not work.....any help would be appreciated. Thanks!
    Last edited by Try2Live4God; 08-15-2007 at 05:58 AM. Reason: Added more code...

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The main method does not go inside any other method or constructor.
    It stands by itself as a method inside a class body, viz, the curley braces {}.
    Compile errors:
    Java Code:
    C:\jexp>javac bankingrx.java
    bankingrx.java:46: cannot find symbol
    symbol  : method validWithdrawal(double)
    location: class CustomerRx
                        cust.validWithdrawal(withdrawal);
                            ^
    bankingrx.java:49: cannot find symbol
    symbol  : method showMessageDialog(java.lang.String)
    location: class javax.swing.JOptionPane
                        JOptionPane.showMessageDialog("Your remaining balance is:" +
    
                                   ^
    bankingrx.java:53: cannot find symbol
    symbol  : variable output
    location: class BankingRx
                        JOptionPane.showMessageDialog(null,output,"not valid choice"
    ,
                                                           ^
    3 errors
    Comments in code below show how these were fixed.
    I changed the names of the classes so you can run this as–is.
    Java Code:
    import java.io.*;
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    
    public class BankingRx 
    {
        public BankingRx() {}
    
        public static void main(String[] args)
        {
            //Housekeeping
            int depCount = 0, withdrawalCount = 0, choices;
            double startingBalance = 0, deposit = 0, withdrawal = 0;
            boolean validWithdrawal = true;
    
            DecimalFormat twoDigits = new DecimalFormat("0.00");
    
            //Processing
            CustomerRx cust = new CustomerRx(); //Instantiate customer object
    
            // populates the starting balance field with
            // accountBalance from customer class
            startingBalance = cust.accountBalance;
    
            do
            {
                choices = Integer.parseInt(
                              JOptionPane.showInputDialog("Menu :\n" +
                                               "Enter 1 - For Deposit\n" +
                                               "Enter 2 - For Withdrawal\n" +
                                               "Enter3 - For Balance\n" +
                                               "Enter 9 - To Terminate\n"));
    
                switch(choices)
                {
                    case 1:
                        deposit = Double.parseDouble(
                                      JOptionPane.showInputDialog("Pl ease enter " +
                                                                  "a deposit amount."));
                        cust.processDeposit(deposit);
                        break;
                    case 2:
                        withdrawal = Double.parseDouble(
                                         JOptionPane.showInputDialog("Pl ease enter " +
                                                                     "a withdrawal."));
                        // CustomerRx class does not have this method.
    //                    cust.validWithdrawal(withdrawal);
                        break;
                    case 3:
                        // JOptionPane class does not have a method that takes
                        // these type of arguments.
    //                    JOptionPane.showMessageDialog("Your remaining balance is:" +
    //                                                   cust.accountBalance);
                        // Try this
                        JOptionPane.showMessageDialog(null,"Your remaining balance " +
                                                      "is:" + cust.accountBalance);
                        break;
                    default:
                        // Variable "output" has not been declared.
                        String output = "hello world";
                        JOptionPane.showMessageDialog(null,output,"not valid choice",
                                                      JOptionPane.INFORMATION_MESSAGE);
                }
    
                String output = "Customer Name : " + cust.customerName + "\n";
                output += "Customer Account # : " + cust.customerAcct + "\n";
                output += "Starting Balance : " + twoDigits.format(startingBalance) + "\n\n";
                output += "Total Deposit : " + twoDigits.format(cust.totalDeposits) + "\n";
                output += "Total Withdrawal : " +
                           twoDigits.format(cust.totalWithdrawals) + "\n\n";
                output += "Closing Balance : " + twoDigits.format(cust.accountBalance) + "\n";
    
                JOptionPane.showMessageDialog(null, output, "Transaction Summary",         
                                              JOptionPane.INFORMATION_MESSAGE);
    //            System.exit(0);
            } while (choices != 9);        
        }
    }
    
    class CustomerRx
    {
        // member variables in class scope
        String customerName;
        int customerAcct;
        double accountBalance, totalDeposits, totalWithdrawals;
    
        //constructor
        CustomerRx()
        {
            customerName = JOptionPane.showInputDialog("Please enter the Customer's name");
            customerAcct = Integer.parseInt(
                               JOptionPane.showInputDialog("Plea se enter the " +
                                                           "Customer's Account Number"));
             accountBalance = Double.parseDouble(
                                  JOptionPane.showInputDialog("Pl ease enter the " +
                                                              "starting balance"));
             totalDeposits= 0;
             totalWithdrawals=0;
        }
    
        //methods
        void processDeposit(double depositAmount)
        {
            accountBalance += depositAmount;
            totalDeposits +=depositAmount;
        }
    
        boolean processWithdrawal(double withdrawalAmount)
        {
            if (withdrawalAmount <= accountBalance)
            {
                accountBalance -= withdrawalAmount;
                totalWithdrawals += withdrawalAmount;
                return true;
            }
            else
            {
                String errorMessage = "Insufficient Funds!! ";
                errorMessage += "Your current balance is " + accountBalance;
    
                JOptionPane.showMessageDialog(null, errorMessage, "Error",
                                              JOptionPane.ERROR_MESSAGE);
                return false; 
            }
        }
    }

  3. #3
    Try2Live4God is offline Member
    Join Date
    Aug 2007
    Posts
    5
    Rep Power
    0

    Default

    Thanks a lot for the help on this. I will try to run this!

  4. #4
    Try2Live4God is offline Member
    Join Date
    Aug 2007
    Posts
    5
    Rep Power
    0

    Default

    Hey Hardwired,

    It worked! Thanks a bunch for your help!

    And out of curiosity.....is this program possible without a do while loop? Would a regular while loop have worked like I had in my original code??

Similar Threads

  1. New to the Forum
    By srock258 in forum Introductions
    Replies: 3
    Last Post: 11-03-2008, 05:19 AM
  2. New sub forum
    By tim in forum Suggestions & Feedback
    Replies: 3
    Last Post: 03-17-2008, 08:12 PM
  3. Welcome to our new forum: Forum Lobby
    By JavaForums in forum Forum Lobby
    Replies: 18
    Last Post: 02-07-2008, 05:40 AM
  4. To correct forum
    By Jman in forum Introductions
    Replies: 3
    Last Post: 01-18-2008, 02:33 AM
  5. New to the Forum
    By JavaJunkie1983 in forum New To Java
    Replies: 7
    Last Post: 01-14-2008, 06:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •