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

📄 cityguide.java

📁 本书通过实例介绍了嵌入式编程的方法与技巧书中例子具有实用性
💻 JAVA
字号:
/*
 * CityGuide.java
 * This application is for Tour Guide i.e to visit the places in a particular City
 * and know the details of that place.
 */

package TourGuide;

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

public class CityGuide extends MIDlet implements CommandListener
{

    HttpConnection hc = null;

    private Display display = null;

    Command quit = null;
    Command ok = null;
    Command details=null;
    Command inform=null;
    Command back1 = null;

    Form frm = null;

    Ticker t =null;

    List menu = null;
    List menuc = null;
    List menud = null;
    List menui=null;

    String url,temp,str,item,tempp,tempm;
    StringItem cityname;
    StringItem st1 = null,st2 = null,st3 = null,st4 = null,st5 = null,st6 = null;

    // Initialize the Display and place system controls in the Constructor..

    public CityGuide()
    {
        display = Display.getDisplay(this);
        quit = new Command("Quit",Command.SCREEN,1);
        ok = new Command("OK",Command.OK,2);
        details = new Command("SELECT",Command.SCREEN,2);
        inform = new Command("CLICK",Command.SCREEN,2);
        back1 = new Command("BACK",Command.BACK,2);
        t = new Ticker("Welcome To Electronic City Guide" );
    }

     /**
	  * Initialize all the classes to be used in the program here (startApp())..
	 */

    public void startApp()
    {
            try {
                System.out.println("Started Application");
                cityDetails(str);
            }catch(IOException ie){
				System.out.println("start App after cityDetails(str)"+ie);
			}

    } // end of startApp()

    /**
    * This is a method to retrieve Cities from Database using JSP
    */
    private void cityDetails(String str) throws IOException
    {
        StringBuffer sb = new StringBuffer();
        InputStream is = null;
        HttpConnection hc = null;

        try{

        long len = 0;
        int ch = 0;
        String name="";
        hc = (HttpConnection)Connector.open("http://localhost:8080/Cities.jsp");
        is = hc.openInputStream();
        len = hc.getLength();
        if(len !=-1){
            for(int i=0;i<len;i++){
                if((ch = is.read()) !=-1)
                {
                    sb.append((char)ch);
                 }
            }
          }else{
            while(( ch = is.read()) !=-1){
                len = is.available() ;
	        sb.append((char)ch);

               }
            }

        String str1 = sb.toString();
        str1.trim();
        menu = new List("Please Select a City ",List.IMPLICIT);
        menu.setTicker(t);
        int index = str1.indexOf("#");

            while(index != -1)
            {

                index = str1.indexOf("#");

                if(index != -1)
                {
                String item = str1.substring(0,str1.indexOf("#"));
                str1 = str1.substring(str1.indexOf("#")+1);
                item=item.trim();
                 menu.append(item,null);
                 menu.addCommand(ok);
                 menu.addCommand(quit);
                 menu.setCommandListener(this);

                 display.setCurrent(menu);
                }

            }

        }catch(Exception e){System.out.println(e+"Exception In Menu");}
         finally{
             try{
                is.close();
                hc.close();
             }catch(Exception e){ System.out.println(e+"Exception in Finally");}

        }
    } // end of cityDetails(String str)

	/**
    * This is a method to retrieve Details when a particular city is Clicked
    */
    private void cityClicked()
    {
                List down = (List)display.getCurrent();
                temp = down.getString(down.getSelectedIndex());

      if(temp.equals("Washington D.C.")){
              categoryDetails(temp);
                }
      else if(temp.equals("Chicago"))
      {
          categoryDetails(temp);
      }
    }  //end of cityClicked()

    /**
	    * This is a method to retrieve Category Details of a paticular City
	    * from Database using JSP
    */
    private void categoryDetails(String temp)
    {
                     StringBuffer sb1 = new StringBuffer();
                     InputStream is1 = null;
                     HttpConnection hcp = null;

        try{

        long lenp = 0;
        int ch = 0;
        String name=" ";
        temp = temp.replace(' ','_');

        String url ="http://localhost:8080/Categories.jsp?City="+temp;

        hcp = (HttpConnection)Connector.open(url);
        is1 = hcp.openInputStream();
        lenp = hcp.getLength();

         if(lenp !=-1){
           for(int i=0;i<lenp;i++){
                if((ch = is1.read()) !=-1)
                {
                    sb1.append((char)ch);

                 }
            }
         }else{
            while(( ch = is1.read()) !=-1){
                lenp = is1.available() ;
	        sb1.append((char)ch);
               }
            }
        String str1 = sb1.toString();
        str1.trim();

         cityname=new StringItem("Select Category ",temp);
         menuc = new List("Please Select a Category",List.IMPLICIT);
         int index = str1.indexOf("#");
         while(index != -1)
            {
               index = str1.indexOf("#");
             if(index != -1)
                {
                String item = str1.substring(0,str1.indexOf("#"));
                str1 = str1.substring(str1.indexOf("#")+1);
                item=item.trim();

                menuc.append(item,null);
                menuc.addCommand(details);
                menuc.addCommand(back1);
                menuc.setCommandListener(this);
                display.setCurrent(menuc);
                }

            }


        }catch(Exception e){System.out.println(e+"Exception In Menu city clicked");}
         finally{
             try{
                is1.close();
                hcp.close();
             }catch(Exception e){ System.out.println(e+"Exception in Finally in city clicked");}

     }

    }  // end of categoryDetails(String temp)

    /**
    * This is a method for Action Performed i.e when a Command is Clicked
    *  i.e. Event handling routine..
    */
     public void commandAction(Command c,Displayable d)
     {
        if(c == quit){
            destroyApp(true);
        }
        else if(c==ok)
        {
            cityClicked();
        }
        else if(c==details)
        {
             List down = (List)display.getCurrent();
             String cname=cityname.getText();
             tempp = down.getString(down.getSelectedIndex());
             informationDetails(cname,tempp);
        }
        else if(c==inform)
        {
             List down = (List)display.getCurrent();
             String cname=cityname.getText();
             tempm = down.getString(down.getSelectedIndex());
             informationCategory(cname,tempm);
        }
        else if(c==back1)
        {
             try{
                    String cname=cityname.getText();
                    cityDetails(cname);
             }
             catch(Exception p)
             {
                 System.out.println("Exception in back "+p);
             }
        }


     }  //end of commandAction(Command c,Displayable d)

     /**
    * This is a method to retrieve Information when a particular city
    * and Name of the  Category is Clicked
    */
     private void informationCategory(String cname,String tempm)
     {

                     StringBuffer sbc = new StringBuffer();
                     InputStream isc = null;
                     HttpConnection hcc = null;
                     int count = 1;

        try{

        long lenc = 0;
        int ch = 0;
        String name=" ";
        cname = cname.replace(' ','_');
        tempm=tempm.replace(' ','_');
        String param=cname+" & name="+tempm;
        param = param.replace(' ','_');
        String url ="http://localhost:8080/NameCategory.jsp?city="+param;
        hcc = (HttpConnection)Connector.open(url);
        isc = hcc.openInputStream();
        lenc = hcc.getLength();
            if(lenc !=-1){
            for(int i=0;i<lenc;i++){
                if((ch = isc.read()) !=-1)
                {
                    sbc.append((char)ch);

                 }
            }
          }else{
            while(( ch = isc.read()) !=-1){
                lenc = isc.available() ;
	        sbc.append((char)ch);

               }
            }

        String str1 = sbc.toString();

        str1.trim();


         menui = new List("Information ",List.IMPLICIT);
          int index = str1.indexOf("#");

            while(index != -1)
            {

                index = str1.indexOf("#");

              if(index != -1)
                {
                  String item = str1.substring(0,str1.indexOf("#"));
                  str1 = str1.substring(str1.indexOf("#")+1);
                  item=item.trim();
                  //Font f=STYLE_BOLD;
                   if(count == 1)
                {
                    st1 = new StringItem("Description : ",item);
                }
                if(count == 2)
                {
                    st2 = new StringItem("Timing : ",item);
                }
                if(count == 3)
                {
                    st3 = new StringItem("Entry : ",item);
                }
                if(count == 4)
                {
                    st4 = new StringItem("RecomTime : ",item);
                }
                if(count == 5)
                {
                    st5 = new StringItem("PublicTransport : ",item);
                }
                if(count == 6)
                {
                    st6 = new StringItem("PrivateTransport : ",item);
                }

                count++;
               }

            }

                 frm =new Form("Details");
                 frm.append(st1);
                 frm.append(st2);
                 frm.append(st3);
                 frm.append(st4);
                 frm.append(st5);
                 frm.append(st6);
                 frm.addCommand(quit);
                 frm.addCommand(back1);
                frm.setCommandListener(this);
                display.setCurrent(frm);

        }catch(Exception e){System.out.println(e+"Exception In Menu city clicked");}
         finally{
             try{
                isc.close();
                hcc.close();
             }catch(Exception e){ System.out.println(e+"Exception in Finally in city clicked");}


        }
         System.out.println("information category"+tempm);

     } //end of informationCategory(String cname,String tempm)


    /**
	    * This is a method to retrieve names of a particular Category
	    * when a particular City and Category is Clicked
    */
    private void informationDetails(String cname,String tempp)
    {
                     StringBuffer sbd = new StringBuffer();
                     InputStream isd = null;
                     HttpConnection hcd = null;


        try{

        long lend = 0;
        int ch = 0;
        String name=" ";
        cname = cname.replace(' ','_');
        tempp=tempp.replace(' ','_');
        String param=cname+" & category="+tempp;
        param = param.replace(' ','_');
        String url ="http://localhost:8080/CityDetails.jsp?city="+param;
        hcd = (HttpConnection)Connector.open(url);
        isd = hcd.openInputStream();
        lend = hcd.getLength();
            if(lend !=-1){
            for(int i=0;i<lend;i++){
                if((ch = isd.read()) !=-1)
                {
                    sbd.append((char)ch);

                 }
            }
          }else{
            while(( ch = isd.read()) !=-1){
                lend = isd.available() ;
	        sbd.append((char)ch);

               }
            }

        String str1 = sbd.toString();

        str1.trim();

         cityname=new StringItem("Select",temp);
         menud = new List("Please Select an Option",List.IMPLICIT);
          int index = str1.indexOf("#");

            while(index != -1)
            {

                index = str1.indexOf("#");

              if(index != -1)
                {
                  String item = str1.substring(0,str1.indexOf("#"));
                  str1 = str1.substring(str1.indexOf("#")+1);
                  item=item.trim();
                   menud.append(item,null);
                menud.addCommand(inform);
                menud.addCommand(back1);
                menud.setCommandListener(this);
                display.setCurrent(menud);

               }

            }

        }catch(Exception e){System.out.println(e+"Exception In Menu city clicked");}
         finally{
             try{
                isd.close();
                hcd.close();
             }catch(Exception e){ System.out.println(e+"Exception in Finally in city clicked");}


        }
    } //informationDetails(String cname,String tempp)

// If the Application needs to be paused temporarily.
     public void pauseApp()
     {
        menu = null;
        menuc = null;
        menud = null;
    } // end of pauseApp()

    // Clean up when the application is destroyed..

    public void destroyApp(boolean unconditional)
    {
         menu = null;
         menuc = null;
         menud = null;
        notifyDestroyed();
    } //end of destroyApp(boolean unconditional)

}

⌨️ 快捷键说明

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