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

我正在尝试创建并执行一种方法,该方法允许用户选择一个帐户,用户可以从中选择一个帐户,也可以将金额转入一个帐户,用户也可以选择该帐户。但是,我不知道如何让用户输入询问他们想要将资金转移到哪个类别。


我试过使用 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) {



我无法对用户输入做任何事情,因为我需要所需的帐户才能使用它们运行传输方法。


喵喔喔
浏览 265回答 4
4回答

Cats萌萌

我想我明白你在说什么,我已经检查了你的代码,但我认为那里有一些冗余代码,比如你的“帐户来自”。如果您是一个帐户的所有者并且您要转移到另一个帐户,您需要指定您的帐号吗?你明白?其次,最好使用多个 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's Account) 的实例并创建一个名为 (James' Account) 的实例James,所以现在,你有两个帐户类,它们具有不同的字段但相似的方法(getBalance(), transfer(), getAcctNum(), addFunds(), withdrawFunds())。我希望这就是您所需要的,祝您编码愉快!

慕盖茨4494581

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

一只萌萌小番薯

我建议您先创建虚拟数据(现有)帐户:&nbsp; List<Account> accountList = new ArrayList<>();&nbsp; Account acct1 = new Account("tester1", 100.0);&nbsp; Account acct2 = new Account("tester2", 100.0);&nbsp; accountList.add(acct1);&nbsp; accountList.add(acct2);为 Account 添加构造函数以便于添加 Accounts:&nbsp; &nbsp; public Account(String name, double balance) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.balance = balance;&nbsp; &nbsp; }将新帐户帐户存储在列表中:accountList.add(a1);对于用于传输的“(toDo == 6)”的程序:首先检查 accountList 是否至少有 2 个帐户,因为如果只有 1 个帐户就没有意义。else if (toDo == 6) {&nbsp; if (accountList.size() > 2) {&nbsp; &nbsp; System.out.println("Enter the account you would like to transfer money from:");&nbsp; &nbsp; String fromAccount = scan.next();&nbsp; &nbsp; System.out.println("Enter the account you would like to transfer money to:");&nbsp; &nbsp; String toAccount = scan.next();&nbsp; &nbsp; System.out.println("Enter the amount of money you would like to transfer: $");&nbsp; &nbsp; double moneyToTransfer = scan.nextDouble();&nbsp; &nbsp; for (Account account : accountList) {&nbsp; &nbsp; &nbsp; if (account.getName().equals(fromAccount)) {&nbsp; &nbsp; &nbsp; &nbsp; account.withdraw(moneyToTransfer);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if (account.getName().equals(toAccount)) {&nbsp; &nbsp; &nbsp; &nbsp; account.addFunds(moneyToTransfer);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; } else&nbsp;&nbsp; &nbsp; System.out.println("Cannot transfer.");&nbsp; }还要考虑该帐户是否实际存在,因此添加额外的检查。希望这可以帮助!

潇潇雨雨

我知道这个对话是从不久前开始的,但是,必须提到在这种情况下绝对不要像安东尼建议的那样使用静态方法,因为静态方法只能访问静态变量和方法。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java