【问题标题】:How to properly use if else statements in getter and setter methods? [duplicate]如何在 getter 和 setter 方法中正确使用 if else 语句? [复制]
【发布时间】:2018-09-25 14:06:28
【问题描述】:

我正在尝试创建一个银行账户程序,用户可以在其中为储蓄账户输入“s”或“S”账户类型。他们还可以为支票账户输入“c”或“C”。我在让用户输入通过 getter/setter 方法运行然后根据输入在输出中返回字符串“Savings”或“Checking”时遇到问题。

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

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";
        }

    }
}

【问题讨论】:

  • 请添加您遇到的错误。说我有问题根本没有帮助。
  • 您应该为帐户类型创建一个枚举。从用户输入到实际类型的转换不应在 setter 中完成,而应接近接收用户输入的逻辑。您的域对象不应该关心它是用户文本输入还是表单下拉列表或其他内容。而在 Java 中,字符串是使用 equals 方法进行比较的,而不是使用 ==
  • 目前我不一定会遇到错误。我希望 JOptionPane.showMessageDialog 在用户输入“S”、“s”或“C”、“c”后让 Account Type 在其旁边显示“Savings”或“Checking”。
  • 设置前请检查条件,然后将最终结果尝试发送到setter方法
  • 使用if(accountType.equalsIgnoreCase( "c" )

标签: java if-statement getter-setter


【解决方案1】:

你的setAccountType 方法代码应该是这样的:

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

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

        }

这将解决问题。

参考了解== and equals() 之间的区别: What is the difference between == vs equals() in Java?

我使用equalsIgnoreCase() 来检查小写和大写值。

【讨论】:

    【解决方案2】:

    String 是一个 java 对象,使用 == 运算符比较两个 java 对象引用只会在两个引用引用同一个对象时返回 true。这里if条件中的“s”和setter中的输入字符串“s”指的是不同的对象。因此,要使其工作,您需要使用String的equals方法来比较值而不是对象引用。

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

    【讨论】:

    • 不完全正确,字符串常量通常是“内部”的,因此具有相同值的两个常量实际上可以与 == 进行比较,尽管最好不要依赖它。
    • @shazinltc 我同意你在谈论字符串文字池。当您使用文字(不是 new String())初始化两个字符串时有效,但在这种情况下,一个字符串是输入参数,因此我们无法确定是否从文字池中获取了两个具有相同值的字符串。并且文字池的大小也有限制,因此在比较两个字符串时最好使用 eqauls。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    相关资源
    最近更新 更多