2

I am trying to create a bank account program where the user can enter either "s" or "S" account types for a Savings account. They can also enter "c" or "C" for a checking account. I'm having problems with getting the user input to run through getter/setter methods then returning in the output the String "Savings" or "Checking" depending on the input.

package com.company;
import javax.swing.*;

public class Main {

    public static void main(String[] args) {

        BankAccount myBank = new BankAccount();

        myBank.setAccountType(JOptionPane.showInputDialog("please enter an account type"));
            JOptionPane.showMessageDialog(null, "Account Number: " + "\nAccount Type: " + myBank.getAccountType() +"\nMinimum Balance: "+ "\nBalance Before Interest and Fees: " + "\n\nNew Balance:\n");

    }
}

BankAccount class

package com.company;

public class BankAccount  {
    private int accountNumber;
    private String accountType;
    private double minSavings = 2500;
    private double minChecking = 1000;
    private double currentBalance;


    public BankAccount(){ }

    public String getAccountType () {
        return this.accountType;
    }

    public void setAccountType (String please_enter_an_account_type) {
        if (accountType == "S" || accountType == "s")  {
            this.accountType = "Savings";
        }

        else if (accountType == "C" || accountType == "c")  {
            this.accountType = "Checking";
        }

    }
}
Shafin Mahmud
  • 3,831
  • 1
  • 23
  • 35
Zayne Morrison
  • 53
  • 1
  • 1
  • 3
  • 1
    Please add the error you're getting. Saying I'm having problems doesn't help at all. – faris Sep 25 '18 at 04:20
  • 3
    You should create an enum for the account type. Translation from user input to the actual type should not be done in the setter but close to the logic that receives user input. Your domain object shouldn't care if it was user text input or a form drop down or something else. And in Java, Strings are compared using the `equals` method, not using `==` – Erwin Bolwidt Sep 25 '18 at 04:22
  • I'm not getting an error necessarily at the moment. I want the JOptionPane.showMessageDialog to have the Account Type display either "Savings" or "Checking" next to it after the user enters either a "S", "s" or "C", "c". – Zayne Morrison Sep 25 '18 at 04:24
  • Please check the condition before setting and then final outcome try to send to the setter method – SaviNuclear Sep 25 '18 at 04:49
  • Use `if(accountType.equalsIgnoreCase( "c" )` – c0der Sep 25 '18 at 05:03

2 Answers2

3

your setAccountType method code should be like this :

public void setAccountType (String accountType)
        {
            if (accountType.equalsIgnoreCase("S"))
            {
                this.accountType = "Savings";
            }

            else if (accountType.equalsIgnoreCase("C"))
            {
                this.accountType = "Checking";
            }

        }

This will resolve the issue.

Reference for difference between == and equals() : What is the difference between == vs equals() in Java?

I have used equalsIgnoreCase() to check both lowercase and uppercase values.

Raj
  • 707
  • 6
  • 23
1

String is a java object and comparing two java object reference using == operator will only return true when two reference are referring to the same object. Here "s" in if condition and the input string "s" in setter refers to the different object.So to make it work you need to use equals method of String which compares the value not the object reference.

 public void setAccountType (String accountType)
        {
            if (accountType.toLowerCase().equals("s"))
            {
                this.accountType = "Savings";
            }

            else if (accountType.toLowerCase().equals("c"))
            {
                this.accountType = "Checking";
            }

        }
Nawnit Sen
  • 973
  • 2
  • 7
  • 14
  • Not entirely true, the string constants are usually "interned" such that two constants with the same value can actually be compared with ==, though it's better not to rely on that. – shazinltc Sep 25 '18 at 05:48
  • @shazinltc I agree you are talking about string literal pool. that works when you are initializng two String using literal (not new String()) but in this case one string is input parameter so we cant be sure if two string with same value are being taken from literal pool or not. And literal pool has the limitayion on size also so its better to use eqauls while comparing two string. – Nawnit Sen Sep 25 '18 at 14:36