⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverthread.java

📁 电子银行系统
💻 JAVA
字号:
package Server;import java.net.*;import java.io.*;import java.util.*;import Exception.*;import model.*;import business.Bank;public class ServerThread extends Thread {	/*	 * 因为网络中传送的都是object(对象),所以要从socket中获得对象输入流和对象输出流;	 */	private Socket s;	private ObjectOutputStream out;	private ObjectInputStream in;	public ServerThread(Socket s) throws Exception {		/*		 * 接受从客户端传过来的socket,并且从其中获得对象输入流和对象输出流;		 */		this.s = s;		out = new ObjectOutputStream(s.getOutputStream());		in = new ObjectInputStream(s.getInputStream());	}	/*	 * 服务器线程也是个死循环(专门用来和客户的socket通信),来接收一个Info对象,正常情况给客户端 发送Account对象,	 * 异常发送异常信息	 */	public void run() {		try {			while (true) {				try {										/*					 * 如果网络中没有数据(对象)传送,readObject()方法会阻塞这里,					 * 如果读到了null说明出错;						 */					Object o = in.readObject();					if (o == null) {						System.out.println("客户端下线或者网络故障 !");						break;					}										/*					 * 因为接受的Info对象, 并且取得Info对象的客户端传来数据信息,					 * 所以信息都封装在Info对象的HashMap中;					 */					Info inf = (Info) o;				    HashMap data = inf.getData();					/*					 *  获得一个银行的实例(银行是单例)					 */					Account account = null;					Bank bank = Bank.newBank();					/*					 * 从Info对象中解析出数据,根据Info对象的type值执行不同的动作,					 * 调用Bank的不同方法;					 * 如果是注册的请求,就获得客户的注册信息					 */					if (inf.getType() == Info.REGISTER) {						String pass1 = (String) data.get("pass1");						String pass2 = (String) data.get("pass2");						String name = (String) data.get("name");						String personId = (String) data.get("personId");						String email = (String) data.get("email");						int type = (Integer) data.get("type");						/*						 *  然后调用银行的开户方法register();						 */						account = bank.register(pass1, pass2, name, personId,								email, type);						/*						 * 将处理成功的客户对象克隆一份传给客户端;						 * 为什么克隆一份呢?						 * 						 */ 						out.writeObject(account.clone());						out.flush();											// 登录的请求,					} else if (inf.getType() == Info.LOGIN) {						String pass = (String) data.get("pass");						long id = (Long) data.get("id");						/*						 *  然后调用银行的开户方法login();						 */						account = bank.login(id, pass);						out.writeObject(account.clone());						out.flush();					/*					 *  接下来处理业务请求Info.BUSINESS					 */					} else {												/*						 * 注册或登录完成之后,就开始进行用户相应的操作,						 * 然后再根据不用业务类型,来实现不同的业务;						 */						String choice = (String) data.get("choice");						double money = (Double) data.get("money");						long id = (Long) data.get("id");							if (choice.equals("存款")) {														account = bank.deposit(id, money);							System.out.println(account.getBalance());													} else if (choice.equals("取款")) {							account = bank.withdraw(id, money);						} else if (choice.equals("设置透支额度")) {							account = bank.setCeiling(id, money);						} else if (choice.equals("申请贷款")) {							account = bank.requestLoan(id, money);						} else {							account = bank.payLoan(id, money);						}												//将处理完的结果对象写回给用户;						out.writeObject(account.clone());						out.flush();					}				} catch (BusinessException e) {					/*					 * 如果在处理过程中出错了,就将异常对象写回给客户					 */					out.writeObject(e.getMessage());					out.flush();				}			}		} catch (IOException e) {		} catch (ClassNotFoundException e) {		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -