-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankSystem.java
185 lines (175 loc) · 7.32 KB
/
BankSystem.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
0、你需要设计BankAccount类去模型化用户银行账户,CashAccount, CreditAccount
1、账户保存用户名称和余额,精确到分
2、用户应该可以进行存取操作
3、可以改变用户名
4、系统需要可以发现注册了多少银行账户
5、对于每一个账户,只有最后6个事务应该能够按升序存储并打印
6、余额为负数,存款为负数,取的钱大于余额时抛出IllegalArgumentException异常
用一个BankAccount作为基类,CreditAccount和BookAccount作为子类分别记录信用卡用户和存折用户
信用卡用户和存折用户共有的信息存在BankAccount内,特有的信息分别存在子类中,更换用户名和存取
的方法在父类中实现,记录银行账户个数采用全局变量,每增加一个初始化类则增加一次,对于账户操作
记录在父类中实现,没写进一次则记录一次,记录满六个时更新记录表。对于异常操作在CheckingAccount
类中实现,发现异常时抛出异常,并打印异常说明。
*/
import java.util.Arrays;
import java.util.List;
public class BankSystem{
//记录注册用户数
static int account_num = 0;
// 记录已经登记的用户身份证号
static String[] idtable = new String[10];
static boolean checkin(String idnumber) {
List<String> list = Arrays.asList(idtable);
if (list.contains(idnumber))
return true;
else
return false;
}
public static void main(String args[]) {
BookAccount account1 = new BookAccount("Eric", 0, "12345678", 0, "17130110001");
CreditAccount account2 = new CreditAccount("Nancy", 1, "87654321", 0, "1713110040");
CheckingAccount check = new CheckingAccount();
//检查余额是否为负数
check.checkBalance(account1);
//初始化两个类
System.out.println("----------------------------------------------");
System.out.println("initital Class BookAccount, Class CreditAccoun");
System.out.println("----------------------------------------------");
System.out.println("Class BookAccount");
System.out.println("name:" + account1.name + " ");
System.out.println("sex:" + account1.sex);
System.out.println("idnumber:" + account1.idnumber);
System.out.println("balance:" + account1.balance);
System.out.println("booknumber:" + account1.booknumber);
System.out.println("----------------------------------------------");
System.out.println("Class CreditAccount");
System.out.println("name:" + account2.name + " ");
System.out.println("sex:" + account2.sex);
System.out.println("idnumber:" + account2.idnumber);
System.out.println("balance:" + account2.balance);
System.out.println("booknumber:" + account2.cardnumber);
System.out.println("----------------------------------------------");
System.out.println("Deposits and Withdrawals");
System.out.println("----------------------------------------------");
System.out.println("Deposits 100 yuan");
double deposits = 100;
//检查存款是否为负数
check.checkBalance(account1);
account1.deposits(deposits);
System.out.println("balance:" + account1.balance);
double withdrawals = 55.36;
//检查是否取款大于余额
check.checkOverdrawn(withdrawals,account1);
System.out.println("Withdrawals 55.36 yuan");
account1.withdrawals(withdrawals);
System.out.println("balance:" + account1.balance);
System.out.println("----------------------------------------------");
System.out.println("Change Name");
System.out.println("----------------------------------------------");
System.out.println("Old name:" + account1.name);
account1.changename("Steve");
System.out.println("New name:" + account1.name);
System.out.println("----------------------------------------------");
System.out.println("The amount of account : " + account_num);
System.out.println("----------------------------------------------");
}
static int numofaccount(){
return account_num;
}
}
class BankAccount {
protected String name = new String("uninitialized"); // 姓名
protected int sex = -1; // 性别,0为男性,1为女性
protected String idnumber = new String("uninitialized"); // 身份证号
protected double balance = 0; // 余额
protected int i = 0;
protected double[] record = new double[6];
//更改用户名
void changename(String newname) {
name = newname;
}
//存钱
void deposits(double amount){
this.balance += amount;
if(i == 6)
i = 0;
record[i] = amount;
}
//取钱
void withdrawals(double amount){
this.balance -= amount;
if(i == 6)
i = 0;
record[i] = -amount;
}
void printRecord(){
System.out.println(record);
}
}
// 存折和信用卡账户,一个人仅能拥有存折和信用卡其中的一个
// 存折账户
class BookAccount extends BankAccount {
String booknumber = new String();
BookAccount(String name, int sex, String idnumber, double balance, String booknumber) {
if (BankSystem.checkin(idnumber)) {
System.out.println("idnumber exited");
System.exit(0);
} else {
//System.out.print("added");
//
BankSystem.account_num++;
}
this.name = name;
this.sex = sex;
this.idnumber = idnumber;
this.booknumber = booknumber;
}
}
//信用卡账户
class CreditAccount extends BankAccount{
String cardnumber = new String(); //信用卡号
CreditAccount(String name, int sex, String idnumber, double balance, String cardnumber){
if (BankSystem.checkin(idnumber)) {
System.out.println("idnumber exited");
System.exit(0);
} else {
//System.out.print("added");
//
BankSystem.account_num++;
}
this.name = name;
this.sex = sex;
this.idnumber = idnumber;
this.cardnumber = cardnumber;
}
}
class CheckingAccount {
void checkBalance(BankAccount account){
try{
if(account.balance < 0)
throw new IllegalArgumentException();
}
catch(IllegalArgumentException arg){
System.out.println("The account is constructed with a negative balance");
}
}
void checkDeposited(double deposits){
try{
if(deposits < 0)
throw new IllegalArgumentException();
}
catch(IllegalArgumentException arg){
System.out.println("a negative amount is deposited");
}
}
void checkOverdrawn(double withdrawals,BankAccount account){
try{
if(withdrawals > account.balance)
throw new IllegalArgumentException();
}
catch(IllegalArgumentException arg){
System.out.println("The amount withdrawn exceeds the current balance");
}
}
}