?? client.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 + -