亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在java中將資金從一個賬戶轉移到另一個賬戶,使用每個所需賬戶的用戶輸入,以及轉賬金額

如何在java中將資金從一個賬戶轉移到另一個賬戶,使用每個所需賬戶的用戶輸入,以及轉賬金額

喵喔喔 2023-03-23 15:15:29
我正在嘗試創建并執行一種方法,該方法允許用戶選擇一個帳戶,用戶可以從中選擇一個帳戶,也可以將金額轉入一個帳戶,用戶也可以選擇該帳戶。但是,我不知道如何讓用戶輸入詢問他們想要將資金轉移到哪個類別。我試過使用 Scanner 方法,但無法正常工作。這是代碼,它與下面的代碼相同,但這更容易查看 imo:https://gist.github.com/KamronKelley/32d9a40c285e501f64cf73fa28bf87b5package 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) {我無法對用戶輸入做任何事情,因為我需要所需的帳戶才能使用它們運行傳輸方法。
查看完整描述

4 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

我想我明白你在說什么,我已經檢查了你的代碼,但我認為那里有一些冗余代碼,比如你的“帳戶來自”。如果您是一個帳戶的所有者并且您要轉移到另一個帳戶,您需要指定您的帳號嗎?你明白?


其次,最好使用多個 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())。


我希望這就是您所需要的,祝您編碼愉快!


查看完整回答
反對 回復 2023-03-23
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

您需要跟蹤列表或地圖中的所有帳戶。然后您使用用戶輸入搜索并檢索所需的帳戶。此外,我認為您不需要 Account 對象中的 transfer 方法,您可以只使用 main 中的 withdraw 和 addFunds,或者像靜態方法一樣使用它。此外,我認為您將需要一些額外的操作,例如創建新帳戶以及可能只會更改活動帳戶的登錄名。目前您只有 1 個賬戶,即 a1,因此轉移任何東西都沒有意義。



查看完整回答
反對 回復 2023-03-23
?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

我建議您先創建虛擬數據(現有)帳戶:


  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);

為 Account 添加構造函數以便于添加 Accounts:


    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.");

  }

還要考慮該帳戶是否實際存在,因此添加額外的檢查。希望這可以幫助!


查看完整回答
反對 回復 2023-03-23
?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

我知道這個對話是從不久前開始的,但是,必須提到在這種情況下絕對不要像安東尼建議的那樣使用靜態方法,因為靜態方法只能訪問靜態變量和方法。



查看完整回答
反對 回復 2023-03-23
  • 4 回答
  • 0 關注
  • 244 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號