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

📄 mainserver.java

📁 一个基于局域网的c/s模式网上购物系统,功能比较全面.数据库为Access.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		
		while(rs.next())
		{
			out.println(rs.getString(7));
			out.println(rs.getString(6));
			out.println(rs.getString(2));
			if(shopOpen[Integer.parseInt(rs.getString(1))])
			{
				out.println("open");
			}
			else
			{
				out.println("closed");
			}
			out.println(rs.getString(8));
		}
		out.println("OVER");
	  }
	  
	  public void outputGoods()throws Exception
	  {
	  	String shopName = in.readLine();
	  	String strSQL = "SELECT * FROM goods WHERE shopName='"+shopName+"'";
		rs = db.getResult(strSQL);
		
		while(rs.next())
		{
			out.println(rs.getString(3));
			out.println(rs.getString(5));
			out.println(rs.getString(4));
			out.println(rs.getString(6)+"");
		}
		out.println("OVER");
	  }

	  public void addGoods()throws Exception
	  {
	  	String shopName = in.readLine();
	  	String goodsName = in.readLine();
	  	String price = in.readLine();
	  	String quantity = in.readLine();
	  	
	  	try
	  	{
	  		String strSQL = "INSERT INTO goods(shopName,goodsName,price,quantity) VALUES('"
	  				+shopName+"','"+goodsName+"','"+price+"','"+quantity+"')";
   			
   			if(db.updateSql(strSQL))
   			{
   				out.println("SUCCESS");
   			}
   			else
   			{
   				out.println("FAILURE");
   			}
	  	}
	  	
	  	catch(Exception e)
       	{
       		out.println("FAILURE");
       	}
	  }	  
	  
	  public void updateGoods()throws Exception
	  {
	  	String shopName = in.readLine();
	  	String goodsName = in.readLine();
	  	String price = in.readLine();
	  	String quantity = in.readLine();
	  	String info = in.readLine();
	  	
	  	try
	  	{
	  		String strSQL = "UPDATE goods SET price ='"+price+"', quantity ='"+quantity+"',info ='"+info+
	  			   "' WHERE shopName ='"+shopName+"' AND goodsName ='"+goodsName+"'";
   			
   			if(db.updateSql(strSQL))
   			{
   				out.println("SUCCESS");
   			}
   			else
   			{
   				out.println("FAILURE");
   			}
	  	}
	  	
	  	catch(Exception e)
       	{
       		e.printStackTrace();
       		out.println("FAILURE");
       	}

	  }
	  
	  public void deleteGoods()throws Exception
	  {
	  	String shopName = in.readLine();
	  	String goodsName = in.readLine();
	  	
	  	try
	  	{
	  		String strSQL = "DELETE FROM goods WHERE shopName ='"+shopName+"' AND goodsName ='"+goodsName+"'";
   			
   			if(db.updateSql(strSQL))
   			{
   				out.println("SUCCESS");
   			}
   			else
   			{
   				out.println("FAILURE");
   			}
	  	}
	  	
	  	catch(Exception e)
       	{
       		out.println("FAILURE");
       	}
	  }
	  
	  public void addOrder()throws Exception
	  {
	  	String customer = in.readLine();
	  	String shop = in.readLine();
	  	String name = in.readLine();
	  	String quantity = in.readLine();
	  	String price = in.readLine();
	  	int orderNumber;
	  	do
	  	{
	  		orderNumber = (int)(Math.random()*90000+10000);
	  	  	String strSQL = "SELECT orderNumber FROM orderlist WHERE orderNumber ="+orderNumber;
	  	  	rs = db.getResult(strSQL);
	  	}while(rs.first());
	  	
	  	String strSQL = "INSERT INTO orderlist(orderNumber,customer,shop,name,quantity,price,status) VALUES('"
	  						+orderNumber+"','"+customer+"','"+shop+"','"+name+"','"+quantity+"','"+price+"','unconfirmed')";

	  	if(db.updateSql(strSQL))
	  	{
	  		strSQL = "SELECT quantity FROM goods WHERE shopName='"+shop+"' AND goodsName ='"+name+"'";
			rs = db.getResult(strSQL);
			rs.next();
			int nowQuantity = Integer.parseInt(rs.getString("quantity"));
			nowQuantity = nowQuantity-Integer.parseInt(quantity);
			strSQL = "UPDATE goods SET quantity ='"+nowQuantity+"' WHERE shopName ='"+shop+"' AND goodsName ='"+name+"'";
			db.updateSql(strSQL);
	  	}
	  	else
	  	{
	  		
	  	} 
	  	
	  	out.println(orderNumber);
	  }
	  
	  public void confirmOrder()throws Exception
	  {
	  	String customer = in.readLine();
	  	String price = in.readLine();
	  	int orderNumber = Integer.parseInt(in.readLine());
	  	String shop = in.readLine();
	  	
	  	String strSQL = "UPDATE orderlist SET status ='confirmed' WHERE orderNumber ="+orderNumber;
	  			   	
	  	if(db.updateSql(strSQL))
	  	{
	  		strSQL = "SELECT ID FROM user WHERE shopName ='"+shop+"'";
	  		rs = db.getResult(strSQL);
			rs.next();
			int ID = rs.getInt("ID");
			hasOrder[ID] = true;
			
	  		strSQL = "SELECT account FROM user WHERE userName='"+customer+"'";
			rs = db.getResult(strSQL);
			rs.next();
			int AID = Integer.parseInt(rs.getString("account"));
			
			strSQL = "SELECT balance FROM account WHERE accountID='"+AID+"'";
   			rs = db.getResult(strSQL);
   			rs.next();
   			double balance = Double.parseDouble(rs.getString("balance"))-Double.parseDouble(price);

			if(balance >= 0)
			{
				strSQL = "UPDATE account SET balance ='"+balance+"' WHERE accountID ='"+AID+"'";
   				db.updateSql(strSQL);
   			
   				out.println("CONFIRMED");
   			}
   			else
   			{
   				out.println("FAILED");
   			}
	  	}
	  	else
	  	{
	  		out.println("FAILED");
	  	}
	  }
	  
	  public void cancelOrder()throws Exception
	  {
	  	int orderNumber = Integer.parseInt(in.readLine());
	  	String shop = in.readLine();
	  	String strSQL = "UPDATE orderlist SET status ='cancel' WHERE orderNumber ="+orderNumber;
	  			   	
	  	if(db.updateSql(strSQL))
	  	{
	  		strSQL = "SELECT ID FROM user WHERE shopName ='"+shop+"'";
	  		rs = db.getResult(strSQL);
			rs.next();
			int ID = rs.getInt("ID");
			hasOrder[ID] = true;
			
	  		out.println("CANCELED");
	  	}
	  	else
	  	{
	  		out.println("FAILED");
	  	}
	  }
	  
	  public void outputOrder()throws Exception
	  {
	  	String shopName = in.readLine();
	  	String strSQL = "SELECT orderNumber,customer,name,quantity FROM orderlist WHERE shop='"+shopName+"' AND status ='accept'";
		rs = db.getResult(strSQL);
		
		while(rs.next())
		{
			out.println(rs.getString("orderNumber"));
			out.println(rs.getString("customer"));
			out.println(rs.getString("name"));
			out.println(rs.getString("quantity"));
		}
		out.println("OVER");
	  }
	  
	  public void checkOrder()throws Exception
	  {
	  	int ID = Integer.parseInt(in.readLine());
	  	String shop = in.readLine();
	  	int AID = Integer.parseInt(in.readLine());
		if(hasOrder[ID])
		{
			DataBase tempdb = new DataBase();
		    ResultSet temprs;
		    boolean haveCancel = false;
	  		String name,customer,cancelMsg="";
		  	int quantity,nowQuantity,orderNumber;
		  	double balance=0,price,incomes;
	  	
			String strSQL = "SELECT orderNumber,customer,name,quantity,price,status,balance FROM orderlist,user,account WHERE shop='"
	  					+shop+"' AND shopName='"+shop+"' AND accountID ='"+AID+"' AND status<> 'accept' AND status<>'completed' AND status<>'unconfirmed'";
			rs = db.getResult(strSQL);
			if(rs.next())
			{
				balance = Double.parseDouble(rs.getString("balance"));
			

				do
				{
					if(rs.getString("status").equals("confirmed"))
					{
						orderNumber = Integer.parseInt(rs.getString("orderNumber"));
						name = rs.getString("name");
						quantity = Integer.parseInt(rs.getString("quantity"));
						price = Double.parseDouble(rs.getString("price"));
						balance += price;
				
	
						strSQL = "UPDATE orderlist SET status ='accept' WHERE orderNumber ="+orderNumber;
			   			tempdb.updateSql(strSQL);
		
					}
					
					else if(rs.getString("status").equals("cancel"))
					{
						haveCancel = true;
						
						orderNumber = Integer.parseInt(rs.getString("orderNumber"));
						customer = rs.getString("customer");
						name = rs.getString("name");
						quantity = Integer.parseInt(rs.getString("quantity"));
						price = Double.parseDouble(rs.getString("price"));
						
								   			
			   			strSQL = "SELECT quantity FROM goods WHERE shopName='"+shop+"' AND goodsName ='"+name+"'";
						temprs = tempdb.getResult(strSQL);
						temprs.next();
						nowQuantity = Integer.parseInt(temprs.getString("quantity"));
						nowQuantity = nowQuantity+quantity;
						strSQL = "UPDATE goods SET quantity ='"+nowQuantity+"' WHERE shopName ='"+shop+"' AND goodsName ='"+name+"'";
						tempdb.updateSql(strSQL);
						
						
						cancelMsg += customer+"取消了定单:  商品:"+name+", 数量:"+quantity+", 出价:"+price+"\n";
						
						strSQL = "DELETE FROM orderlist WHERE orderNumber ="+orderNumber;
		   				tempdb.updateSql(strSQL);
					}
				}while(rs.next());
				
				strSQL = "UPDATE account SET balance ='"+balance+"' WHERE accountID ='"+AID+"'";
			   	db.updateSql(strSQL);
		
			   	if(haveCancel)
			   	{
			   		out.println(cancelMsg);
			   		out.println("OVER");
			   		out.flush();
			   	}
			   	
			   	else
			   	{
			   		out.println("UPDATED");
			   		out.flush();
			   	}
		   	
		   	
			   	tempdb.closeConnection();
			   	hasOrder[ID] = false;
		   	}
		   	
		   	else
		   	{
		   		out.println("SKIP");
				out.flush();
		   	}
		}
		
		else
		{
			out.println("SKIP");
			out.flush();
		}
	
	  }
	  
	  public void completeOrder()throws Exception
	  {
	  	int orderNumber = Integer.parseInt(in.readLine());
	  	
	  	String strSQL = "UPDATE orderlist SET status ='completed' WHERE orderNumber ="+orderNumber;
	  	db.updateSql(strSQL);
	  }
	  
	  public void search()throws Exception
	  {	  
	  	String shopName = in.readLine();
	  	String goodsName = in.readLine();
	  	
	  	String strSQL = "SELECT * FROM goods WHERE shopName='"+shopName+"' AND goodsName LIKE '%"+goodsName+"%'";
		rs = db.getResult(strSQL);
		
		while(rs.next())
		{
			out.println(rs.getString(3));
			out.println(rs.getString(5));
			out.println(rs.getString(4));
		}
		out.println("OVER");
	  }
	  
	  public void connect()throws Exception
	  {
	  	String shopName = in.readLine();
	  	
	  	String strSQL = "SELECT * FROM user WHERE shopName='"+shopName+"'";
		rs = db.getResult(strSQL);
		rs.next();
		int ID = Integer.parseInt(rs.getString(1));
		
		out.println(IP[ID]);
	  }
	  
	  public void setBroadcast()throws Exception
	  {
	  	String next = in.readLine();
	  	while(!next.equals("OVER"))
	  	{
	  		systemBroadcast += next + "\n";
	  		next = in.readLine();
	  	}
	  	
	  	if(systemBroadcast.length()>255)
	  	{
	  		systemBroadcast = systemBroadcast.substring(systemBroadcast.lastIndexOf("\n"));
	  	}
	  }
	  
	  public void broadcast()throws Exception
	  {
	  	out.println(systemBroadcast);
	  	out.println("OVER");
	  }


		

  		
  		
	  public void run()
	  {
	  	System.out.println("Connected with a client from:"+socket.getInetAddress().toString().substring(1));
	  	
	    try
	    {
	      while (true) 
	      {
      	
	        String cmd = in.readLine();
	      		
	        if (cmd.equals("END"))
	        {
	        	break;
			}

			else if (cmd.equals("LOGINC"))
	        {
	        	this.loginCustomer();
	        }
	        
	        else if (cmd.equals("LOGINM"))
	        {
	           	this.loginMerchant();
	        }
	        
	        else if (cmd.equals("LOGON"))
	        {
	        	this.logon();	
	        }
	        
			else if (cmd.equals("LOGOUT"))
	        {
	        	this.logout();
			}
			
			else if (cmd.equals("UPDATEUSER"))
	        {
	        	this.updataUser();
			}
			
			else if(cmd.equals("UPDATESHOP"))
			{
				this.updateShop();
			}
			
			else if(cmd.equals("OPENSHOP"))
			{
				this.openShop();
			}
			
			else if(cmd.equals("CLOSESHOP"))
			{
				this.closeShop();
			}
			
			else if (cmd.equals("VIEW"))
			{
				this.viewUserInfo();
			}
			
	        else if (cmd.equals("WITHDRAW"))
	        {
	        	this.withdraw();
	        }
	        
	        else if (cmd.equals("DEPOSIT"))
	        {
	        	this.deposit();
	        }
	        
	        else if (cmd.equals("OPEN"))
	        {
	        	this.open();
	        }
	        
	        else if(cmd.equals("CLOSE"))
	        {
	        	this.close();
	        }
	        
	        else if (cmd.equals("CREATMALL"))
	        {
	        	this.outputInfo();
	        }
	        
	        else if (cmd.equals("CREATSHOP"))
	        {
	        	this.outputGoods();
	        }
	        
	        else if (cmd.equals("CHECK"))
	        {
	        	this.check();
	        }
	        
	        else if (cmd.equals("ADD"))
	        {
	        	this.addGoods();
	        }
	        
	        else if (cmd.equals("EDIT"))
	        {
	        	this.updateGoods();
	        }
	        
	        else if (cmd.equals("DELETE"))
	        {
	        	this.deleteGoods();
	        }
	        
	        else if (cmd.equals("SEARCH"))
	        {
	        	this.search();
	        }
	        
	        else if (cmd.equals("CONNECT"))
	        {
	        	this.connect();
	        }
	        
	        else if(cmd.equals("AD"))
	        {
	        	this.setBroadcast();
	        }
	        
	        else if(cmd.equals("BROADCAST"))
	        {
	        	this.broadcast();
	        }
	        
	        else if(cmd.equals("ADDORDER"))
	        {
	        	this.addOrder();
	        }
	        
	        else if(cmd.equals("CONFIRM"))
	        {
	        	this.confirmOrder();
	        }
	        
	        else if(cmd.equals("CANCEL"))
	        {
	        	this.cancelOrder();
	        }
	        
	        else if(cmd.equals("CHECKORDER"))
	        {
	        	this.checkOrder();
	        }
	        
	        else if(cmd.equals("OUTPUTORDER"))
	        {
	        	this.outputOrder();
	        }
	        
	        else if(cmd.equals("SENT"))
	        {
	        	this.completeOrder();
	        }
	        
	        

	      }
	      
	      System.out.println("closing...");
	    } 
	    catch (Exception e)
	    {
	    }
	    finally {
	      try {
	      	in.close();
	      	out.close();
	        socket.close();
	      } catch(IOException e) {}
	    }
	  }
	}
	

	
	
	public static void main(String[] args)throws Exception
	{
		
		ActivityShop=0;
		systemBroadcast="";

		semaphore = new int[200];
		shopOpen = new boolean[200];
		userActive = new boolean[200];
		hasOrder = new boolean[200];
		IP = new String[200];

		ServerSocket s = new ServerSocket(PORT);
	    System.out.println("Server Started");
    	try 
    	{
      		while(true)
      		{
        		Socket socket = s.accept();
        		 
        		try 
        		{	        		
          			new ServeOne(socket);
          			
        		}
        		catch(IOException e) 
        		{
        			e.printStackTrace();
        			socket.close();
        		}
      		}
    	}
    	finally
    	{
    		System.out.println("Close All...");
    	 	s.close();
     	}
    	
	}
}
	


⌨️ 快捷键说明

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