hooyantsing's Blog

第92次课程_网络线程案例_ATM系统模拟

字数统计: 1.4k阅读时长: 7 min
2019/12/19

源辰76班

第92次课程

2019.12.19

内容

网络线程案例_ATM系统模拟[廖彦]

1.项目案例

ATM.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
package com.yc.net.atm;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Scanner;
/**
*
* 银行柜员机类
*
*/
public class ATM {
    private Scanner sc = new  Scanner(System.in);
    private Socket server;
    private DataOutputStream dos;
    private DataInputStream dis;
    // 记录当前登录的用户卡号
    private String card;
    public static void main(String[] args)  throws UnknownHostException, IOException,  ClassNotFoundException {
       new ATM().start();
    }
    public void start() throws  UnknownHostException, IOException,  ClassNotFoundException {
       System.out.println("开始连接银行服务器!");
       server = new Socket("127.0.0.1",  8888);
       System.out.println("银行服务器连接成功!");
       InputStream in =  server.getInputStream();
       OutputStream out =  server.getOutputStream();
       dis = new DataInputStream(in);
       dos = new DataOutputStream(out);
       System.out.println("银行服务器准备就绪!");
       int op = 0;
       boolean ok = false;
       while (ok == false) {
           System.out.println("******************************");
           System.out.println("*           我的银行                 *");
           System.out.println("******************************");
           System.out.println("*            1、登录                   *");
           System.out.println("*            2、开户                   *");
           System.out.println("******************************");
           System.out.print("请输入你要办理的业务编号:");
           op = sc.nextInt();
           switch (op) {
           case 1:
               if ((ok = login()) == false)  {
                  System.out.println("密码错误!");
               }
               break;
           case 2:
               register();
               break;
           }
       }
       while (true) {
           System.out.println("\n");
           System.out.println("******************************");
           System.out.println("*           我的银行                 *");
           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("*         0、退       出                *");
           System.out.println("******************************");
           System.out.print("请输入你要办理的业务编号:");
           op = sc.nextInt();
           switch (op) {
           case 0:
               System.exit(0);
           case 1:
               balance();
               break;
           case 2:
               deposit();
               break;
           case 3:
               withdraw();
               break;
           case 4:
               transfer();
               break;
           case 5:
               details();
               break;
           default:
               System.out.println("输入错误,请求重新输入!");
           }
       }
    }
    /**
     * 注册
     */
    private void register() {
       // TODO Auto-generated method stub
    }
    /**
     * 余额
     */
    private void balance() {
       // TODO Auto-generated method stub
    }
    /**
     * 转账
     */
    private void transfer() {
       // TODO Auto-generated method stub
    }
    /**
     * 存款
     * @throws IOException
     */
    private void deposit() throws  IOException {
       System.out.print("请放入现金:");
       int money = sc.nextInt();
       dos.writeUTF("deposit");
       dos.writeUTF(card);
       dos.writeInt(money);
       String result = dis.readUTF();
       System.out.println(result);
    }
    /**
     * 取款
     */
    private void withdraw() {
       // TODO Auto-generated method stub
    }
    /**
     * 查询明细
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void details() throws  IOException, ClassNotFoundException {
       dos.writeUTF("details");
       dos.writeUTF(card);
       ObjectInputStream oos = new  ObjectInputStream(dis);
       @SuppressWarnings("unchecked")
       List<OpRecord> ret =  (List<OpRecord>)oos.readObject();
       for(OpRecord o : ret){
           System.out.println(o);
       }
    }
    private boolean login() throws  IOException {
       System.out.print("请输入卡号:");
       String card = sc.next();
       System.out.print("请输入密码:");
       String pwd = sc.next();
       dos.writeUTF("login");
       dos.writeUTF(card);
       dos.writeUTF(pwd);
       dos.flush();
       String result = dis.readUTF();
       System.out.println(result);
       boolean ret = "登录成功!".equals(result);
       if(ret == true){
           this.card = card;
       }
       return ret;
    }
}

Bank.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
package com.yc.net.atm;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
/**
* 银行类
*/
public class Bank {
    private ServerSocket server;
    {
       try {
           server = new ServerSocket(8888);
       } catch (IOException e) {
           throw new RuntimeException(e);
       }
    }
    public static void main(String[] args)  throws IOException {
       Bank bank = new Bank();
       bank.begin();
    }
    private void begin() throws IOException  {
       // 建立服务器,等待客户端连接
       System.out.println("银行服务器启动完成!");
       Socket client = server.accept();
       System.out.println("客户端连接成功!");
       // 当客户端连接成功之后,fwq进入就绪状态
       InputStream in =  client.getInputStream();
       OutputStream out =  client.getOutputStream();
       DataInputStream dis = new  DataInputStream(in);
       DataOutputStream dos = new  DataOutputStream(out);
       System.out.println("客户端就绪!");
       while (true) {
           String cmd = dis.readUTF();
           String card;
           // 当客户端发送命令过来,解析命令
           switch (cmd) {
           case "login":
               card = dis.readUTF();
               String pwd = dis.readUTF();
               System.out.println("登录判断");
               if (login(card, pwd)) {
                  dos.writeUTF("登录成功!");
               } else {
                  dos.writeUTF("登录失败!");
               }
               break;
           case "register":
               break;
           case "deposit":
               card = dis.readUTF();
               int money = dis.readInt();
               if(deposit(card,money)){
                  dos.writeUTF("存款成功!");
               } else {
                  dos.writeUTF("存款失败!");
               }
               break;
           case "details":
               card = dis.readUTF();
               List<OpRecord> ret =  details(card);
               ObjectOutputStream oos = new  ObjectOutputStream(out);
               oos.writeObject(ret);
               oos.flush();
               break;
           default:
               // 错误的命令
           }
       }
       // 执行对应的操作,并返回结果
    }
    private List<OpRecord> details(String  card) {
       // TODO Auto-generated method stub
       List<OpRecord> ret = new  ArrayList<>();
       ret.add(new  OpRecord(1,"123456",1000,0,new  Timestamp(System.currentTimeMillis())));
       ret.add(new  OpRecord(2,"123456",-2000,0,new  Timestamp(System.currentTimeMillis())));
       ret.add(new  OpRecord(3,"123456",8888,0,new  Timestamp(System.currentTimeMillis())));
       ret.add(new  OpRecord(4,"123456",-6666,7,new  Timestamp(System.currentTimeMillis())));
       return ret;
    }
    private boolean deposit(String card, int  money) {
       // TODO Auto-generated method stub
       return true;
    }
    private boolean login(String card,  String pwd) {
       if ("123456".equals(card) &&  "123".equals(pwd)) {
           return true;
       } else {
           return false;
       }
    }
}

OpRecord.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
package com.yc.net.atm;
import java.sql.Timestamp;
public class OpRecord implements  java.io.Serializable{
    private int id;
    private String accountid;
    private double opmoney;
    private double charge;
    private Timestamp optime;
    /**
     * JavaBean 规范
     * 1、所有的属性都是私有
     * 2、所有的属性都提供 getter 和 setter 方法
     * 3、必须提供无参数的构造方法
     * ....
     */
    public OpRecord(){}
    public OpRecord(int id, String  accountid, double opmoney, double charge,  Timestamp optime) {
       super();
       this.id = id;
       this.accountid = accountid;
       this.opmoney = opmoney;
       this.charge = charge;
       this.optime = optime;
    }
    @Override
    public String toString() {
       return "OpRecord [id=" + id + ",  accountid=" + accountid + ", opmoney=" +  opmoney + ", charge=" + charge
               + ", optime=" + optime + "]";
    }
    public int getId() {
       return id;
    }
    public void setId(int id) {
       this.id = id;
    }
    public String getAccountid() {
       return accountid;
    }
    public void setAccountid(String  accountid) {
       this.accountid = accountid;
    }
    public double getOpmoney() {
       return opmoney;
    }
    public void setOpmoney(double opmoney) {
       this.opmoney = opmoney;
    }
    public double getCharge() {
       return charge;
    }
    public void setCharge(double charge) {
       this.charge = charge;
    }
    public Timestamp getOptime() {
       return optime;
    }
    public void setOptime(Timestamp optime)  {
       this.optime = optime;
    }
}
CATALOG