1

課題はほぼ完了です。私は銀行プログラムを作成しています、そして私はほとんどすべてをしました。私が立ち往生しているのはトランザクションの部分です。public String getTransactionInfo(int n)銀行口座の最後のn個のトランザクションを返す関数を作成する必要があります。私はこの部分を理解できないようです。変数がprivate int numOfTransactionsあり、それを関数に組み込んでみましたが、機能しませんでした。これが私が試したものです。

public String gettransactionInfo(int n)
{
    numOfTransactions = n;
    return n;
}

それはうまくいきませんでした。これを返す方法がわからないのは文字列です。何か案は?

import java.util.Scanner;
public class BankApp {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    Bank myBank = new Bank();

    int user_choice = 2;

    do {
        //display menu to user
        //ask user for his choice and validate it (make sure it is between 1 and 6)
        System.out.println();
        System.out.println("1) Open a new bank account");
        System.out.println("2) Deposit to a bank account");
        System.out.println("3) Withdraw to bank account");
        System.out.println("4) Print short account information");
        System.out.println("5) Print the detailed account information including last transactions");
        System.out.println("6) Quit");
        System.out.println();
        System.out.print("Enter choice [1-6]: ");
        user_choice = s.nextInt();
        switch (user_choice) {
            case 1: System.out.println("Enter a customer name");
                    String cn = s.next();
                    System.out.println("Enter a opening balance");
                    double d = s.nextDouble();
                    System.out.println("Account was created and it has the following number: " + myBank.openNewAccount(cn, d));
                    break;
            case 2: System.out.println("Enter a account number");
                    int an = s.nextInt();
                    System.out.println("Enter a deposit amount");
                    double da = s.nextDouble();
                    myBank.depositTo(an, da);
                    break;
            case 3: System.out.println("Enter a account number");
                    int acn = s.nextInt();
                    System.out.println("Enter a withdraw amount");
                    double wa = s.nextDouble();
                    myBank.withdrawFrom(acn, wa);
                    break;
            case 4: System.out.println("Enter a account number");
                    int anum = s.nextInt();
                    myBank.printAccountInfo(anum);
                    break;
            //case 5: ... break;
        }
}
while (user_choice != '6');
}

static class Bank {
private BankAccount[] accounts;     // all the bank accounts at this bank
private int numOfAccounts;      // the number of bank accounts at this bank

// Constructor: A new Bank object initially doesn’t contain any accounts.
public Bank() {
    accounts = new BankAccount[100];
    numOfAccounts = 0;
    }

// Creates a new bank account using the customer name and the opening balance given as parameters
// and returns the account number of this new account. It also adds this account into the account list
// of the Bank calling object.
public int openNewAccount(String customerName, double openingBalance) {

    BankAccount b = new BankAccount(customerName, openingBalance);
    accounts[numOfAccounts] = b;
    numOfAccounts++;
    return b.getAccountNum();
}

// Withdraws the given amount from the account whose account number is given. If the account is
// not available at the bank, it should print a message.
public void withdrawFrom(int accountNum, double amount) {
    for (int i =0; i<numOfAccounts; i++) {
        if (accountNum == accounts[i].getAccountNum()  ) {
            accounts[i].withdraw(amount);
            System.out.println("Amount withdrawn successfully");
            return;
        }
    }
    System.out.println("Account number not found.");
    }

// Deposits the given amount to the account whose account number is given. If the account is not
// available at the bank, it should print a message.
public void depositTo(int accountNum, double amount) {
    for (int i =0; i<numOfAccounts; i++) {
        if (accountNum == accounts[i].getAccountNum()  ) {
            accounts[i].deposit(amount);
            System.out.println("Amount deposited successfully");
            return;
        }
    }
    System.out.println("Account number not found.");
}

// Prints the account number, the customer name and the balance of the bank account whose
// account number is given. If the account is not available at the bank, it should print a message.
public void printAccountInfo(int accountNum) {
    for (int i =0; i<numOfAccounts; i++) {
                if (accountNum == accounts[i].getAccountNum()  ) {
                    System.out.println(accounts[i].getAccountInfo());
                    return;
                }
            }
    System.out.println("Account number not found.");
}

// Prints the account number, the customer number and the balance of the bank account whose
// account number is given, together with last n transactions on that account. If the account is not
// available at the bank, it should print a message.
public void printAccountInfo(int accountNum, int n) {
    for (int i =0; i<numOfAccounts; i++) {
                        if (accountNum == accounts[i].getAccountNum()  ) {
                            System.out.println(accounts[i].getAccountInfo());
                            System.out.println(accounts[i].getTransactionInfo(n));
                            return;
                        }
                    }
    System.out.println("Account number not found.");
    }

}





  static class BankAccount{

       private int accountNum;
       private String customerName;
       private double balance;
       private double[] transactions;
       private int numOfTransactions;
       private  static int noOfAccounts=0;

       public String getAccountInfo(){
           return "Account number: " + accountNum + "\nCustomer Name: " + customerName + "\nBalance:" + balance +"\n";
       }

       public String getTransactionInfo(int n)
       {
            numOfTransactions = n;
            return n;

        }

       public BankAccount(String abc, double xyz){
         customerName = abc;
         balance = xyz;
         noOfAccounts ++;
         accountNum = noOfAccounts;
         transactions = new double[100];
         transactions[0] = balance;
         numOfTransactions = 1;
       }

    public int getAccountNum(){
        return accountNum;
    }
    public void deposit(double amount){

        if (amount<=0) {
            System.out.println("Amount to be deposited should be positive");
        } else {
            balance = balance + amount;
            transactions[numOfTransactions] = amount;
            numOfTransactions++;
        }
    }
    public void withdraw(double amount)
    {
        if (amount<=0){
             System.out.println("Amount to be withdrawn should be positive");
         }
        else
        {
            if (balance < amount) {
                System.out.println("Insufficient balance");
            } else {
                balance = balance - amount;
                transactions[numOfTransactions] = amount;
                numOfTransactions++;
            }
        }
    }

}//end of class
}
4

6 に答える 6

4

あなたの問題に対する正しい解決策は次のようになると思います:

    public String gettransactionInfo(int n)
{
  //Traverse the "transactions" array in *reverse* order
  //In For loop start with transactions.length, and decrement by 1 "n" times
  // Append the transaction amount to a String
 // Return the string.
}
于 2013-03-07T05:04:50.037 に答える
3
public String gettransactionInfo(int n)
{
    numOfTransactions = n;
    return n;
}

へへ!ここで正確に何をしようとしていますか?:) ...右辺値と左辺値を台無しにしましたが、それは答えとは関係ありません。

重要なのは、クラスに文字列配列を含めることです。トランザクションが発生するたびに、トランザクションの詳細を配列に追加します.....次に、gettransactionInfo は文字列の最後の n 個の詳細を出力する必要があります...

例えば

transInfo[0] = "A deposited 30K"
transInfo[1] = "B withdrew 20K"
transInfo[2] = "C deposited 5k"

最後の 1 トランザクションは、文字列「C 預金 5k」の最後のエントリを表示します。

于 2013-03-07T05:02:33.627 に答える
1

トランザクション数を (格納されている) 整数ではなく文字列として返したい場合は、整数を文字列に変換します。

Java では、次の方法で行います。

Integer.toString(numOfTransactions)

補足: 好奇心から、あなたのプログラムでは、なぜこれを行っているのですか?

public String gettransactionInfo(int n)
{
    numOfTransactions = n;
    return n;
}

numberOfTransactions を n 値に設定しているため、これは非効率的で間違っています。その機能は、慣例により、getter メソッドとは対照的に、setter メソッドに配置されます。これは、実装が行うべきことです。

理想的には、次のようにする必要があります。

public String gettransactionInfo()
{
    return Integer.toString(numOfTransactions);
}

編集:

コメントによると、正しいことは、インデックス n に対応するトランザクション配列からインデックスを返すことです。

この場合、トランザクション配列は文字列配列である必要があります。

編集2:

別の文字列配列を使用する場合は、(トランザクションに応じて) 次のように更新します。

import java.util.Scanner;
public class BankApp {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    Bank myBank = new Bank();

    int user_choice = 2;

    do {
        //display menu to user
        //ask user for his choice and validate it (make sure it is between 1 and 6)
        System.out.println();
        System.out.println("1) Open a new bank account");
        System.out.println("2) Deposit to a bank account");
        System.out.println("3) Withdraw to bank account");
        System.out.println("4) Print short account information");
        System.out.println("5) Print the detailed account information including last transactions");
        System.out.println("6) Quit");
        System.out.println();
        System.out.print("Enter choice [1-6]: ");
        user_choice = s.nextInt();
        switch (user_choice) {
            case 1: System.out.println("Enter a customer name");
                    String cn = s.next();
                    System.out.println("Enter a opening balance");
                    double d = s.nextDouble();
                    System.out.println("Account was created and it has the following number: " + myBank.openNewAccount(cn, d));
                    break;
            case 2: System.out.println("Enter a account number");
                    int an = s.nextInt();
                    System.out.println("Enter a deposit amount");
                    double da = s.nextDouble();
                    myBank.depositTo(an, da);
                    break;
            case 3: System.out.println("Enter a account number");
                    int acn = s.nextInt();
                    System.out.println("Enter a withdraw amount");
                    double wa = s.nextDouble();
                    myBank.withdrawFrom(acn, wa);
                    break;
            case 4: System.out.println("Enter a account number");
                    int anum = s.nextInt();
                    myBank.printAccountInfo(anum);
                    break;
           case 5:  System.out.println("Enter a account number");
                    anum = s.nextInt();
                    myBank.printTransactionInfo(anum);
                    break;
          default: System.out.println("Invalid option. Please try again.");

        }
}
while (user_choice != '6');
}

static class Bank {
private BankAccount[] accounts;     // all the bank accounts at this bank
private int numOfAccounts;      // the number of bank accounts at this bank

    //Constructor: A new Bank object initially doesn’t contain any accounts.
public Bank() {
    accounts = new BankAccount[100];
    numOfAccounts = 0;
    }

// Creates a new bank account using the customer name and the opening balance given as parameters
// and returns the account number of this new account. It also adds this account into the account list
// of the Bank calling object.
public int openNewAccount(String customerName, double openingBalance) {

    BankAccount b = new BankAccount(customerName, openingBalance);
    accounts[numOfAccounts] = b;
    numOfAccounts++;
    return b.getAccountNum();
}

// Withdraws the given amount from the account whose account number is given. If the account is
// not available at the bank, it should print a message.
public void withdrawFrom(int accountNum, double amount) {
    for (int i =0; i<numOfAccounts; i++) {
        if (accountNum == accounts[i].getAccountNum()  ) {
            accounts[i].withdraw(amount);
            System.out.println("Amount withdrawn successfully");
            return;
        }
    }
    System.out.println("Account number not found.");
    }

// Deposits the given amount to the account whose account number is given. If the account is not
// available at the bank, it should print a message.
public void depositTo(int accountNum, double amount) {
    for (int i =0; i<numOfAccounts; i++) {
        if (accountNum == accounts[i].getAccountNum()  ) {
            accounts[i].deposit(amount);
            System.out.println("Amount deposited successfully");
            return;
        }
    }
    System.out.println("Account number not found.");
}

// Prints the account number, the customer name and the balance of the bank account whose
// account number is given. If the account is not available at the bank, it should print a message.
public void printAccountInfo(int accountNum) {
    for (int i =0; i<numOfAccounts; i++) {
                if (accountNum == accounts[i].getAccountNum()  ) {
                    System.out.println(accounts[i].getAccountInfo());
                    return;
                }
            }
    System.out.println("Account number not found.");
}

public void printTransactionInfo(int accountNum) {
    for (int i =0; i<numOfAccounts; i++) {
                if (accountNum == accounts[i].getAccountNum()  ) {
                    System.out.println(accounts[i].getAccountInfo());
                    System.out.println("Last transaction: " + accounts[i].getTransactionInfo(accounts[i].getNumberOfTransactions()-1));
                    return;
                }
            }
    System.out.println("Account number not found.");
}


// Prints the account number, the customer number and the balance of the bank account whose
// account number is given, together with last n transactions on that account. If the account is not
// available at the bank, it should print a message.
public void printAccountInfo(int accountNum, int n) {
    for (int i =0; i<numOfAccounts; i++) {
                        if (accountNum == accounts[i].getAccountNum()  ) {
                            System.out.println(accounts[i].getAccountInfo());
                            System.out.println(accounts[i].getTransactionInfo(n));
                            return;
                        }
                    }
    System.out.println("Account number not found.");
    }

}
    static class BankAccount{

       private int accountNum;
       private String customerName;
       private double balance;
       private double[] transactions;
       private String[] transactionsSummary;
       private int numOfTransactions;
       private  static int noOfAccounts=0;

       public String getAccountInfo(){
           return "Account number: " + accountNum + "\nCustomer Name: " + customerName + "\nBalance:" + balance +"\n";
       }

       public String getTransactionInfo(int n)
       {
            String transaction = transactionsSummary[n];
            if (transaction == null) {
                return "No transaction exists with that number.";
            }
            else {
                return transaction;
            }
        }

       public BankAccount(String abc, double xyz){
         customerName = abc;
         balance = xyz;
         noOfAccounts ++;
         accountNum = noOfAccounts;
         transactions = new double[100];
         transactionsSummary = new String[100];
         transactions[0] = balance;
         transactionsSummary[0] = "A balance of : $" + Double.toString(balance) + " was deposited.";
         numOfTransactions = 1;
       }

    public int getAccountNum(){
        return accountNum;
    }

    public int getNumberOfTransactions() {
        return numOfTransactions;
    }

    public void deposit(double amount){

        if (amount<=0) {
            System.out.println("Amount to be deposited should be positive");
        } else {
            balance = balance + amount;
            transactions[numOfTransactions] = amount;
            transactionsSummary[numOfTransactions] = "$" + Double.toString(amount) + " was deposited.";
            numOfTransactions++;
        }
    }
    public void withdraw(double amount)
    {
        if (amount<=0){
             System.out.println("Amount to be withdrawn should be positive");
         }
        else
        {
            if (balance < amount) {
                System.out.println("Insufficient balance");
            } else {
                balance = balance - amount;
                transactions[numOfTransactions] = amount;
                transactionsSummary[numOfTransactions] = "$" + Double.toString(amount) + " was withdrawn.";
                numOfTransactions++;
            }
        }
    }

    }//end of class
}
于 2013-03-07T04:57:39.040 に答える
0

ケース5のあなたの説明は

最後の取引を含む詳細なアカウント情報を印刷する

トランザクション数を文字列として返すだけでは、それは達成できません。

BankAccount クラスのトランザクション配列で発生するすべてのトランザクションを保存し、gettransactionInfo()関数でその配列の最後の n 値を取得する必要があります。

編集:

openNewAccount()ところで、関数を呼び出すのを忘れました

于 2013-03-07T05:00:46.307 に答える
0

整数を文字列に変換するには:

String out = Integer.toString(n);
于 2013-03-07T04:54:32.463 に答える
0

プリミティブ int をそのラッパー クラスに変換し、そのtoString()メソッドを呼び出します

public String gettransactionInfo(int n) { return new Integer(n).toString(); }

于 2013-03-07T05:10:06.643 に答える