java - 无法通过类方法更改对象的值

标签 java object methods

我创建了一个名为 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"));
    }

}

最佳答案

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 语句,这将减少重复代码的数量,但这超出了这个问题;)

关于java - 无法通过类方法更改对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58905375/

相关文章:

c++ - 嵌套对象中的内存不可用

java - 为什么条目在 Firebase 数据库中不断循环?

c# - WPF 数据绑定(bind)到复合类模式?

java - 检查API版本

javascript - 如何遍历复杂的 Json 对象并对每个等于特定值的属性做一些事情

java - 递归isSubtring方法java

javascript类范围和匿名函数

java - 请帮我调用打印方法

java - 从列表中找到离用户位置最近的 Gps 点

java - 在 Java 中四舍五入到最接近的其他 double 倍数?