1

I am creating a program that has a ATM class in which there is a cheqings and savings account that can be changed based on what the user inputs in the main method in the starting class. For some reason when the user enters in the amount to deposit/withdraw from their account the math is done but the value is not stored. therefor when I go to display the account balance both accounts are at their starting values.

public class ATM {

private double cheqBal;
private double savingsBal;
private String bankName;

public ATM(double cheq, double savings, String name) {
    cheqBal = cheq;
    savingsBal = savings;
    bankName = name;
}

public double getCheq() {
    return cheqBal;
}
public double getSavings() {
    return savingsBal;
}
public String getName() {
    return bankName;
}

public void setCheq(double cheq) {
    cheqBal = cheq;
}
public void setSavings(double savings) {
    savingsBal = savings;
}
public void setName(String name) {
    bankName = name;
}

public double depositCheq(double cheq) {
    if (cheq < 0) {
        System.out.println("Sorry you cannot do that!");
    } else {
        cheqBal += cheq;
    }
    System.out.println(getCheq());
    return cheqBal;
}

There is the ATM class where the methods for depositing/withdrawing money are. I only showed the deposit chequing method.

double savBal = 500;
        double cheqBal = 1000;

        ATM bank1 = new ATM(cheqBal, savBal, "BMO");

        String userChoice = JOptionPane.showInputDialog("1: Deposit Cheqings \n2: Deposit Savings \n"
                + "3: Withdraw Cheqings \n4: Withdraw Savings \n5: Show Balance\n6: Exit");
        int choice = Integer.parseInt(userChoice);

        if (choice == 1) {
            String cheqIn = JOptionPane.showInputDialog("How much would you like to deposit?: ");
            bank1.depositCheq(Double.parseDouble(cheqIn));
            cheqBal = bank1.getCheq();
        } else if (choice == 2) {
            String savIn = JOptionPane.showInputDialog("How much would you like to deposit?: ");
            bank1.depositSavings(Double.parseDouble(savIn));
            savBal = bank1.getSavings();
        } else if (choice == 3) {
            String cheqOut = JOptionPane.showInputDialog("How much would you like to withdraw?: ");
            bank1.withdrawCheq(Double.parseDouble(cheqOut));
            cheqBal = bank1.getCheq();
        } else if(choice == 4){
            String savOut = JOptionPane.showInputDialog("How much would you like to withdraw?: ");
            bank1.withdrawSavings(Double.parseDouble(savOut));
            savBal = bank1.getSavings();
        }else if(choice == 5){
            bank1.toString();
        }else if(choice == 6){
            break;

There is the main method. When i hit 1 to deposit money it does not reiterate the amount I deposited when i hit 5 to show balance. (all of the main method is in a loop so it continues until exit).

Sorry for the large amount of code, hope you guys understand my problem and can help!

AlexAgla
  • 63
  • 1
  • 7
  • 1
    You can't change a value in the main method from another class. Java is pass-by-value, not pass-by-reference. You can only change member variables, i.e. things where you can access the same variable. – Andy Turner Oct 18 '16 at 15:23
  • 1
    You have to return the value you want to change and set it in the main method. – Peter Lawrey Oct 18 '16 at 15:25
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Nikolas Charalambidis Oct 18 '16 at 15:25

1 Answers1

0

You are constructing a new ATM object every time you run the loop, with the starting values passed to the constructor, so you display the values of this new object and the old one gets destroyed.

Roc Aràjol
  • 190
  • 9