简体   繁体   中英

Return to method & passing variables between methods?

I'm currently doing an assignment regarding an ATM machine that I have to program. There are 4 methods in total - MAIN, WITHDRAWAL, DEPOSIT and CHECKBALANCE. From the MAIN method, I have a menu and therefore would be sending variables to the rest of the methods from there.

I would like to know if there is a way, if after I finish calculating my total in WITHDRAWAL, that I can pass that value to CHECKBALANCE? Because I have tried:

total = deposit - withdrawal;
checkbalance(withdrawal);

For the user to check their balance, they have to get the value calculated in their previous choice sent to the CHECKBALANCE method right? But the error that popped up was "arguements list differ in length". Also, I would like to know how to return back to the menu in MAIN after I finish each task performed in the other methods. I have tried:

choice = Integer.parseInt(JOptionPane.showInputDialog("Would you like to return to the    menu? (1 = Yes, 2 = No));

if (choice == 2)
{
System.exit(0);
}

main();

I would appreciate it VERY much if any one of you could lend me a helping hand. I'm still new in this and just started learning java this semester. Please and thank you to everybody :)

UPDATES:

package lab2;

import javax.swing.JOptionPane;
public class LAB2 
{

    public static void main(String[] args) 
    {
        double initialAmount, withdraw = 0, deposit = 0, checkBalance = 0;
        int choice;

        initialAmount = Double.parseDouble(JOptionPane.showInputDialog("Please enter current savings amount: "));

        while(initialAmount < 0)
        {
            initialAmount = Double.parseDouble(JOptionPane.showInputDialog("Invalid amount. Amount cannot be negative. Please re-enter" ));   
        }

        choice = Integer.parseInt(JOptionPane.showInputDialog("MENU: "
                + "What would you like to do? Please choose from 1 - 4\n"
                + "1) Withdraw\n"
                + "2) Deposit\n"
                + "3) Check balance\n"
                + "4) Exit"));
        while ((choice <= 0) || (choice > 4))
        {
            choice = Integer.parseInt(JOptionPane.showInputDialog("Invalid option. Please choose between option 1 - 4: "));
        }

        if (choice == 1)
        {
            ChoiceOne(withdraw, initialAmount);
        }

        else if (choice == 2)
        {
            ChoiceTwo(deposit, initialAmount);
        }
        else if (choice == 3)
        {
            ChoiceThree(checkBalance, initialAmount);
        }

        else if (choice == 4)
        {
            System.exit(0);
        }
    }

    /////////////////////////////////////////////////////////////////////////////
    public static void ChoiceOne(double withdraw, double initialAmount)
    {
        double afterDeduction;
        int input, choice1;

        withdraw = Double.parseDouble(JOptionPane.showInputDialog("Your current balance is: RM"+initialAmount+" . How much would you like to withdraw? "));

        while (withdraw > initialAmount)
        {
            input = Integer.parseInt(JOptionPane.showInputDialog("Insufficient balance to perform withdrawal. Continue? (1 = Yes, 2 = Exit ")); 

            if (input == 2)
            {
                JOptionPane.showMessageDialog(null,"Thank you!");
                System.exit(0);
            }  
            else if (input == 1)
            {
                withdraw = Double.parseDouble(JOptionPane.showInputDialog("Your current balance is: RM"+initialAmount+" . How much would you like to withdraw? "));

                afterDeduction = initialAmount - withdraw;
                JOptionPane.showMessageDialog(null, "You have withdrawn RM"+withdraw+". Your total balanace now is: RM"+afterDeduction+".");

                choice1 = Integer.parseInt(JOptionPane.showInputDialog("Would you like to continue to menu or exit? (1 = Yes /2 = No"));

                if (choice1 == 2)
                {
                    JOptionPane.showMessageDialog(null,"Thank you!");
                    System.exit(0);
                }

                if (choice1 == 1)
                {

                }
            }
        }

        while  (withdraw < 0)
        {
            input = Integer.parseInt(JOptionPane.showInputDialog("Invalid withdrawal. Please re-enter: "));
        }

        afterDeduction = initialAmount - withdraw;
        JOptionPane.showMessageDialog(null,"You have withdrawn RM"+withdraw+". Your total balanace now is: RM"+afterDeduction+"."
                + "/nWould you like to return to the menu? ");
    }

    /////////////////////////////////////////////////////////////////////////////
    public static void ChoiceTwo (double deposit, double initialAmount)
    {
        double afterAddition; 
        int input;

        deposit = Double.parseDouble(JOptionPane.showInputDialog("How much would you like to deposit? "));

        while (deposit < 0)
        {
            input = Integer.parseInt(JOptionPane.showInputDialog("Invalid deposit. Please re-enter: "));
        }

        if (deposit > 0)
        {
        afterAddition = deposit + initialAmount;
        JOptionPane.showMessageDialog(null,"You have deposit RM"+deposit+". Your total balanace now is: RM"+afterAddition+"."
                + "/nWould you like to return to the menu? ");
        }
    }

    /////////////////////////////////////////////////////////////////////////////
    public static void ChoiceThree (double CheckBalance, double initialAmount)
    {
    }

}

You can create new object, let's call it SessionData and store all operation data in it. It could significantly simplify argument passing issues. That's all I can advise basing on information provided by you.

我只看一小段代码就无法完全理解您的工作,但是,如果布尔值等于true,则可以尝试使用布尔值检查用户是否要返回主菜单,然后显示如果主菜单等于false,则程序继续执行/结束。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM