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

📄 shopmanager.java

📁 一个基于局域网的c/s模式网上购物系统,功能比较全面.数据库为Access.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

class ShopManager implements ActionListener
{
	private Socket socket;
	private BufferedReader in;
	private PrintWriter out;
	private Merchant merchant;
	private JFrame f;
	private MyTable defaultModel,orderListModel;
	private JTable table,orderList;
	private JTextField amount,user;
	private JTextArea ad;
	private JButton switchButton;
	private String[] goodsInfo;
	private String message;
	private boolean isOpen;
	
	////////////////////店铺服务器///////////////////
	private class ShopServer extends Thread
	{
		private final int PORT = 8000;
	    private ServerSocket s;
	    private String Mname;
    
 		class ServeThread extends Thread
		{
		private Socket socket;
		private BufferedReader in;
		private PrintWriter out;
		private final int CPORT = 8001;
		private ServerSocket cs;
	
	
	
		class ChatDlg 
		{
			private JFrame dialog;
			private JTextArea echo,send;
			private boolean exit;
		    private Socket csocket;
		    private BufferedReader cin;
			private PrintWriter cout;
			private String Cname;

		
			class receive extends Thread
			{

				receive()throws IOException
				{
					start();
				}
	
				public void run()
				{
					String msg=null;
					String nextmsg=null;
					try
					{
						nextmsg=cin.readLine();
					}catch(IOException ioe){}
				
					while(true)
					{
						try
						{
							if(nextmsg.equals("CHATOVER"))
							{
								JOptionPane.showMessageDialog(null,"        对方已退出聊天!","提示",1);
								exit = true;
								break;
							}
		
							else if(echo.getLineCount()>30)
							{
								msg=echo.getText();
								msg=msg.substring(msg.indexOf("\n")+1);
								echo.setText(msg+'\n'+Cname+":"+nextmsg);
							}
							
							else
							{
								if(!echo.getText().equals(""))
								{
									echo.append('\n'+Cname+":"+nextmsg);
								//	echo.setText(echo.getText()+'\n'+Cname+":"+nextmsg);
								}
								else
								{
									echo.setText(Cname+":"+nextmsg);
								}
							}
						nextmsg=cin.readLine();	
	
						}catch(IOException e){break;}
					}System.out.println("CHAT END");
				}
			}
	
			public ChatDlg(String name)
			{
				try
				{
					csocket = cs.accept();
					Cname = name;
				
					cin = 
	    		 	 	new BufferedReader(
   		 		 	 	  new InputStreamReader(
   		 		 	 	    csocket.getInputStream()));
   		 			cout = 
   	 				  new PrintWriter(
    				    new BufferedWriter(
    				      new OutputStreamWriter(
    				        csocket.getOutputStream())), true);
    				        
    				cout.println(Mname);
    				        
    			}catch(Exception ex){}
				
				exit = false;
				
				dialog = new JFrame("Chat with customer:"+name);
			
				JPanel textPanel = new JPanel();
				textPanel.setLayout(new GridBagLayout());
				textPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK,2),
	  		                "聊天信息",TitledBorder.CENTER,TitledBorder.TOP));
	  		
	 		    GridBagConstraints gbc = new GridBagConstraints();
	 		    gbc.anchor = GridBagConstraints.WEST; 
	 		    gbc.insets = new Insets(2,2,2,2); 
   	 
			    echo = new JTextArea(10,30);
			    JPanel t1 = new JPanel();
				t1.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
				t1.setLayout(new GridLayout(0,1));
				t1.add(echo);
		    
		    	send = new JTextArea(3,30);
		    	send.addKeyListener(new KeyListener(){
		    		public void keyPressed(KeyEvent e)
  					{
  						int keyCode = e.getKeyCode();
  						if(keyCode==KeyEvent.VK_ENTER && e.isControlDown())
  						{
  							cout.println(send.getText());
							try
							{
						
								if(echo.getLineCount()>30)
								{
									String msg=echo.getText();
									msg=msg.substring(msg.indexOf('\n')+1);
									echo.setText(msg+'\n'+Mname+":"+send.getText());
								}
								else
								{
									if(echo.getText().equals(""))
									{
										echo.setText(Mname+":"+send.getText());
									}
									else
									{
										echo.append('\n'+Mname+":"+send.getText());
									//	echo.setText(echo.getText()+'\n'+Mname+":"+send.getText());
									}
								}
		
							}catch(Exception ex){}
							send.setText("");
	  					}
	  				}
  		
  					public void keyTyped(KeyEvent e)
  					{
  			
  					}
  		
  					public void keyReleased(KeyEvent e)
  					{
  			
  		 			};
  				});
  				
			    JPanel t2 = new JPanel();
				t2.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
				t2.setLayout(new GridLayout(0,1));
				t2.add(send);

	    		gbc.gridy=1;
	    		gbc.gridx=0;
	   	 		textPanel.add(new JLabel("Receive:"),gbc);
				gbc.gridy=2;
				textPanel.add(new JScrollPane(t1),gbc);
				gbc.gridy=3;
				gbc.gridx=0;
				textPanel.add(new JLabel("Send:"),gbc);
				gbc.gridy=4;
				textPanel.add(new JScrollPane(t2),gbc);
	
				JPanel p = new JPanel();
			
				JButton b = new JButton("发送");
				b.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						cout.println(send.getText());
						try
						{
						
							if(echo.getLineCount()>30)
							{
								String msg=echo.getText();
								msg=msg.substring(msg.indexOf('\n')+1);
								echo.setText(msg+'\n'+Mname+":"+send.getText());
							}
							else
							{
								if(echo.getText().equals(""))
								{
									echo.setText(Mname+":"+send.getText());
								}
								else
								{
									echo.append('\n'+Mname+":"+send.getText());
									//	echo.setText(echo.getText()+'\n'+Mname+":"+send.getText());
								}
							}
			
						}catch(Exception ex){}
						send.setText("");
					}
				});					

				p.add(b);
			
				b = new JButton("退出");
				b.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						if(exit)
						{
							try
							{
								dialog.dispose();
								cout.println("CHATOVER");
								cin.close();
								cout.close();
								csocket.close();
							}catch(Exception ex){}
						}
						else
						{
							JOptionPane.showMessageDialog(null,"        对方尚未退出聊天!","退出失败",2);
						}
					}
				});
			
				p.add(b);
		
				try
				{
					new receive();		
	
					dialog.add(textPanel,BorderLayout.NORTH);
					dialog.add(p,BorderLayout.SOUTH);
					dialog.setBounds(365,310,360,400);
					dialog.setResizable(false);
		  	    	dialog.setVisible(true);
		  	    	dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
 	 
 		  		    dialog.addWindowListener(new WindowAdapter(){
 			       		public void windowClosing(WindowEvent e)
   			     		{
   			     			if(exit)
							{
								try
								{
									dialog.dispose();
									cout.println("CHATOVER");
									cin.close();
									cout.close();
									csocket.close();
								}catch(Exception ex){}
							}
							else
							{
								JOptionPane.showMessageDialog(null,"        对方尚未退出聊天!","退出失败",2);
							}
   	    		 		}
   		     		});
   		     
   	   	 		}catch(IOException ex){}
			}
		}
	
		public ServeThread(Socket s)throws IOException
		{
   		 	socket = s;
   		 	in = 
   		 	  new BufferedReader(
   		 	    new InputStreamReader(
    		      socket.getInputStream()));
    		out = 
    		  new PrintWriter(
    		    new BufferedWriter(
    		      new OutputStreamWriter(
    		        socket.getOutputStream())), true);
    		        
    		cs = new ServerSocket(CPORT);        
    		
    		start();
    	}
    	
    	
    	public void trade()throws Exception
    	{
    		
    		String buyerName = in.readLine();
    		String goodsName = in.readLine();
    		int num = Integer.parseInt(in.readLine());
    		double price = Double.parseDouble(in.readLine());
    	    		
    		System.out.println(buyerName+" want to buy "+goodsName);
    		
    		int accept = JOptionPane.showConfirmDialog(null,
    					buyerName+"向您发出定单"+"\n"+"    商品:"+goodsName+"\n"+"    数量:"+num+"\n"+"    出价:"+price+"\n"+"是否确认交易?","定单",JOptionPane.YES_NO_OPTION);
    			
    		if(accept==JOptionPane.YES_OPTION)
    		{
    			out.println("ACCEPT");
    			int index=-1;
    			for(int i=0;i<table.getRowCount();i++)
    			{
    				if(table.getValueAt(i,0).equals(goodsName))
    				{
    					index = i;
    					break;
    				}
    			}
    			
    			num = Integer.parseInt(table.getValueAt(index,2).toString()) - num;
    			table.setValueAt(num,index,2);
    			
    		}
    			
    		else
    		{
    			out.println("REFUSE");
    		}
    	}
    	
    	public void sendMsg()throws Exception
    	{
    		out.println(message);
    		out.println("OVER");
    	}
    	
    	public void setMsg()throws Exception
    	{
    		    					
    		String msg = in.readLine();
    		while(!msg.equals("OVER"))
    		{
    			message +=msg+"\n";
    			msg = in.readLine();
    		}
    	}
   	 
   		public void run()
   		{
   		 	try
   		 	{
   		 		while(true)
   		 		{
   	 				String cmd = in.readLine();
   	 			
   	 				if (cmd.equals("END"))
    				{
    					break;
    				}
    			    			
    				else if (cmd.equals("BUY"))
    				{
    					trade();
    				}
    				
    				else if(cmd.equals("CHAT"))
    				{
    					String Cname = in.readLine();
    					new ChatDlg(Cname);
    				}
    				
    				
    				else if(cmd.equals("GETMSG"))
    				{
    					sendMsg();
    				}
    				
    				else if(cmd.equals("MSG"))
    				{
    					setMsg();
    				}
    			}
   			}
   			catch (Exception e) {e.printStackTrace();} 
   			
   			finally {
		      try {
	   		   	in.close();
	   		   	out.close();
	   		    socket.close();
	   		    cs.close();
	   		    System.out.println("closing connect with a customer");
	    	  } catch(IOException e) {}
	    	}
		}
		}
	
		public ShopServer(String name)
		{
			Mname = name;
  			start();  	
  		}
  	
  		public void run()
  		{
  			try
    		{
  	   		 	s = new ServerSocket(PORT);
    			System.out.println("Server Started");
    	
    			while(true)
    			{
    				Socket socket = s.accept();
    		    	try
    		    	{
    		    		new ServeThread(socket);
    		    	}
    		    	catch(IOException e)
    		    	{
    		    		socket.close();
 				    }
    		  	}
    		}catch(IOException e){}
    		finally
    		{
    			try
    		  	{
    		  		s.close();
    		  	}catch(IOException e){}
    		}
  		} 		
	}	 
	
	////////////////////定单状态监听类///////////////
	private class OrderCheck extends Thread
	{
		private String echoMsg,next;
		private PrintWriter os;
		private BufferedReader is;
		private Socket listener;
		OrderCheck()
		{
			try
			{
				String ip = socket.getInetAddress().getHostAddress();
				listener = new Socket(ip, 8080);
				
				is = 
	    		  new BufferedReader(
	        		new InputStreamReader(
	        		  listener.getInputStream()));
	    
	    		os =
	    			 new PrintWriter(
    				  new BufferedWriter(
     				   new OutputStreamWriter(
       					listener.getOutputStream())),true);
				
			}catch(Exception ex){ex.printStackTrace();}
			echoMsg = "";
			start();
		}
		
		public void run()
		{
			try
			{
				while(true)
				{
					os.println("CHECKORDER");
					os.println(merchant.getId());
					os.println(merchant.getShopName());
					os.println(merchant.getAccount());
					
					next = is.readLine();
					
					
					
					if(next.equals("UPDATED"))
					{
						amount.setText(merchant.checkBalance(in,out)+"");
						this.sleep(1000);
						viewOrder();
					}
					
					else if(next.equals("SKIP"))
					{

					}

					else

⌨️ 快捷键说明

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