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

📄 mtrade.java

📁 移动商务平台
💻 JAVA
字号:
/**
 *
 ******************************************
 ***  to Kristy,SUN YAT-SEN UNIVERSITY  *** 
 ******************************************  
 *
 *mTrade.java
 *Create on November,2003
 *Company:LeftBank
 *
 *@author Ben,XIDIAN UNIVERSITY
 *@version 0.1
 *
 */

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import java.io.IOException;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.DataInputStream;	import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;	
import org.kxml.*;
import org.kxml.parser.*;

import javax.microedition.rms.*;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;

import java.lang.String;


/** 
 *
 *The class mTrade is the main class for the M-Trade
 *
 */ 

public class mTrade extends MIDlet implements CommandListener
{
       Display display = null;	       GetData gdata = null;
       GetImage gImage = null;	       private String[] picNames = null;
       private Vector vBrand = null;
       private Vector vQuantity = null;
       private Vector vPrice = null;
       private Hashtable htable = null;
       	
       private List menu = null;
       private Form inputform = null;	       private Form totalform = null;	       private Form detailform = null;	       private Form finishform = null;
       private ImageCanvas canvas;
         	
       static final String rsName = "image";
       int numOfPic;
       private TextField input = null;	       private String itemselect = null;
               
       private Command cmdGo= null;
       private Command cmdSave = null;	       private Command cmdTotal = null;
       private Command cmdBuy = null;
       private Command cmdBack = null;	       private Command cmdExit = null;	       private Command cmdChoose = null;
       private Command cmdContinue = null;
       
       RecordStore rs;	
   
   
       public void startApp()
       {
              display = Display.getDisplay(this);	              
	      /**
	       *create the splash screen
	       */		 
	      cmdGo = new Command("Go",Command.SCREEN,1);		        //connect the server to getdata
	      cmdExit = new Command("Exit",Command.EXIT,1);
	        //close the program		      canvas = new ImageCanvas();		      canvas.addCommand(cmdExit);		      canvas.addCommand(cmdGo);
	      canvas.setCommandListener(this);
	      display.setCurrent(canvas);	              
	      /**
	       *create main menu
	       */
	      cmdTotal = new Command("Total",Command.SCREEN,1);
	      menu = new List("Merchandise",List.IMPLICIT);
	      menu.addCommand(cmdTotal);
	      menu.addCommand(cmdExit);
	      menu.setCommandListener(this);
              
	      /**
	       *create detailform
	       */
	      cmdChoose = new Command("ChooseIt",Command.SCREEN,1);
	        //press it to input how many do you want to buy
	      cmdBack = new Command("Back",Command.BACK,2);
	        //back to menu
	      detailform = new Form("detail");
	      detailform.addCommand(cmdChoose);
	      detailform.addCommand(cmdBack);
	      detailform.setCommandListener(this);	 
              
	      /**
	       create inputform,this form is used to input quantity
	       */
	      htable = new Hashtable();
	        //save customer's choice
	      cmdSave = new Command("Save",Command.BACK,1);
	        //save customer's choice
	      inputform = new Form("input Form");		      inputform.addCommand(cmdSave);
	      inputform.setCommandListener(this);	              
	      /**
	       *create totalform,in this form,customer can see the total bill and confirm it
	       */
	      cmdBuy = new Command("Buy",Command.SCREEN,1);
	        //send customer's choice to server and update it
	      totalform = new Form("View Total");		      totalform.addCommand(cmdBack);
	      totalform.addCommand(cmdBuy);
              totalform.setCommandListener(this);	   
              /**
               *create finishform,it tells customer that trade has been finished
               */
              cmdContinue = new Command("Continue",Command.SCREEN,1);
                //restart the MIDlet after trade has been finished 
              finishform = new Form("Finish!");
              finishform.addCommand(cmdExit);
              finishform.addCommand(cmdContinue);
              finishform.append("Trade Is Successful.\n"+"\nContinue OR Exit?");  
              finishform.setCommandListener(this);     
               
       }//end startApp


       public void commandAction(Command c, Displayable d)
       {
              int totalbill = 0;
	      int a = 0;
              
	      if(c == cmdGo)
	      {
	      	    
                    Alert alert = new Alert("Connecting","Connecting,please wait...",null,AlertType.INFO);
                    alert.setTimeout(Alert.FOREVER);
                    display.setCurrent(alert);
         
	            int nums = menu.size();		            while(nums-->0){menu.delete(nums);}	
	              //when customer had buy things,MIDlet should update the menu,
	              //so the menu should be cleared.
	               
	            gdata = new GetData();
	 	      //Gdata Instantiated
	 	    gdata.parseData();	                      //GetData parseData method called,this method parses XML 
	 	    
	 	    vBrand = gdata.returnBrand();
	 	    vBrand.trimToSize();
	 	    vQuantity = gdata.returnStock();
	 	    vQuantity.trimToSize();
	 	    vPrice = gdata.returnPrice();
	 	    vPrice.trimToSize();
	 	      //return Brand,Quantity and Price

                    	 	    
         	    for(int i=1;i<=vBrand.size();i++)
	 	    {
	 	          menu.append
	 	          ((String)vBrand.elementAt(i-1)	+" "
	 		  +(String)vQuantity.elementAt(i-1)+" "		 		  +(String)vPrice.elementAt(i-1),null);		 	    }
	       	    display.setCurrent(menu);
	       	    alert = null;
              }//end cmdGo
       
     	      if(c == List.SELECT_COMMAND)
	      {
	            int i=0;
	            boolean isDown = false;
		    int num = menu.getSelectedIndex();			    itemselect	= (String)vBrand.elementAt(num);			    getpicNames();

                    //this loop runs to check if the image has been download  
		    if(numOfPic!=0)
		    {
		          for(i=0;i<numOfPic;i++)
			  {
			        if(picNames[i].equals(itemselect)) 
			        {
			             isDown = true;
			             break;
			        }
			  }				    }
		    
                    //download image if the image doesn't exist
		    if(!isDown)
		    {
			  String url = "http://localhost/m-trade/image/" + itemselect +	".png";				  System.out.println(url);
			  gImage = new GetImage(this,url,itemselect);
			  gImage.download();
		    }
		    ImageItem imgItem = new ImageItem(itemselect,showImage(2*i+2),3,
				                       "a image	exceeds	display's capacity");
		    clearForm(detailform);
		    detailform.append(imgItem);
		    detailform.append(new StringItem(null,"Brand:"+itemselect+"\n"));
		    detailform.append(new StringItem(null,"Price:"+(String)vPrice.elementAt(num)+"\n"));
		    detailform.append(new StringItem(null,"Made in China"));
	            display.setCurrent(detailform);
 	      }//end LIst.SELECT_COMMAND
		
	      if(c == cmdChoose)
	      {
		    /**customer input how many he/she wants*/
		    clearForm(inputform);
		    input = new TextField("Input Quantity","",5,TextField.NUMERIC);
		    inputform.append(input);
		    display.setCurrent(inputform);
              }
                
	      if(c == cmdSave)
	      {
	      	    /**save customer's choice*/
	      	    if(!(input.getString().equals("")))
	      	    {
	      	          htable.put(itemselect,input.getString());
	      	    }
	            display.setCurrent(menu);
	      }

	      if(c == cmdTotal)
	      {
	      	    /**show total bill*/
                    clearForm(totalform);
	      	    String str;
	      	    StringItem sitem;
	      	    for (Enumeration e = htable.keys(); e.hasMoreElements();) 
	      	    {
	      	          Object hkey = e.nextElement();
	      	    	  int indexitem = vBrand.indexOf((String)hkey);
	      	    	  int quan = Integer.parseInt((String)htable.get(hkey));
	      	    	  int price = Integer.parseInt((String)vPrice.elementAt(indexitem));
	      	    	  String total = new String();
	      	    	  total = total.valueOf(quan*price);
	      	    	  str = ((String)hkey) +"	    " +	total;
	      	    	  sitem = new StringItem("",str);		      	    	  totalform.append(sitem);
	      	          totalbill+=(quan*price);
	      	    }//end for
	      	    str = "Total"+"	       "+totalbill;
	      	    sitem = new StringItem("",str);		      	    totalform.append(sitem);
	      	    display.setCurrent(totalform);
	      }//end cmdTotal

	      if(c == cmdBack)
	      {
	            display.setCurrent(menu);
	      }

	      if(c == cmdBuy && !(htable.isEmpty()))
	      {
	      	    /**send customer's choice to server and update database*/
	      	    String url,url1,url2;
	      	    url1 = "http://localhost/m-trade/asp/order.asp?";
	      	    for (Enumeration e = htable.keys(); e.hasMoreElements();) 
	      	    {
	      	          Object hkey = e.nextElement();
	      	          int indexitem = vBrand.indexOf((String)hkey);
	      	          int quan = Integer.parseInt((String)htable.get(hkey));
	      	          int price = Integer.parseInt((String)vPrice.elementAt(indexitem));
	      	          url2 = "brand="+(String)hkey + "&" + "quan="+(String)htable.get(hkey) + "&" +"rate="+(quan*price);
	      	          url = url1+url2;
	      	          try
	      	          {
	      	          	  DataInputStream ins = Connector.openDataInputStream(url);
	      	          }
	      	          catch(IOException ex)
	      	          {
	      	          	  System.out.println("IOException occured");
	      	          }
                          display.setCurrent(finishform);
	      	    }//end for
	      }//end cmdBuy

	      if(c == cmdBuy && htable.isEmpty())
	      {
	      	    Alert erroralert = new Alert("ERROR","You Didn't Chose Any...",null,AlertType.ERROR);
	      	    erroralert.setTimeout(5000);
	      	    display.setCurrent(erroralert);		      }
              
              if(c == cmdContinue)
              {
                    /**start another trade*/
                    display.setCurrent(canvas);	
              }
              
	      if(c == cmdExit)
	      {
	      	    /**exit*/
	      	    this.destroyApp(true);
	      	    this.notifyDestroyed();		      }
	}//end CommandAction


	public void pauseApp()
	{
	}
	
	
	public void destroyApp(boolean b)
	{
  	}
 
 
        /**
         *get images' name form the rocordstore
         */
	public void getpicNames()
	{
	       try
	       {
		     rs = RecordStore.openRecordStore(rsName,true);
		     numOfPic = rs.getNumRecords()/2;
		     if(numOfPic!=0)
		     {
		           picNames = null;
		           picNames = new String[numOfPic];
		           for(int i=0;i<numOfPic;i++)
		           {
		                 picNames[i]= new String(rs.getRecord(2*i+1));
		           }
		     }//end if 
	       }//end try
	       catch(Exception e){}
	}//end getpicNames      
      
      
        /**
         *get image from the recordstore
         */
	public Image showImage(int n)
	{
	       Image image = null;
	       try
	       {
		     byte[] imageBytes = rs.getRecord(n);
		     image = Image.createImage(imageBytes,0,imageBytes.length);
	       }
	       catch(Exception e){}    
	       return image;
	}//end showImage

 
        /**
         *clear the form before display it
         */
	public void clearForm(Form objForm)
	{
	       int nums = objForm.size();		       while(nums-->0){objForm.delete(nums);}		}//end cleatForm  

}//end mTrade 
 
 





⌨️ 快捷键说明

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