-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise9_3.java
131 lines (115 loc) · 3.63 KB
/
Exercise9_3.java
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package exercises;
/** Exercise 9.3: Bank account Write a program that simulates a bank account.
* A button allows a deposit to be made into the account.
* The amount is entered into a text field. A second button allows a withdrawal to be made.
* The amount (the balance) and the state of the account is continually displayed -
* it is either OK or overdrawn. Create a class named Account to represent bank accounts.
* It has methods deposit, withdraw, getCurrentBalance and setCurrentbalance.
Filename: Exercise9_3.java
@author: © Gary Hill (200WXYZ)
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor: Gary Hill
@version: 1.0
Date: 11/06/19
*/
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Exercise9_3 {
private JFrame frmExercise;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Exercise9_3 window = new Exercise9_3();
window.frmExercise.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Exercise9_3() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmExercise = new JFrame();
frmExercise.setTitle("Exercise4_2");
frmExercise.setBounds(100, 100, 450, 300);
frmExercise.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmExercise.getContentPane().setLayout(null);
JLabel lblAmountToWithdraw = new JLabel("Amount");
lblAmountToWithdraw.setBounds(10, 29, 54, 24);
frmExercise.getContentPane().add(lblAmountToWithdraw);
textField = new JTextField();
textField.setBounds(60, 31, 129, 20);
frmExercise.getContentPane().add(textField);
textField.setColumns(10);
JLabel lblBalance = new JLabel("Balance: ");
lblBalance.setBounds(54, 64, 109, 33);
frmExercise.getContentPane().add(lblBalance);
JButton btnDeposit = new JButton("Deposit");
btnDeposit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Account ac = new Account();
double value = Double.parseDouble(textField.getText());
textField.setText(null);
double initBal = ac.getCurrentBalance();
double finBal = value+initBal;
ac.setCurrentBalance(finBal);
lblBalance.setText(Double.toString(finBal));
}
});
btnDeposit.setBounds(10, 122, 89, 23);
frmExercise.getContentPane().add(btnDeposit);
JButton btnWithdraw = new JButton("Withdraw");
btnWithdraw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Account ac = new Account();
double value = Double.parseDouble(textField.getText());
textField.setText(null);
double initBal = ac.getCurrentBalance();
double finBal = initBal-value;
if(finBal>=0) {
ac.setCurrentBalance(finBal);
lblBalance.setText(Double.toString(finBal));
}else {
lblBalance.setText("Overdrawn");
}
}
});
btnWithdraw.setBounds(131, 122, 89, 23);
frmExercise.getContentPane().add(btnWithdraw);
}
//inner class that models the bank account
public static class Account {
private static double balance;
public void deposit(double value) {
balance += value;
}
public void withdraw(double value) {
balance -= value;
}
public double getCurrentBalance() {
return balance;
}
public void setCurrentBalance(double value) {
balance = value;
}
}
}