【问题标题】:Can't change value of object from a class method无法从类方法更改对象的值
【发布时间】:2019-11-17 21:28:08
【问题描述】:

我创建了一个名为BankAccount 的类,该类具有从给定帐户存入和提取一定余额的方法。当我在我的主要方法中调用该方法时,它不会使用新余额更新帐户。该程序应该允许用户选择三个账户之一,并决定他们是否想从该银行账户存款或取款。然后它会询问他们是否愿意进行更多交易。如果他们希望不再进行任何交易,他们可以选择在不同的日期进行交易,然后显示每个银行账户的更新数据,我目前不这样做,因为余额没有从任何一个更新存款或取款。任何帮助将不胜感激。

public class BankAccount
{

    public final int MIN_LENGTH = 2;
    public final int MAX_LENGTH = 40;
    public final String DEFAULT_NAME = "nobody";
    public final double MIN_BALANCE = 0.0;
    public final int DEFAULT_NUMBER = 0;
    public final int MIN_NUMBER = 0;
    public final int MAX_NUMBER = 999999;

    private double balance;
    private String owner;
    private int accountNumber;

    public BankAccount(double initBal, String name, int number)
    {
        balance = initBal;
        owner = name;
        accountNumber = number;
    }

    public BankAccount(double initBal, String name)
    {
        balance = initBal;
        owner = name;
        accountNumber = 45678;
    }

    public BankAccount(String name)
    {
        owner = name;
        balance = MIN_BALANCE;
        accountNumber = 34567;
    }

    public void deposit(double amount)
    {
        balance = balance + amount;
    }

    public void withdraw(double amount)
    {
        balance -= amount;
    }

    public void withdraw(double amount, double fee)
    {
        balance -= (amount + fee);
    }

    public double getBalance()
    {
        return balance;
    }

    public String getOwner()
    {
        return owner;
    }

    public int getAccountNumber()
    {
        return accountNumber;
    }

    public void setAccountNumber(int newAccountNumber)
    {
        accountNumber = newAccountNumber;
    }

    public void setOwner(String name)
    {
        owner = name;
    }

    public void display()
    {
        System.out.println("Account number: " + getAccountNumber());
        System.out.println("Account owner: " + getOwner());
        System.out.println("Account balance: " + getBalance() + "\n");
    }

    public void close()
    {
        owner = "CLOSED";
        balance = 0.0;
    }

    private static boolean isValidName(String name)
    {
        final int MIN_LENGTH = 2;
        final int MAX_LENGTH = 40;

        if(name.length() >= MIN_LENGTH && name.length() <= MAX_LENGTH && name != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private static boolean isValidBalance(double initBalance)
    {
        if(initBalance > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    private static boolean isValidNumber(int acctNum)
    {
        final int MIN_NUMBER = 0;
        final int MAX_NUMBER = 999999;

        if(acctNum >= MIN_NUMBER && acctNum <= MAX_NUMBER)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}


import java.util.Scanner;

public class Transaction
{

    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        int accountChoice;
        double moneyAmount;
        String accountAction, transactionChoice = "nothing";
        BankAccount accountOne = new BankAccount(1000.0,"Matthew Johnson",12345);
        BankAccount accountTwo = new BankAccount(1000.0,"Sue Alexander",45678);
        BankAccount accountThree = new BankAccount(1000.0,"James William",34567);

        System.out.println("The following accounts are available:\n");
        do
        {
            accountOne.display();
            accountTwo.display();
            accountThree.display();

            System.out.print("Enter the number of the account you would like to access: ");
            accountChoice = keyboard.nextInt();

            System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
            accountAction = keyboard.nextLine();

            keyboard.nextLine();

            System.out.print("Enter the amount: ");
            moneyAmount = keyboard.nextDouble();

            if(accountChoice == 12345)
            {
                if(accountAction.equalsIgnoreCase("D"))
                {
                    accountOne.deposit(moneyAmount);
                }
                else if(accountAction.equalsIgnoreCase("W"))
                {
                    accountOne.withdraw(moneyAmount);
                }
            }
            else if(accountChoice == 45678)
            {
                if(accountAction.equalsIgnoreCase("D"))
                {
                    accountTwo.deposit(moneyAmount);
                }
                else if(accountAction.equalsIgnoreCase("W"))
                {
                    accountTwo.withdraw(moneyAmount);
                }
            }
            else if(accountChoice == 34567)
            {
                if(accountAction.equalsIgnoreCase("D"))
                {
                    accountThree.deposit(moneyAmount);
                }
                else if(accountAction.equalsIgnoreCase("W"))
                {
                    accountThree.withdraw(moneyAmount);
                }
            }

            keyboard.nextLine();
            System.out.println("More transactions? (y/n)");
            transactionChoice = keyboard.nextLine();

            if(transactionChoice.equalsIgnoreCase("Y"))
            {
                continue;
            }
            else if(transactionChoice.equalsIgnoreCase("N"))
            {
                System.out.println("Would you like to enter transactions for another day? (y/n)");
                transactionChoice = keyboard.nextLine();
                if(transactionChoice.equalsIgnoreCase("Y"))
                {
                    continue;
                }
                else if(transactionChoice.equalsIgnoreCase("N"))
                {
                    break;
                }
            }
            else
            {
                System.out.println("Invlid Input");
                break;
            }

        } while(!transactionChoice.equalsIgnoreCase("N"));
    }

}

【问题讨论】:

    标签: java object methods


    【解决方案1】:

    nextInt 不会消耗留在缓冲区中的换行符,该换行符由(您的第一个)nextLine 语句捕获并将accountAction 设置为空字符串。这就是为什么我通常不使用这种工作流程,而是依赖nextLine 并单独解析该行,例如...

    System.out.print("Enter the number of the account you would like to access: ");
    accountChoice = new Scanner(keyboard.nextLine()).nextInt();
    
    System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
    accountAction = keyboard.nextLine();
    
    System.out.print("Enter the amount: ");
    moneyAmount = keyboard.nextDouble();
    

    当您遇到此类问题时,请使用System.out.println 打印出您的变量,这样您就可以确切地看到发生了什么,直到您学会如何使用调试器;)

    您还可以通过使用一个或多个循环来改进您的代码,而不是依赖if-else 语句,这将减少重复代码的数量,但这超出了问题的范围;)

    【讨论】:

    • 成功了!!谢谢,这个问题困扰了我好几个小时,非常感谢。另外,如果您不介意,您能否解释一下使用一个或多个循环的含义?
    • @MikeMB35 不是使用 3 个 if-else 语句,而是将您的帐户放入一个数组(或 ArrayList)并循环查找您想要更改的帐户,但您可能没有到目前为止还没有;)
    猜你喜欢
    • 1970-01-01
    • 2021-10-04
    • 2019-04-07
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多