0

取り組んでいる Java サンプル プロジェクトのレンガの壁に到達しました。このプロジェクトでの私の目標は、ユーザー入力を使用して使用されているアカウントの種類を特定し、特定のアカウントの種類ごとに計算して利息を計算することです。

現在、ファクトリ メソッド「public Account createAccount」を作成しています。ユーザープロンプトから文字列パラメーターを受け入れる必要があります。それが当座預金か、普通預金か、CD か教えてください。ここで、私は問題に遭遇します。「accttype」のユーザー値を、アカウントの種類ごとに固有の新しいオブジェクトに渡す必要があります。私の問題は、これを行う方法がわからないことです。ファクトリメソッドで実装する必要がありますか? これらの値を渡すにはどうすればよいですか? 前もって感謝します

Account.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Account implements ActionListener {

JButton calculate;
private int period;
private int balance;
private int fbalance;
private int rate;
private int monthlyFee;
private String printstring;


@Override
public String toString() {
    return String.format("Period: " + period + ", Balance: " + balance);
}

public int getPeriod() {
    return period;
}

public void setPeriod(int period) {
    this.period = period;
}

public int getBalance() {
    return balance;
}

public void setBalance(int balance) {
    this.balance = balance;
}

public int getRate() {
    return rate;
}

public void setRate(int rate) {
    this.rate = rate;
}

    public int getFbalance() {
    return fbalance;
}

public void setFbalance(int fbalance) {
    this.fbalance = fbalance;
}

public String getPrintstring() {
    return printstring;
}

public void setPrintString(String printstring) {
    this.printstring = printstring;
}

public void calculate() {
    for ( int i = 0; i<period; i++)
{
    fbalance = balance + balance * rate - monthlyFee;
}

}

public void actionPerformed(ActionEvent e) {
    calculate();
}



}

Banker.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Banker {

// Array for type of bank account

    private static void createAndShowGUI() {

    // Declare strings for period, balance, rate
    String period;
    String balance;
    String rate;

    // Prompt for account type
    String[] accttype = { "Checking", "Savings", "CD" }; // Array of bank acct types
    String input = (String) JOptionPane.showInputDialog(null, "Choose account...",
    "Choose bank account type", JOptionPane.QUESTION_MESSAGE, null,
    accttype, // Array of acct types
    accttype[0]); // First choice

    // Prompt user for input
    period = JOptionPane.showInputDialog(null, "Number of periods (length):");
    balance = JOptionPane.showInputDialog(null, "Beginning balance:");
    rate = JOptionPane.showInputDialog(null, "Interest rate (use decimal, example: .05      = 5%):");

    // Make Calculate button
    JButton calculate = new JButton("Calculate");

    // Make 2 Labels
    JLabel blabel = new JLabel("Period: " + period);
    JLabel plabel = new JLabel("Balance: " + balance);

    // Setup window with flow layout and exit on close
    JFrame frame = new JFrame("Interest Savings Calculator Plus");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Add combo box, calc button and labels
    frame.add(calculate);

    frame.add(plabel);
    frame.add(blabel);
    frame.pack();
    frame.setVisible(true);


    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
            createAndShowGUI();
        }

public Account createAccount(String type){

}


}
4

1 に答える 1

2

3 つのアカウント タイプすべてに Account クラスを使用する場合は、Account クラスにコンストラクターを追加し、次のようにアカウント タイプを保持する変数を追加することをお勧めします。

public class Account implements ActionListener {

    ...

    private String accountType = null;

    public Account(String accountType) {

        this.accountType = accountType;

    }

    ...
}

次に、 createAccount() メソッドで新しい Account オブジェクトを作成し、次のように返すことができます。

public Account createAccount(String type) {

    Account account = new Account(type);

    return account;

}

次に、単純にアカウントの種類を渡すことができます (これは createAndShowGUI() メソッドで「入力」変数が設定されるものです):

Account account = createAccount(input);

それ以外の場合は、getter と setter を使用して accountType 変数を追加し、新しい Account を作成して set メソッドを呼び出すこともできますが、これらの値をパラメーターとして受け入れるコンストラクターを使用することをお勧めします。

Account オブジェクトで他の変数も設定する必要があるため、返された Account オブジェクトでセッターを呼び出すか、 createAccount() メソッドと Account コンストラクターを変更して、より多くのパラメーターを受け入れて渡すことができます。新しい Account オブジェクトを作成するとき。

于 2012-11-28T23:25:41.143 に答える