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

📄 client.java

📁 本银行系统涉及到多线程
💻 JAVA
字号:
import java.net.*;
import banking.MyStruct;
import java.io.*;


/**客户端应用程序,要求输入IP地址**/
public class Client
{
	public static void main(String[] args) throws Exception
	{
		//声明了Socket类型,输出流类型,输入流类型,数据输出流类型的引用并初始化为空
		Socket socket=null;
		OutputStream s1out=null;
		InputStream  sIn=null;
		DataOutputStream dos=null;
		DataInputStream  dataIn=null;
		
		
		/**建立Socket连接,获取Socket的输出流与输入,
		   建立数据输出流,数据输入流并与Socket输出流和输入流连接**/
		try
		{
			socket=new Socket(args[0],9911);
			s1out = socket.getOutputStream();
			sIn  = socket.getInputStream();
			dos = new DataOutputStream(s1out);
			dataIn = new DataInputStream(sIn);
		}
		catch (Exception e)
		{
			System.out.println("连接失败");
			//exit();
		}
	
		Double b_tem;
		String input;
		String firstName=new String();
		String lastName=new String();
		double balance=0;
		int opType=-1;
		String password=new String();
		String id=new String();
		String accountNum=new String();
		String rInf=new String();
		//开始接受客户输入;(循环开始)
		
		L:while(true)
		{
			System.out.println("请选择操作类型   创建用户输入“0”    查询输入“1”    存款输入“2” 取款输入“3” 退出输入“q” ");
			
			/*通过不可实例化类System的静态函数in获得终端(字节)输入流
			  把对象(字节)输入流转化为终端(字符)输入流
			  创建一个缺省长度的字符输入流缓冲区
			*/
			BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
			
			//使用缓冲区的readLine()方法,把输入流的内容送进缓冲区
			//把缓冲区的内容传入一个字符串对象,并且取得它的引用存放于input中
			input=new String(in.readLine());
			
			/**对用户的操作做判断**/
			if (input.equals("")) continue L;
			
			if(input.equals("q"))
			{
				  opType=5;  //操作类型-1为退出
				  //把通讯信息封装
				  String myMsn=new String(Integer.toString(opType)+"#"+firstName+"#"+lastName+"#"+id+"#"+Double.toString(balance)+"#"+password+"#"+accountNum);
				  try
					{
				
						dos.writeUTF(myMsn);
					
			
					}
				  catch (Exception e)
					{
					}
				  break;
			}
			  opType=(int)(input.charAt(0))-48;
			  
			//用户的开户操作
			switch(opType)
			{
				case 0:
				{
					System.out.println("请输入姓");
					firstName=new String(in.readLine());
				
					System.out.println("请输入名");
					lastName=new String(in.readLine());
				
					System.out.println("请输入身份证号码");
					id=new String(in.readLine());
				
					System.out.println("请输入金额");
					try
					{
						b_tem=new Double(in.readLine());				
					}
									
					catch (Exception e)
					{
						System.out.println("输入金额数据不合法,请重新输入一遍");
						try
						{
							b_tem=new Double(in.readLine());
						}
						catch (Exception a)
						{
							System.out.println("输入不合法,本次操作取消");
							continue L;
						}
					}	
					balance=b_tem.doubleValue();
				
					System.out.println("请输入密码");
					password=new String(in.readLine());
									
								 
				}
				break;
			
			//用户的查询操作
				case 1:
				{
					System.out.println("请输入身份证号码");
					id=new String(in.readLine());
					System.out.println("请输入帐号");
					accountNum=new String(in.readLine());
					System.out.println("请输入密码");
					password=new String(in.readLine());
			    		
				}
				break;
			
			//用户的存款操作
				case 2:
				{
					System.out.println("请输入身份证号码");
					id=new String(in.readLine());
					
					System.out.println("请输入帐号");
                			accountNum=new String(in.readLine());	
	
					System.out.println("请输入密码");
					password=new String(in.readLine());

					System.out.println("请输入存款金额");
					
					try
					{
						b_tem=new Double(in.readLine());				
					}
									
					catch (Exception e)
					{
						System.out.println("输入金额数据不合法,请重新输入一遍");
						try
						{
							b_tem=new Double(in.readLine());
						}
						catch (Exception a)
						{
							System.out.println("输入不合法,本次操作取消");
							continue L;
						}
					}	
					balance=b_tem.doubleValue();

									

				
                
				}
		               	break;
           		//用户的取款操作
            			case 3:
				{
					
					System.out.println("请输入身份证号码");
					id=new String(in.readLine());
					
					System.out.println("请输入帐号");
	               			accountNum=new String(in.readLine());
				
					System.out.println("请输入密码");
					password=new String(in.readLine());
				

					System.out.println("请输入取款金额");
					try
					{
						b_tem=new Double(in.readLine());				
					}
									
					catch (Exception e)
					{
						System.out.println("输入金额数据不合法,请重新输入一遍");
						try
						{
							b_tem=new Double(in.readLine());
						}
						catch (Exception a)
						{
							System.out.println("输入不合法,本次操作取消");
							continue L;
						}
					}	
					balance=b_tem.doubleValue();

					
					                                
				}
				break;
				default:
				{
					System.out.println("输入不正确,请重新选择");
					continue L;
				}
			}
				//把用户的操作参数按通讯协议打包成对应的字符串
	       		String myMsn=new String(Integer.toString(opType)+"#"+firstName+"#"+lastName+"#"+id+"#"+Double.toString(balance)+"#"+password+"#"+accountNum);
			

			//把通讯字符串写入已经挂钩Socket输出流的数据输出流,实现发送通讯信息
			try
			{
				
				dos.writeUTF(myMsn);
										    
			}
			catch (Exception e)
			{
				System.out.println("操作失败,可能你的连接已失效");
				System.out.println("请重启客户端,给您带来的不便请原谅");
				System.out.println("按回车键退出");
				in.read();
				break L;
			}
			
			
			//读取用户业务操作的结果
			try
			{
								
				rInf=dataIn.readUTF();
			}
			catch (Exception e)
			{
	        	}
	        	switch(opType)
			{
			case 5:
				System.out.println("谢谢光临");
				break;
			case 0: 
				{
					
					System.out.println("您的新帐户号码是 "+rInf);
					System.out.println("谢谢你的支持"+'\n');
				}
				break;
			case 1:
				{
					System.out.println("您帐上的余额是  "+rInf+'\n');
								
				}
				break;
			case 2:{
				  	System.out.println("你已经成功存款"+balance+"  你现在帐上的余额是: "+rInf+'\n');
				break;
				}
			case 3:{
					System.out.println(rInf+'\n');
				break;
			}

			default:System.out.println("错误输入"+'\n');
			}
	        	
			
		
			
		//返回循环,继续接受用户输入
		}
	
		
		
	}
	
	
	
	
	
}

⌨️ 快捷键说明

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