-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreditCard.java
More file actions
114 lines (103 loc) · 3.96 KB
/
CreditCard.java
File metadata and controls
114 lines (103 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//******************************************************************************
// Filename: CreditCard.java
//
// Author: David C. Drake (https://davidcdrake.com)
//
// Description: CreditCard applet class for calculating monthly interest and
// payment amounts according to user-provided data.
//******************************************************************************
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;
public class CreditCard extends Applet implements ActionListener {
final int TEXT_FIELD_SIZE = 5, APPLET_WIDTH = 390, APPLET_HEIGHT = 110;
final double MONTHLY_PAYMENT_RATIO = 0.02, MIN_MONTHLY_PAYMENT = 20.0;
double initialBalance, balance, interestRate, interestCharge, totalInterest,
percentage, payment;
NumberFormat dollar = NumberFormat.getCurrencyInstance(),
percent = NumberFormat.getPercentInstance();
private Label inputBLabel, inputIRLabel, outputBLabel, outputILabel,
outputPLabel, resultBLabel, resultILabel, resultPLabel;
private TextField inputBalance, inputInterestRate;
private Button monthButton;
private String text;
private boolean firstTime;
//private JPanel myPanel;
public void init() {
firstTime = true;
inputBLabel = new Label("Initial balance:");
inputIRLabel = new Label("Interest rate:");
outputBLabel = new Label("New balance:");
outputILabel = new Label("Total interest charged:");
outputPLabel = new Label("% of original balance:");
resultBLabel = new Label("N/A");
resultILabel = new Label("N/A");
resultPLabel = new Label("N/A");
inputBalance = new TextField(TEXT_FIELD_SIZE);
inputInterestRate = new TextField(TEXT_FIELD_SIZE);
monthButton = new Button("One Month");
monthButton.addActionListener(this);
/*myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
myPanel.add(inputBLabel);
myPanel.add(inputBalance);
myPanel.add(inputIRLabel);
myPanel.add(inputInterestRate);
myPanel.add(outputBLabel);
myPanel.add(resultBLabel);
myPanel.add(outputILabel);
myPanel.add(resultILabel);
myPanel.add(outputPLabel);
myPanel.add(resultPLabel);
myPanel.add(monthButton);*/
add(inputBLabel, BorderLayout.NORTH);
add(inputBalance, BorderLayout.NORTH);
add(inputIRLabel, BorderLayout.NORTH);
add(inputInterestRate, BorderLayout.NORTH);
add(outputBLabel, BorderLayout.WEST);
add(resultBLabel, BorderLayout.EAST);
add(outputILabel, BorderLayout.WEST);
add(resultILabel, BorderLayout.EAST);
add(outputPLabel, BorderLayout.WEST);
add(resultPLabel, BorderLayout.EAST);
add(monthButton, BorderLayout.SOUTH);
setBackground(Color.white);
setSize(APPLET_WIDTH, APPLET_HEIGHT);
}
public void actionPerformed(ActionEvent event) {
// First time button gets pushed, read user data from TextFields:
if (firstTime) {
text = inputInterestRate.getText();
interestRate = Double.parseDouble(text) / 100;
text = inputBalance.getText();
balance = Double.parseDouble(text);
initialBalance = balance;
firstTime = false;
}
// Calculate monthly payment:
if (balance <= MIN_MONTHLY_PAYMENT) {
payment = balance;
balance = 0.0;
} else if ((balance * MONTHLY_PAYMENT_RATIO) > MIN_MONTHLY_PAYMENT) {
payment = balance * MONTHLY_PAYMENT_RATIO;
} else {
payment = MIN_MONTHLY_PAYMENT;
}
balance -= payment;
// Calculate monthly interest charge:
if (balance > 0.0) {
interestCharge = balance * interestRate;
balance += interestCharge;
} else {
interestCharge = 0.0;
percentage = 0.0;
}
totalInterest += interestCharge;
percentage = totalInterest / initialBalance;
// Update labels:
resultBLabel.setText("" + dollar.format(balance));
resultILabel.setText("" + dollar.format(totalInterest));
resultPLabel.setText("" + percent.format(percentage));
}
}