如何在Java中使用每个所需帐户的用户输入将资金从一个帐户转移到另一个帐户,以及转移金额

时间:2019-07-12 06:06:16

标签: java methods

我正在尝试创建并执行一种方法,该方法允许用户选择要从中选择的帐户以及用户要从中选择金额的帐户。但是,我不知道如何获得用户输入,以及他们想将资金转移到哪个班级。

我尝试使用Scanner方法,但无法正常工作。

这是代码,与下面的代码相同,但这更易于查看imo:https://gist.github.com/KamronKelley/32d9a40c285e501f64cf73fa28bf87b5

package banking;

public class Account {
    String name;
    double balance;

    public Account() {
        name = "";
        balance = 0.0;
    }

    public void setName(String newName) {
        name = newName;
    }

    public String getName() {
        return name;
    }

    public double getBalance() {
        return balance;
    }

    public void addFunds(double addedAmount) {
        balance = balance + addedAmount;
    }

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

    public void transfer(double amount, Account from, Account to) { //here is the transfer method, if i can improve this or alter it in order to make it easier to run using user input from the main file, let me know
        if(from.balance >= amount){
            from.balance = from.balance - amount;
            to.balance = to.balance + amount;
            System.out.println("Funds successfully transfered.");
        } else {
                System.out.println("Insufficient funds");
            }
        }
    }

package banking;
import java.util.Scanner;

public class BankSimulator {
    public static void main(String[] args) {

            System.out.println("Hello and welcome to the banking system. Please enter a name to create an account, no spaces: ");
            Scanner scan = new Scanner(System.in);
            Account a1 = new Account();
            a1.setName(scan.next());
            System.out.println("Account name: " + a1.getName());

        int count = 0;
        while(count == 0) {

                System.out.println("What would you like to do next?"  + "\n" +
                "Change account name: press 1"  + "\n" +
                "See account name: press 2"  + "\n" +
                "Check balance: press 3"  + "\n" + 
                "Add money to balance: press 4"  + "\n" +
                "Withdraw money from balance: press 5" + "\n" +
                "Exit program: press 7: " + "\n" +
                "To transfer funds between accounts: press 6");

                int toDo = scan.nextInt();

            if(toDo == 1) {

                    System.out.println("Enter new account name: ");
                    a1.setName(scan.next());
                    System.out.println("Account name: " + a1.getName());
            }
            else if(toDo == 2) {
                System.out.println("Account name: " + a1.getName());
            }
            else if(toDo == 3) {
                System.out.println("Current balance: $" + a1.getBalance());
            }
            else if(toDo == 4) {
                System.out.println("Desired amount to add: $");
                a1.addFunds(scan.nextDouble());
                System.out.println("Money successfully added to balance."); 
            }
            else if(toDo == 5) {
                System.out.println("Desired amount to withdraw: $");
                a1.withdraw(scan.nextDouble());
                System.out.println("Money successfully withdrawn from balance.");
            }
            else if(toDo == 6) {
                System.out.println("Enter the account you would like to transfer money from:");
                String fromAccount = scan.next();
                System.out.println("Enter the account you would like to transfer money to:");
                String toAccount = scan.next();
                System.out.println("Enter the amount of money you would like to transfer: $");
                double moneyToTransfer = scan.nextDouble();
        //this is what i need help with, I don't know what to do with these three things, and since the first two arent accounts, i cant run the transfer method on them


            }
            else if(toDo == 7) {
                System.out.println("Thank you for using our banking system. Until next time.");
                count = 1;

            }
        }
    }
}

我无法对用户输入执行任何操作,因为我需要所需的帐户才能使用它们进行转账方法。

4 个答案:

答案 0 :(得分:0)

您需要跟踪列表或地图中的所有帐户。然后,使用用户输入搜索并检索所需的帐户。另外,我认为您不需要Account对象中的转移方法,您可以只使用main的withdraw和addFunds,也可以像静态方法一样使用它。另外,我认为您还需要执行一些其他操作,例如创建新帐户,以及可能仅更改活动帐户的登录名。目前您只有1个帐户,即a1,因此转移任何款项都没有意义。

答案 1 :(得分:0)

我想我理解您在说什么,我已经检查了您的代码,但是我认为其中有一些多余的代码,例如您的“帐户来源”。如果您是帐户的所有者,并且要转移到另一个帐户,是否需要指定您的帐号?你懂吗?

第二,最好使用多个if和else-if来切换case语句。

class bankAccount {
String name;
private double balance;
private final long acctNum = ThreadLocalRandom.current().nextLong(100000000, 999999999);

public bankAccount(String name, double balance) {
    this.name = name;
    this.balance = balance;
    System.out.println("HELLO " + name + ", Your account number is: " + acctNum);
}
public void setName(String name) {
    this.name = name;
}
public void addFunds(int amount) {
    this.balance += amount;
}
public void withdrawFunds(int amount) {
    this.balance -= amount;
}
public double getBalance() {
    return balance;
}
public long getAcctNum() {
    return acctNum;
}
public void transfer(bankAccount name, double amount) {
    if(this.balance >= amount) {
        name.balance += amount;
        this.balance -= amount;
        System.out.println("Transaction Successful");
    }
    else {
        System.err.println("Insufficient Funds!");
    }
}
}

class BankSimulator {
static bankAccount John = new bankAccount("John", 50000);
static bankAccount James = new bankAccount("James", 3000);

public static void main(String[] args) {
    John.transfer(James, 300);
    System.out.println("John's new balance is "+ John.getBalance());
    System.out.println("James' new balance is "+ James.getBalance());
}

}

由于您将使用另一个account,因此您应该创建Account类的两个实例,这将澄清帐户之间要转移到的歧义。

基本上,我要做的只是创建Account类,创建了一个名为John(John的Account)的实例,并创建了一个名为James(James的Account)的实例,所以,现在,您要做两个帐户类,具有不同的字段但方法类似(getBalance(), transfer(), getAcctNum(), addFunds(), withdrawFunds())

我希望这就是您所需要的,快乐编码!

答案 2 :(得分:0)

我建议您首先创建虚拟数据(现有)帐户:

  List<Account> accountList = new ArrayList<>();
  Account acct1 = new Account("tester1", 100.0);
  Account acct2 = new Account("tester2", 100.0);
  accountList.add(acct1);
  accountList.add(acct2);

为帐户添加构造函数,以更轻松地添加帐户:

    public Account(String name, double balance) {
        this.name = name;
        this.balance = balance;
    }

将新帐户帐户存储在列表中:

accountList.add(a1);

对于要传输的“(toDo == 6)”过程:

首先检查accountList是否至少有2个帐户,因为只有1个帐户就没有意义。

else if (toDo == 6) {
  if (accountList.size() > 2) {
    System.out.println("Enter the account you would like to transfer money from:");
    String fromAccount = scan.next();
    System.out.println("Enter the account you would like to transfer money to:");
    String toAccount = scan.next();
    System.out.println("Enter the amount of money you would like to transfer: $");
    double moneyToTransfer = scan.nextDouble();

    for (Account account : accountList) {
      if (account.getName().equals(fromAccount)) {
        account.withdraw(moneyToTransfer);
      }
      if (account.getName().equals(toAccount)) {
        account.addFunds(moneyToTransfer);
      }
    }
  } else 
    System.out.println("Cannot transfer.");
  }

还要考虑该帐户是否实际存在,因此添加其他检查。希望这会有所帮助!

答案 3 :(得分:-2)

我知道这个对话是前一阵子的,但是必须提一下,在这种情况下绝对不要像安东尼建议的那样使用静态方法,因为静态方法只能访问静态变量和方法。