Fork me on GitHub

SimpleBankSystem

这是一个简单的银行操作系统,只有后端代码,没有前端代码,通过list以及集合的初始化来模仿数据库

Bank类

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
package com.dwx.bank;

import com.dwx.customer.Customer;

import java.util.ArrayList;

public class Bank {
//对于Bank类 应为要模拟数据库 就需要设计一个集合来存储用户信息
//对于此种加载类时就需要产生的数据,易使用static来修饰
static ArrayList<Customer> BankDataBase = new ArrayList<>();
static {
//对数据库进行初始化
Customer customer1 = new Customer("1311164587", "welcome", "dwx", 1000.0);
Customer customer2 = new Customer("1311164586", "welcome", "dwa", 1000.0);
Customer customer3 = new Customer("1311164585", "welcome", "dwb", 1000.0);

BankDataBase.add(customer1);
BankDataBase.add(customer2);
BankDataBase.add(customer3);
}

public static ArrayList<Customer> getBankDataBase() {
return BankDataBase;
}

public static void setBankDataBase(ArrayList<Customer> bankDataBase) {
BankDataBase = bankDataBase;
}
}
//这个test类被证明是可以使用的 通过static对list进行初始化
//class Test{
// public static void main(String[] args) {
// Bank bank = new Bank();
//
// System.out.println(Bank.BankDataBase.get(0).getID());
// }
//}

Customer类

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
package com.dwx.customer;

public class Customer {
//对于用户 应该包括以下属性
private String ID;
private String password;
private String username;
private double index;

public String getID() {
return ID;
}

public void setID(String ID) {
this.ID = ID;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public double getIndex() {
return index;
}

public void setIndex(double index) {
this.index = index;
}

public Customer() {

}

public Customer(String ID, String password, String username, double index) {
this.ID = ID;
this.password = password;
this.username = username;
this.index = index;
}

}

BankSystem类

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
package com.dwx.service;

import com.dwx.bank.Bank;
import com.dwx.customer.Customer;

import java.util.ArrayList;
import java.util.Scanner;

public class Service {
public static void main(String[] args) {
new Bank();
Customer customer;
//登录程序是和其他程序分开的
while (true) {
customer = login();
if (customer.getPassword() == null) {
System.out.println("登录失败!");
System.out.println("请重新登录");
} else {
System.out.println("登陆成功");
System.out.println("欢迎使用dwxATM" + customer.getUsername() + "用户");
break;
}
}
int order=0;
Scanner scanner = new Scanner(System.in);
Order();
while (order!=5) {
System.out.println("请输入你要进行的操作:");
order = scanner.nextInt();
switch (order) {
case 1:
customer = Saving(customer);
break;
case 2:
customer = Take(customer);
break;
case 3:
View(customer);
break;
case 4:
customer = ChangePassword(customer);
break;
case 5:
System.out.println("正在退出,请等候");
//一个小玩笑,在退出时,编译器啥也没做,就是sleep了3秒
try {
Thread.sleep(3000);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
break;
default:
break;
}
}
System.out.println("退出成功");

}

//登录功能的实现
public static Customer login() {
//调用数据库 查看数据库中有没有我们想要的类
System.out.println("请输入您的用户ID:");
Scanner scanner = new Scanner(System.in);
String ID = scanner.nextLine();
System.out.println("请输入您的用户密码:");
String password = scanner.nextLine();
//使用ThisCustomer来存储在数据库中找到的Customer对象
Customer ThisCustomer = new Customer();
for (Customer customer : Bank.getBankDataBase()) {
//对判断进行分层 实现区别是否数据库中存在这个对象(但是密码输入错误) 还是根本没有存在
if (customer.getID().equals(ID)) {
if (customer.getPassword().equals(password)) {
//此种情况是登录成功 ID和Password都对
ThisCustomer = customer;
break;
} else {
//这种情况是存在ID,但是密码错误
System.out.println("密码错误");
break;
}
}
}
return ThisCustomer;

}

//菜单
public static void Order() {
System.out.println("============================");
System.out.println("1:存钱");
System.out.println("2:取钱");
System.out.println("3:查看余额");
System.out.println("4:修改密码");
System.out.println("5:退出程序");
System.out.println("============================");
}
//存钱操作的实现
public static Customer Saving(Customer customer){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要存入的金额:");
double money = scanner.nextDouble();
//先把数据库拿出来
for (Customer c:Bank.getBankDataBase()){
//在数据库中遍历 查找到我们的用户 对用户进行操作 操作后及时更改暂时的customer对象并及时返回
if (c==customer){
c.setIndex(customer.getIndex()+money);
customer = c;
System.out.println("存钱成功");
}
}

return customer;
}
//取钱操作
public static Customer Take(Customer customer){
Scanner scanner = new Scanner(System.in);
//对于取钱 与存钱不同的是 取钱需要去查看余额是否足够 对数据库的操作无异
System.out.println("请输入您要取出的金额:");
double money = scanner.nextDouble();
if (money>customer.getIndex()){
System.out.println("取钱失败,余额不足");
}else {
//如果余额足够 就需要进行对数据库的操作了
//对数据库进行遍历 找到我们要操作的对象
for (Customer c:Bank.getBankDataBase()){
if (c==customer) {
c.setIndex(c.getIndex() - money);
//对数据库进行操作后 需要及时更改我们正在执行的Customer对象的情况并返回
customer = c;
System.out.println("取钱成功");
}
}
}
return customer;
}
//查看余额操作
public static void View(Customer customer){
System.out.println("用户"+customer.getUsername()+"您的账户余额还有"+customer.getIndex()+"元");
}
//修改密码操作
public static Customer ChangePassword(Customer customer){
//跟存钱一样 修改密码只需要对数据库进行操作 不需要任何的比较
Scanner scanner = new Scanner(System.in);
System.out.println("请输入新密码:");
String password = scanner.nextLine();
for (Customer c:Bank.getBankDataBase()){
if (c == customer){
c.setPassword(password);
customer = c;
System.out.println("修改后的密码为"+customer.getPassword());
}
}
return customer;
}

}

  • Copyrights © 2015-2023 dwx
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信