📄 mainserver.java
字号:
import java.io.*;
import java.net.*;
import java.sql.*;
public class MainServer
{
private static final int PORT = 8080;
private static int ActivityShop;
private static int semaphore[];
private static boolean shopOpen[];
private static boolean userActive[];
private static boolean hasOrder[];
private static String IP[];
private static String systemBroadcast;
static class ServeOne extends Thread
{
private static DataBase db;
private static ResultSet rs;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private int USERID;
class TestActive extends Thread
{
public TestActive()
{
userActive[USERID]=true;
start();
}
public void run()
{
while(true)
{
if(socket.isClosed())
{
try
{
userActive[USERID]=false;
if(V(USERID))
{
System.out.println("userID:"+USERID+" dropped!");
shopOpen[USERID] = false;
String strSQL = "SELECT userName FROM user WHERE ID="+USERID;
rs = db.getResult(strSQL);
rs.next();
String customer = rs.getString("userName");
strSQL = "UPDATE orderlist SET status ='cancel' WHERE status ='unconfirmed' AND customer ='"
+customer+"'";
db.updateSql(strSQL);
}
in.close();
out.close();
socket.close();
break;
}catch(Exception e){}
}
else
{
try
{
this.sleep(5000);
}catch(Exception e){}
}
}
}
}
public ServeOne(Socket s) throws Exception
{
db = new DataBase();
socket = s;
USERID = -1;
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
start();
}
public boolean P(int id)throws Exception
{
if(semaphore[id]<0)
{
return false;
}
else
{
semaphore[id]--;
return true;
}
}
public boolean V(int id)throws Exception
{
if(semaphore[id]>=0)
{
return false;
}
else
{
semaphore[id]++;
return true;
}
}
public void logon()throws Exception
{
boolean legal = false;
boolean exist = false;
boolean merchant = false;
String userName = in.readLine();
String password = in.readLine();
String shopName = null;
String shopType = null;
String shopInfo = null;
int ID = 0;
int AID = 0;
String strSQL = "SELECT * FROM user WHERE userName='"+userName+"'";
rs = db.getResult(strSQL);
if(rs.first() && rs.getString(2).equals(userName))
{
exist = true;
if(rs.getString(3).equals(password))
{
legal = true;
ID = Integer.parseInt(rs.getString(1));
this.USERID = ID;
AID = Integer.parseInt(rs.getString(4));
if(rs.getString(5).equals("Merchant"))
{
merchant = true;
shopName = rs.getString(6);
shopType = rs.getString(7);
shopInfo = rs.getString(8);
}
}
}
if(!exist)
{
out.println("NOTEXIST");
out.flush();
}
else if(exist && !legal)
{
out.println("FAILURE");
out.flush();
}
else if(legal && !merchant)
{
if(P(ID))
{
out.println("SUCCESS");
out.println(ID);
out.println(AID);
System.out.println("User:"+userName+" logon");
new TestActive();
out.flush();
}
else
{
out.println("ALREADY");
out.flush();
}
}
else if(merchant)
{
if(P(ID))
{
out.println("CHOOSE");
out.println(ID);
out.println(AID);
out.println(shopName);
out.println(shopType);
out.println(shopInfo);
System.out.println("User:"+userName+" logon");
new TestActive();
}
else
{
out.println("ALREADY");
}
hasOrder[ID] = true;
}
}
public void logout()throws Exception
{
String userName = in.readLine();
int id = Integer.parseInt(in.readLine());
V(id);
System.out.println("User:"+userName+" Logout");
}
public void loginCustomer()throws Exception
{
boolean legal = true;
String userName = in.readLine();
String password = in.readLine();
int AID = Integer.parseInt(in.readLine());
String strSQL = "SELECT * FROM user WHERE userName='"+userName+"'";
if(db.getResult(strSQL).first())
{
legal = false;
}
if(legal)
{
strSQL = "SELECT * FROM account WHERE accountID='"+AID+"'";
if(!db.getResult(strSQL).first())
{
strSQL="INSERT INTO account(accountID,balance) VALUES('"+AID+"','"+0.0+"')";
db.updateSql(strSQL);
strSQL = "INSERT INTO user(userName,password,account,type) VALUES('"+userName+"','"+
password+"','"+AID+"','"+"Customer"+"')";
if(db.updateSql(strSQL))
{
out.println("SUCCESS");
}
else
{
out.println("FAILURE");
}
}
else
{
out.println("USED");
String name = in.readLine();
strSQL = "SELECT * FROM user WHERE account='"+AID+"' AND userName='"+name+"'";
if(db.getResult(strSQL).first())
{
strSQL = "INSERT INTO user(userName,password,account,type) VALUES('"+userName+"','"+
password+"','"+AID+"','"+"Customer"+"')";
if(db.updateSql(strSQL))
{
out.println("SUCCESS");
}
else
{
out.println("FAILURE");
}
}
else
{
out.println("AFAILURE");
}
}
}
else
{
out.println("FAILURE");
}
}
public void loginMerchant()throws Exception
{
boolean userlegal = true;
boolean shoplegal = true;
String userName = in.readLine();
String password = in.readLine();
int AID = Integer.parseInt(in.readLine());
String shopName = in.readLine();
String shopType = in.readLine();
String shopInfo = in.readLine();
String strSQL = "SELECT * FROM user WHERE userName='"+userName+"'";
if(db.getResult(strSQL).first())
{
userlegal = false;
}
strSQL = "SELECT * FROM user WHERE shopName='"+shopName+"'";
if(db.getResult(strSQL).first())
{
shoplegal = false;
}
if(userlegal&&shoplegal)
{
strSQL = "SELECT * FROM account WHERE accountID='"+AID+"'";
if(!db.getResult(strSQL).first())
{
strSQL = "INSERT INTO account(accountID,balance) VALUES('"+AID+"','"+0.0+"')";
db.updateSql(strSQL);
strSQL = "INSERT INTO user(userName,password,account,type,shopName,shopType,shopInfo) VALUES('"+userName+"','"+
password+"','"+AID+"','"+"Merchant"+"','"+shopName+"','"+shopType+"','"+shopInfo+"')";
if(db.updateSql(strSQL))
{
out.println("SUCCESS");
}
else
{
out.println("FAILURE");
}
}
else
{
out.println("USED");
String name = in.readLine();
strSQL = "SELECT * FROM user WHERE account='"+AID+"' AND userName='"+name+"'";
if(db.getResult(strSQL).first())
{
strSQL = "INSERT INTO user(userName,password,account,type,shopName,shopType,shopInfo) VALUES('"+userName+"','"+
password+"','"+AID+"','"+"Merchant"+"','"+shopName+"','"+shopType+"','"+shopInfo+"')";
if(db.updateSql(strSQL))
{
out.println("SUCCESS");
}
else
{
out.println("FAILURE");
}
}
else
{
out.println("AFAILURE");
}
}
}
else if(!userlegal)
{
out.println("FAILURE");
}
else if(!shoplegal)
{
out.println("SHOPFAILURE");
}
}
public void updataUser()throws Exception
{
boolean legal = true;
int ID = Integer.parseInt(in.readLine());
String userName = in.readLine();
String password = in.readLine();
int AID = Integer.parseInt(in.readLine());
String strSQL = "SELECT * FROM user WHERE userName='"+userName+"' AND ID<>"+ID;
if(db.getResult(strSQL).first())
{
legal = false;
}
if(legal)
{
strSQL = "SELECT * FROM user WHERE ID="+ID+" AND account='"+AID+"'";
if(db.getResult(strSQL).first())
{
strSQL = "UPDATE user SET userName ='"+userName+"', password ='"+password+
"' WHERE ID ="+ID;
if(db.updateSql(strSQL))
{
out.println("SUCCESS");
}
else
{
out.println("ERROR");
}
}
else
{
strSQL = "SELECT * FROM account WHERE accountID='"+AID+"'";
if(!db.getResult(strSQL).first())
{
strSQL = "UPDATE user SET userName ='"+userName+"', password ='"+password+"',account ='"+AID+
"' WHERE ID ="+ID;
if(db.updateSql(strSQL))
{
out.println("SUCCESS");
}
else
{
out.println("ERROR");
}
}
else
{
out.println("USED");
String name = in.readLine();
strSQL = "SELECT * FROM user WHERE account='"+AID+"' AND userName='"+name+"'";
if(db.getResult(strSQL).first())
{
strSQL = "UPDATE user SET userName ='"+userName+"', password ='"+password+"',account ='"+AID+
"' WHERE ID ="+ID;
if(db.updateSql(strSQL))
{
out.println("SUCCESS");
}
else
{
out.println("ERROR");
}
}
else
{
out.println("AFAILURE");
}
}
}
}
else
{
out.println("FAILURE");
}
}
public void updateShop()throws Exception
{
int ID = Integer.parseInt(in.readLine());
String shopType = in.readLine();
String shopInfo = in.readLine();
String strSQL = "UPDATE user SET shopType ='"+shopType+"', shopInfo ='"+shopInfo+
"' WHERE ID ="+ID;
if(db.updateSql(strSQL))
{
out.println("SUCCESS");
}
else
{
out.println("FAILURE");
}
}
public void openShop()throws Exception
{
int ID = Integer.parseInt(in.readLine());
String shopName = in.readLine();
String shopType = in.readLine();
String shopInfo = in.readLine();
boolean shoplegal = true;
String strSQL = "SELECT * FROM user WHERE shopName='"+shopName+"'";
if(db.getResult(strSQL).first())
{
shoplegal = false;
}
if(shoplegal)
{
strSQL = "UPDATE user SET type = 'Merchant', shopName ='"+shopName+"', shopType ='"+shopType+"', shopInfo ='"+shopInfo+
"' WHERE ID ="+ID;
if(db.updateSql(strSQL))
{
out.println("SUCCESS");
}
else
{
out.println("FAILURE");
}
}
else
{
out.println("SHOPFAILURE");
}
}
public void closeShop()throws Exception
{
int ID = Integer.parseInt(in.readLine());
String shopName = in.readLine();
String strSQL = "UPDATE user SET type = 'Customer', shopName ='', shopType ='', shopInfo ='' WHERE ID ="+ID;
db.updateSql(strSQL);
strSQL = "DELETE FROM goods WHERE shopName ='"+shopName+"'";
db.updateSql(strSQL);
}
public void viewUserInfo()throws Exception
{
int ID = Integer.parseInt(in.readLine());
String strSQL = "SELECT * FROM user WHERE ID="+ID;
rs = db.getResult(strSQL);
rs.next();
out.println(rs.getString(2));
out.println(rs.getString(3));
out.println(rs.getString(4));
}
public void withdraw()
{
try
{
int AID = Integer.parseInt(in.readLine());
double amount = Double.parseDouble(in.readLine());
String strSQL = "SELECT balance FROM account WHERE accountID='"+AID+"'";
rs = db.getResult(strSQL);
rs.next();
double balance = Double.parseDouble(rs.getString("balance"))-amount;
System.out.println("account:"+AID+" withdraw:"+amount+" now balance is:"+balance);
strSQL = "UPDATE account SET balance ='"+balance+"' WHERE accountID ='"+AID+"'";
db.updateSql(strSQL);
out.println("SUCCESS");
out.flush();
}
catch(Exception e)
{
out.println("FAILURE");
out.flush();
}
}
public void deposit()
{
try
{
int AID = Integer.parseInt(in.readLine());
double amount = Double.parseDouble(in.readLine());
String strSQL = "SELECT balance FROM account WHERE accountID='"+AID+"'";
rs = db.getResult(strSQL);
rs.next();
double balance = Double.parseDouble(rs.getString("balance"))+amount;
System.out.println("account:"+AID+" deposit:"+amount+" now balance is:"+balance);
strSQL="UPDATE account SET balance ='"+balance+"' WHERE accountID ='"+AID+"'";
db.updateSql(strSQL);
out.println("SUCCESS");
}
catch(Exception e)
{
out.println("FAILURE");
}
}
public void check()throws Exception
{
int AID = Integer.parseInt(in.readLine());
String strSQL = "SELECT * FROM account WHERE accountID='"+AID+"'";
rs = db.getResult(strSQL);
rs.next();
double balance = Double.parseDouble(rs.getString(3));
out.println(balance);
}
public void open()throws Exception
{
String shopName = in.readLine();
int ID = Integer.parseInt(in.readLine());
shopOpen[ID] = true;
System.out.println("shop:"+shopName+" open!");
ActivityShop++;
IP[ID] = socket.getInetAddress().toString().substring(1);
}
public void close()throws Exception
{
String shopName = in.readLine();
int ID = Integer.parseInt(in.readLine());
shopOpen[ID] = false;
System.out.println("shop:"+shopName+" closed!");
ActivityShop--;
}
public void outputInfo()throws Exception
{
String strSQL = "SELECT * FROM user WHERE type='"+"Merchant"+"'";
rs = db.getResult(strSQL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -