类与对象实训

任务描述

模拟银行ATM机,编写一具有简单操作界面的JAVA程序,实现包括存款、取款、查询等功能在内的简单应用

任务分析

相关操作涉及的实体:

1
2
3
用户类(Account) 
属性:姓名、银行卡号、密码、余额。
操作:、存款、取款

1
2
3
ATM类
属性:登陆系统的用户
操作:登录、显示、存款、取款、查询余额

实现

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

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
ATM atm = new ATM();
atm.load_Sys();
}
}

class Account {

private String Password;
private String Name;
private double Balance;
private String Number;
Account() {};
Account(String Name,String Number,String Password,double Balance) {
this.Name = Name;
this.Password = Password;
this.Number = Number;
this.Balance = Balance;
}
public String getName() {
return this.Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getPassword() {
return this.Password;
}
public void setPassword(String Password) {
this.Password = Password;
}
public String getNumber() {
return this.Number;
}
public void setNumber(String Number) {
this.Number = Number;
}
public double getBalance() {
return this.Balance;
}
public void setBalance(double Balance) {
this.Balance = Balance;
}

public void add_Balance(double balance) {
this.Balance += balance;
}
public void sub_Balance(double balance) {
this.Balance -= balance;
}
public void show_balance() {
System.out.println("卡上余额为:" + this.Balance);
}
}
class ATM {

Account act;
public ATM() {
act = new Account("gageshan","****","****",50000000);
}


private void welcome() {
System.out.println("---------------------------------");
System.out.println("1-取款");
System.out.println("2-存款");
System.out.println("3-查询");
System.out.println("4-推出");
System.out.println("---------------------------------");
}

public void load_Sys() {
String card ;
String password ;
Scanner sc = new Scanner(System.in);
System.out.print("请输入您的银行卡号:");
card = sc.next();
System.out.print("请输入您的密码:");
password = sc.next();
int cnt = 1;
while(!check_sys(card,password)) {
if(cnt > 3)
System.exit(0);
cnt++;
System.out.print("密码错误,请重新输入:");
password = sc.next();
}
System.out.println("密码正确");
while(true) {
welcome();
System.out.print("请输入你想进行的操作:");
int op = sc.nextInt();
if(op == 1) {
bet_balance();
}
else if(op == 2) {
add_balance();
}
else if(op == 3) {
query_balance();
}
else {
System.exit(0);
}
}
}
private boolean check_sys(String card,String password) {
if(act.getNumber().equals(card) && act.getPassword().equals(password)) {
// System.out.println("hello the cruel world");
return true;
}
else
return false;
}
private boolean check_balance(double money) {
if(money >= act.getBalance())
return true;
return false;
}
private void bet_balance() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入您取款的金额:");
double money = sc.nextDouble();
if(check_balance(money)) {
System.out.println("余额不足!");
return ;
}
else {
act.sub_Balance(money);
System.out.println("取款成功!");
}
}
private void add_balance() {
System.out.print("请输入您存款的金额:");
Scanner sc = new Scanner(System.in);
double money = sc.nextDouble();
act.add_Balance(money);
System.out.println("存款成功!");
}
private void query_balance() {
System.out.println("当前余额为:" + act.getBalance());
}
}
-------------本文结束感谢您的阅读-------------
0%