使用java实现一个简单的银行账户类,
其中包括:
账户信息:
账号、
姓名、
开户时间、
身份证号码、
账户上的金额
等成员。
有:
存款方法、
取款方法、
显示开户时间的方法、
获得账上的金额的方法
等。
并编写测试类。
账户类:
1 import java.util.ArrayList;
2
3 public class TestDay18_5_Account {
4 {
5 l.add(new TestDay18_5_Account("yang", 123456, "杨金儒", "21102219*********", "123456@qq.com"));
6 }
7 private static ArrayList<TestDay18_5_Account> l = new ArrayList<TestDay18_5_Account>();
8 private String id;//帐号
9 private int password;//密码
10 private String name;//姓名
11 private String personId;//身份证号
12 private double balance = 0.0;
13 private String time;
14
15 public TestDay18_5_Account() {
16 super();
17 }
18
19 public TestDay18_5_Account(String id, int password, String name, String personId, String time) {
20 super();
21 this.id = id;
22 this.password = password;
23 this.name = name;
24 this.personId = personId;
25 this.time = time;
26 }
27
28 public static ArrayList<TestDay18_5_Account> getL() {
29 return l;
30 }
31
32 public String getId() {
33 return id;
34 }
35
36 public void setId(String id) {
37 this.id = id;
38 }
39
40 public int getPassword() {
41 return password;
42 }
43
44 public void setPassword(int password) {
45 this.password = password;
46 }
47
48 public String getName() {
49 return name;
50 }
51
52 public void setName(String name) {
53 this.name = name;
54 }
55
56 public String getPersonId() {
57 return personId;
58 }
59
60 public void setPersonId(String personId) {
61 this.personId = personId;
62 }
63
64 public double getBalance() {
65 return balance;
66 }
67
68 public void setBalance(double balance) {
69 this.balance = balance;
70 }
71
72 public String getTime() {
73 return time;
74 }
75
76 public void setTime(String time) {
77 this.time = time;
78 }
79
80 // 存款方法
81 public void deposit(double balance) {
82 this.balance += balance;
83 System.out.println("成功存入" + balance + "元");
84 System.out.println("当前总余额为:" + this.balance + "元");
85 }
86
87 // 取款方法
88 public void withdraw(double balance) {
89 this.balance -= balance;
90 System.out.println("成功取出" + balance + "元");
91 System.out.println("当前总余额为:" + this.balance + "元");
92 }
93
94 // 输出信息
95 public String test() {
96 return "id:" + id + "\r姓名:" + name + "\r身份证号:" + personId + "\r当前余额:" + balance + "\r开户时间:" + time;
97 }
98 }
账户操作类:
1 package Day18test;
2
3 import java.util.Date;
4 import java.text.SimpleDateFormat;
5 import java.util.Scanner;
6
7 public class TestDay18_5_AccountTool {
8
9
10 public TestDay18_5_AccountTool() {
11 super();
12 // TODO Auto-generated constructor stub
13 }
14
15 // 起始界面
16 public static int begin() {
17 System.out.println("|-----------------------------|");
18 System.out.println("|请选择您要执行的操作(输入数字): |");
19 System.out.println("|1.登录 |");
20 System.out.println("|2.注册 |");
21 System.out.println("|0.退出 |");
22 // System.out.println("|-----------------------------|");
23 int operation = TestDay18_5_AccountTool.getInt("|-----------------------------|");
24 return operation;
25 }
26
27 // 登录方法
28 public static void login() {
29
30 // 登录循环
31 while (true) {
32 // String id = DiyToolsaaa.DiyOperation.getString(i);
33 String id = TestDay18_5_AccountTool.getString("请输入用户名:");
34 int password = Integer.parseInt(TestDay18_5_AccountTool.getString("请输入密码:"));
35 // 登录成功标记
36 boolean access = false;
37 for (TestDay18_5_Account user : TestDay18_5_Account.getL()) {
38 if (id.equals(user.getId()) && password == user.getPassword()) {
39 access = true;
40 TestDay18_5_AccountTool.loginyes(user);
41 break;
42 }
43 }
44 if (!access) {
45 // 登录密码错误执行
46 int loginerrorreturn = TestDay18_5_AccountTool.loginerror();
47 if (loginerrorreturn == 1) {
48 break;
49 }
50 }
51 }
52 }
53
54 // 注册方法
55 public static void register() {
56 TestDay18_5_Account a = new TestDay18_5_Account();
57
58 a.setId(TestDay18_5_AccountTool.getString("请设置用户名:"));
59 a.setPassword(TestDay18_5_AccountTool.getInt("请设置密码(数字):"));// 密码
60 a.setName(TestDay18_5_AccountTool.getString("请输入您的姓名:"));// 姓名
61 a.setPersonId(TestDay18_5_AccountTool.getString("请输入您的身份证号:"));// 身份证号
62 a.setBalance(0.0);
63 // 创建日期对象
64 Date d = new Date();
65 // 给定模式
66 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
67 String s = sdf.format(d);
68 a.setTime(s);
69 TestDay18_5_Account.getL().add(a);
70 }
71
72 // 登录密码错误执行
73 public static int loginerror() {
74 int x = 0;
75 System.out.println("|------------------------------------------|");
76 System.out.println("|用户名或密码错误,请选择您要执行的操作(输入数字):|");
77 System.out.println("|1.重试 |");
78 System.out.println("|2.退出 |");
79 // System.out.println("|------------------------------------------|");
80 int operationerror = TestDay18_5_AccountTool.getInt("|------------------------------------------|");
81 switch (operationerror) {
82 case 1:
83 break;
84 case 2:
85 x = 1;
86 break;
87 default:
88 System.out.println("请输入指定的数字");
89 break;
90 }
91 return x;
92 }
93
94 // 登录成功执行
95 public static void loginyes(TestDay18_5_Account a) {
96 System.out.println("======登录成功======");
97 while (true) {
98 System.out.println("|-----------------------------|");
99 System.out.println("|请选择您要执行的操作(输入数字): |");
100 System.out.println("|1.查询 |");
101 System.out.println("|2.存款 |");
102 System.out.println("|3.取款 |");
103 System.out.println("|0.退出 |");
104 // System.out.println("|-----------------------------|");
105
106 // 获取操作数字
107 int operationnum = TestDay18_5_AccountTool.getInt("|-----------------------------|");
108 // 判断是否退出
109 if (operationnum == 0) {
110 break;
111 }
112
113 // 执行操作数
114 TestDay18_5_AccountTool.operationnum(a, operationnum);
115 }
116 }
117
118 // 具体操作方法
119 public static void operationnum(TestDay18_5_Account a, int operationnum) {
120 // 具体操作执行
121 switch (operationnum) {
122 case 1:
123 // 输出信息
124 System.out.println(a.test());
125 break;
126 case 2:
127 // 存款操作
128 System.out.println("请输入存入金额");
129 double deposit = Double.parseDouble(TestDay18_5_AccountTool.getmoney("==================="));
130 a.deposit(deposit);
131 break;
132 case 3:
133 // 取款操作
134 System.out.println("请输入取出金额");
135 double withdraw = Double.parseDouble(TestDay18_5_AccountTool.getmoney("==================="));
136 a.withdraw(withdraw);
137 break;
138 default:
139 System.out.println("请输入指定的数字");
140 break;
141 }
142 }
143
144 // 获取文本数据
145 public static String getString(String i) {
146 // 创建键盘对象
147 @SuppressWarnings("resource")
148 Scanner sc = new Scanner(System.in);
149
150 // 输入提示
151 System.out.println(i);
152 // 赋值数据
153 String x = sc.next();
154 return x;
155 }
156
157 // 获取整数数据
158 public static int getInt(String i) {
159 // 创建键盘对象
160 @SuppressWarnings("resource")
161 Scanner sc = new Scanner(System.in);
162
163 // 输入提示
164 System.out.println(i);
165 while (true) {
166 if (sc.hasNextInt()) {
167 // 赋值数据
168 int x = sc.nextInt();
169 return x;
170 } else {
171 System.out.println("请输入整数数字");
172 sc = new Scanner(System.in);
173 }
174 }
175 }
176
177 // 获取金额数据(整数/小数)
178 public static String getmoney(String i) {
179 // 创建键盘对象
180 @SuppressWarnings("resource")
181 Scanner sc = new Scanner(System.in);
182
183 // 输入提示
184 System.out.println(i);
185 while (true) {
186 if (sc.hasNextDouble()) {
187 String x = Double.toString(sc.nextDouble());
188 return x;
189 } else {
190 System.out.println("请输入数字");
191 sc = new Scanner(System.in);
192 }
193 }
194 }
195 }
主类:
1 package Day18test;
2
3 public class TestDay18_5 {
4 public static void main(String[] args) {
5
6 System.out.println("======欢迎光临======");
7 // 账户操作循环
8 while (true) {
9 int operation = TestDay18_5_AccountTool.begin();
10 if (operation == 1) {
11 System.out.println("======请登录======");
12 TestDay18_5_AccountTool.login();
13 } else if (operation == 2) {
14 TestDay18_5_AccountTool.register();
15 } else if (operation == 0) {
16 break;
17 } else {
18 System.out.println("请输入指定的数字");
19 }
20 }
21 System.out.println("---------------");
22 System.out.println("程序已结束");
23 }
24 }