gpt4 book ai didi

java - 账户利息计算器 : How to Pass Array Value to an Object?

转载 作者:行者123 更新时间:2023-12-02 07:25:43 25 4
gpt4 key购买 nike

我在一个正在开发的 Java 示例项目中遇到了瓶颈。我这个项目的目标是使用用户输入来计算利息,以确定正在使用的帐户类型,并根据每个特定帐户类型进行计算。

现在我已经创建了一个工厂方法“public Account createAccount”。我需要它接受用户提示中的字符串参数。告诉我是支票、储蓄还是 CD。现在我遇到了麻烦。我必须将“accttype”的用户值传递给特定于每种帐户类型的新对象。我的问题是我只是不知道该怎么做。我必须在工厂方法中实现吗?我怎样才能传递这些值?提前致谢

帐户.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();
}



}

银行家.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){

}


}

最佳答案

如果您想对所有三种帐户类型使用 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 变量,然后创建一个新帐户并调用 set 方法,但我建议使用接受这些值作为参数的构造函数。

由于您可能还希望在 Account 对象中设置其他变量,因此您可以在返回的 Account 对象上调用 setter ,或者可以修改 createAccount() 方法和 Account 构造函数以接受更多参数和当您创建新的 Account 对象时将它们传入。

关于java - 账户利息计算器 : How to Pass Array Value to an Object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13615279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com