我对 Java 很陌生,正在尝试创建一个银行储蓄和利息计算器。我已经创建了基本的计算器,但是在向这个计算器添加了一些新功能后,我似乎把它搞砸了,它不再执行以前的任何任务。
我想用这个计算器完成一些事情: 1. 选择您的帐户类型(储蓄、CD 或支票)并传递给我的扩展 JFrame 的帐户对象。
提示用户原始余额、利率和期限长度。将这些值传递给 Account 对象(这是我无法弄清楚的部分)。
单击计算按钮后打印最终余额。
询问用户“想开设新帐户吗?是/否”如果是,则循环返回。如果没有,退出。
我缺乏一些基本的 Java 知识,这就是我现在陷入困境的原因。任何帮助表示赞赏!
账户.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Account extends JFrame {
private int period;
private int balance;
private int rate;
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 String getPrintstring() {
return printstring;
}
public void setPrintString(String printstring) {
this.printstring = printstring;
}
public void calculate() {
for ( int i = 0; i<period; i++)
{
balance = (balance * rate) + balance;
}
}
}
银行家.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 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 3 Labels
JLabel blabel = new JLabel("Period: " + period);
JLabel plabel = new JLabel("Balance: " + balance);
JLabel flabel = new JLabel("Final 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.add(flabel);
frame.pack();
frame.setVisible(true);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
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
System.out.println(input);
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
createAndShowGUI();
}
}