📄 client.java
字号:
package BS.client;
/**
* @author 束罡
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class Client
{
private String cmd = "";
private String serverHost = "127.0.0.1";
private int serverPort = 1219;
public static void main(String[] args)
{
Client ca = new Client();
ca.init();
ca.start();
}
private synchronized void init()
{
String serverURL = ".\\bin\\InitialData\\InitialClient.txt";
String inLine = ""; //读出的文件临时行
try {
FileInputStream fi = new FileInputStream(serverURL);
InputStreamReader ir = new InputStreamReader(fi) ;
BufferedReader in = new BufferedReader(ir);
while ((inLine = in.readLine()) != null)
{
serverHost = inLine;
}
}
catch(FileNotFoundException e)
{
System.out.println("----------------------------");
System.err.println("ERROR>> The InitialClient file is missed.");
System.out.println("----------------------------");
}
catch(Exception e)
{
System.out.println("----------------------------");
System.err.println("ERROR>> The initialization is failed.");
System.out.println("----------------------------");
}
serverHost = "127.0.0.1";
System.out.println(" $$$$$$$$ Welcome to Banking System $$$$$$$$");
System.out.println();
System.out.println(" 1.WithDraw 2.Deposit");
System.out.println(" 3.BalanceCheck 4.Quit");
System.out.println();
}
public synchronized void start()
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Socket socket = new Socket(serverHost, serverPort);
PrintWriter os=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String result;
while(true)
{
System.out.println("----------------------------");
System.out.println("$请选择您的交易类型:");
cmd = in.readLine();
if(cmd.startsWith("1") || cmd.startsWith("withDraw"))
{
System.out.println("$请输入您的客户编号:");
String t_id = in.readLine();
System.out.println("$请输入您的提款金额:");
String t_amount = in.readLine();
try
{
Integer.parseInt(t_id);
Integer.parseInt(t_amount);
String aTask = "0" + "\t" + t_id + "\t" + "w" + "\t" + t_amount;
// System.out.println(aTask);
os.println(aTask);
os.flush();
result = is.readLine();
System.out.println("\n" + result);
}
catch(NumberFormatException e)
{
System.err.println("ERROR>> Format of the input number is wrong.");
}
}
else if(cmd.startsWith("2") || cmd.startsWith("deposit"))
{
System.out.println("$请输入您的客户编号:");
String t_id = in.readLine();
System.out.println("$请输入您的存款金额:");
String t_amount = in.readLine();
try
{
Integer.parseInt(t_id);
Integer.parseInt(t_amount);
String aTask = "0" + "\t" + t_id + "\t" + "d" + "\t" + t_amount;
// System.out.println(aTask);
os.println(aTask);
os.flush();
result = is.readLine();
System.out.println("\n" + result);
}
catch(NumberFormatException e)
{
System.err.println("ERROR>> Format of the input number is wrong.");
}
}
else if(cmd.startsWith("3") || cmd.startsWith("balanceCheck"))
{
System.out.println("$请输入您的客户编号:");
String t_id = in.readLine();
try
{
Integer.parseInt(t_id);
String aTask = "0" + "\t" + t_id + "\t" + "b" + "\t" + "0";
// System.out.println(aTask);
os.println(aTask);
os.flush();
result = is.readLine() + "\n" + is.readLine();
System.out.println("\n" + result);
}
catch(NumberFormatException e)
{
System.err.println("ERROR>> Format of the input number is wrong.");
}
}
else if(cmd.startsWith("4") || cmd.startsWith("quit"))
{
os.println("quit");
os.flush();
break;
}
else if(cmd.startsWith("help"))
{
System.out.println("Building...");
}
}
socket.close();
is.close();
os.close();
}
catch(Exception e)
{
System.out.println("----------------------------");
System.err.println("ERROR>> Cannot connected to Banking System: " + serverHost);
System.err.println("ERROR>> Maybe too many users or the server specified is not in service.");
System.out.println("----------------------------");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -