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

📄 movieslist.java

📁 Mobile Services
💻 JAVA
字号:
import javax.microedition.midlet.MIDlet;
 
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.ChoiceGroup;
 
import java.util.Vector;
import java.util.Enumeration;
/* This MIDlet is called when the user selects Movies from the MainClass List */
public class  MoviesList extends MIDlet implements CommandListener
{
	static int counter = 0;
  	/* Declaration of different objects */
  	String hallname,realhall,showdate,bookname,bookemail,booktime,bookcategory
  			,strshow,strbal,strrear,strupper,strmiddle;
 	DataParser parsemoviedata;
  	Vector name,id,details;
  	int commandvalue = 0;
  	private Display displaymovielist;
  	private List halllist,listmovies,listdate;
  	private Form statusform,yourname,emailform,categoryform,/*timeform,*/ticketform;
  	private StringItem sitem;
  	private Command backtodate,backtomovie,backtohall,bookticket,backtostatus,
  			emailscreen,backtoname,categoryscreen,backtoemail,backtocat,ticketscreen,
  			finalbook,exitcommand;
  	private TextField entername,enteremail,enterticket;
  	private ChoiceGroup categorybuttons;
  	private MainClass lastscreen;
  	/* Constructor of the Movielist class declared */
 	MoviesList(Display disp,MainClass obj)	
  	{
 		lastscreen = obj;
 		displaymovielist = disp;
  		exitcommand = new Command("Exit",Command.EXIT,1);
  		halllist = new List("Select Hall",List.IMPLICIT);
  		halllist.setCommandListener(this);
  		halllist.addCommand(exitcommand);
  
 	}
  	/* This is start of an application. This method is called when the application is called */
  	public void startApp()
  	{
 		parsemoviedata = new DataParser();
 		/* This will make an Http connection and open an InputStream to read from the connection */
 		parsemoviedata.sourceurl("http://192.192.168.100/midlet/template/midlet_movies.xml");
 		/* This will parse the XML data */
  		parsemoviedata.parseDataMovies(1);
  		/* values taken in a vector */
  		name = parsemoviedata.hall_name();
 		id = parsemoviedata.hall_id();
 		/* This will create a list with the hall name to be displayed to the user */
  		for(Enumeration enum = name.elements() ; enum.hasMoreElements() ;) 
  		{
 			 halllist.append((String)enum.nextElement(),null);
  		}
   		/* List displayed */
 		displaymovielist.setCurrent(halllist);
  		/* Command objects declared */
  		backtodate = new Command("Back",Command.BACK,1);
  		backtomovie = new Command("Back",Command.BACK,1);
  		backtohall = new Command("Back",Command.BACK,1);
  		bookticket = new Command("Book",Command.SCREEN,1);
  		backtostatus = new Command("Back",Command.BACK,1);
  		emailscreen = new Command("OK",Command.SCREEN,1);
  		backtoname = new Command("Back",Command.SCREEN,1);
  		categoryscreen = new Command("OK",Command.SCREEN,1);
  		backtoemail = new Command("Back",Command.SCREEN,1);
  		backtocat = new Command("Back",Command.SCREEN,1);
  		ticketscreen = new Command("OK",Command.SCREEN,1);
  		finalbook = new Command("BOOK",Command.SCREEN,1);
  		
  	}	
  	/* destroyApp() method declared. This will free the resources */
  	public void destroyApp(boolean b)
  	{
  		backtodate = null;
  		backtomovie = null;
  		backtohall = null;
  		bookticket = null;
  		backtostatus = null;
  		emailscreen = null;
  		backtoname = null;
  		categoryscreen = null;
 		backtoemail = null;
  		backtocat = null;
  		ticketscreen = null;
  		finalbook = null;
  		exitcommand = null;
  	}
 
  	public void pauseApp()
  	{}
 	/* This method is used for event handling */
  	public void commandAction(Command c, Displayable d)
  	{
  		
		/* this is true if an item is selected in the list */
  		if(c == List.SELECT_COMMAND)
  		{
  			if(halllist.isShown())
  			{
  				realhall = halllist.getString(halllist.getSelectedIndex());
  				hallname = halllist.getString(halllist.getSelectedIndex());
  			}
  			else if(listmovies.isShown())
  				hallname = listmovies.getString(listmovies.getSelectedIndex());
  			else if(listdate.isShown())
 				hallname = listdate.getString(listdate.getSelectedIndex());
  			counter++;
  			getAllData(hallname,counter);
  		}		
  		/* This will display the date list. On click of Back button */
  		if(c == backtodate)
  		{
  			displaymovielist.setCurrent(listdate);
  			counter = 2;
  		}
  		/* This will display the movie list. On click of Back button */
  		if(c == backtomovie)
  		{
  			displaymovielist.setCurrent(listmovies);
  			counter = 1;
  		}
  		/* This will display the hall list. On click of Back button */
  		if(c==backtohall)
  		{
  			displaymovielist.setCurrent(halllist);
  			counter = 0;
  		}
  		/* This will display a form asking the user to enter his name. On click of Book button */
  		if(c==bookticket)
  		{
  			yourname = new Form("Enter your Name");
  			yourname.setCommandListener(this);
  			yourname.addCommand(emailscreen);
  			yourname.addCommand(backtostatus);
  			entername = new TextField("Enter Name","",20,TextField.ANY);
  			yourname.append(entername);
  			displaymovielist.setCurrent(yourname);
  		}
  		/* This will display the status form. On click of Back button */
  		if(c == backtostatus)
  		{
  			displaymovielist.setCurrent(statusform);
  		}
  		/* This will display the form asking the user to enter his name. On click of Back button */
  		if(c == backtoname)
  		{
  			displaymovielist.setCurrent(yourname);
  		}
  		/* This will display the form asking the user to enter his email. On click of Back button */
  		if(c == backtoemail)
  		{
  			displaymovielist.setCurrent(emailform);
  		}
  		/* This will display the form for selecting a category like "balcony" and "rear stall" etc
  		. On click of Back button */
  		if(c == backtocat)
 		{
  			displaymovielist.setCurrent(categoryform);
 		}
  		/* This will ask the user to enter his email address.On click of OK button */
  		if(c == emailscreen)
  		{
  			bookname = entername.getString();
  			emailform = new Form("Enter your Email");
  			emailform.setCommandListener(this);
  			enteremail = new TextField("Enter Email","",30,TextField.EMAILADDR);
  			emailform.append(enteremail);
 			emailform.addCommand(backtoname);
  			emailform.addCommand(categoryscreen);
  			displaymovielist.setCurrent(emailform);
  		}
  		/* This will ask the user to select a category.On click of Ok button */
  		if(c == categoryscreen)
  		{
  			bookemail = enteremail.getString();
  			categoryform = new Form("Select a Category");
  			categoryform.setCommandListener(this);
  			categorybuttons = new ChoiceGroup("Category",ChoiceGroup.EXCLUSIVE);
  			categorybuttons.append("Balcony",null);
  			categorybuttons.append("Rear Stall",null);
  			categorybuttons.append("Upper Stall",null);
  			categorybuttons.append("Middle Stall",null);
  			categoryform.append(categorybuttons);
  			categoryform.addCommand(backtoemail);
  			categoryform.addCommand(ticketscreen);
  			displaymovielist.setCurrent(categoryform);
  		}
  		/* This will ask the user to enter the number of tickets to be booked.On click of OK button */ 
  		if(c == ticketscreen)
  		{
  			bookcategory = categorybuttons.getString(categorybuttons.getSelectedIndex());
  			ticketform = new Form("Enter the Tickets");
 			ticketform.setCommandListener(this);
  			enterticket = new TextField("Enter Ticket","",6,TextField.NUMERIC);
  			ticketform.append(enterticket);
  			ticketform.addCommand(backtocat);
  			ticketform.addCommand(finalbook);
  			displaymovielist.setCurrent(ticketform);
  		}
  		/* This will book the tickets for the user. On click of a Book Button */
  		if(c == finalbook)
  		{
  			String url = "fname="+bookname +"&email="+ bookemail +"&hallid="+Integer.parseInt((String)id.elementAt(name.indexOf(realhall)))+ "&" +bookcategory+"="+ Integer.parseInt(enterticket.getString())+ "&showtime=" + booktime +"&showdate="+showdate;
  			System.out.println("Print this url " + url);
			url = url.trim();
			/*try
    			{
  				/* This will create an HTPP connection object */
  				//HttpConnection con = (HttpConnection)Connector.open(url);
  				/* This will open an InputStream to the connection */
  				//InputStream ins = con.openInputStream();
  				//ins.close();
  			/*}
  			catch(IOException ex)
    			{
    				System.out.println("IOException occured");
   			}*/
  			
			int i = 0;
			int j=0;
			StringBuffer strbuf = new StringBuffer();
			while(i < url.length())
			{
				i = url.indexOf(" ",i+1);
				if(i == -1)
					break;
				strbuf = strbuf.append(url.substring(j,i));
				strbuf = strbuf.append("%20");
				j = i+1;
			}
			strbuf = strbuf.append(url.substring(j,url.length()));
			System.out.println("This is the value of " + strbuf);
			destroyApp(true);
  			notifyDestroyed();
  			lastscreen.getDisplayObject(displaymovielist);
  		}
  		/* This will delete the MIDlet and close the application */
  		if(c == exitcommand)
  		{
  			destroyApp(true);
  			notifyDestroyed();
  			lastscreen.getDisplayObject(displaymovielist);
  		}
  	}		
  	/* This method is very important method. This method is used to manupulate more than one List */
  	private void getAllData(String str,int counter)
  	{
  		/* this if condition is true if an item is selected from the hall list */
 		if(counter == 1)
 		{	
  			listmovies = new List("List of Movies",List.IMPLICIT);
 			listmovies.setCommandListener(this);
		listmovies.addCommand(backtohall);
 			parsemoviedata.sourceurl("http://192.192.168.100/midlet/template/midlet_status.xml?tname=" + str);
 			parsemoviedata.parseDataMovies(2);
  			details = parsemoviedata.halldata();
  			details.trimToSize();
  			for (Enumeration enum = details.elements();enum.hasMoreElements();) 
  			{
  				if(enum.nextElement().equals("ms"))
  				{
  					listmovies.append((String)enum.nextElement(),null);
  				}
 			}
 			displaymovielist.setCurrent(listmovies);
  		}
  		/* this if condition is true if an item is selected from the movies list */
  		if(counter == 2)
  		{
  			listdate = new List("Show Date",List.IMPLICIT);
  			listdate.setCommandListener(this);
  			listdate.addCommand(backtomovie);
  			int indexofmovie = details.indexOf(str);
  			int indexofms = details.indexOf("ms",indexofmovie+2);
  			if(indexofms == -1)
  				indexofms = details.size();
  			while(indexofmovie < indexofms)
  			{
  				if((details.elementAt(indexofmovie)).equals("st"))
  				{
  					listdate.append((String)details.elementAt(indexofmovie+1),null);
 				}
  				indexofmovie++;
  			}
  			displaymovielist.setCurrent(listdate);
  			
  		}
  		/* this if condition is true if user selects date from the date list */
  		if(counter == 3)
  		{
  			showdate = str;
  			statusform = new Form("Status of Movie");
  			statusform.setCommandListener(this);
  			statusform.addCommand(backtodate);
  			statusform.addCommand(bookticket);
  			int indexdate = details.lastIndexOf(str);
  			int indexms = details.indexOf("ms",indexdate);
  			int indexst = details.indexOf("st",indexdate);
  			indexdate+=1;
  			if(indexst == -1)
  				indexst = details.size();
  			if(indexms == -1)
  				indexms = details.size();
  			String sarray[] = {"Show Time","Balcony","Rear Stall","Upper Stall","Middle Stall"};
  			int i = 0;
  			while((indexdate) < indexms)
  			{
  				if(i > 4)
  					break;
  				if(i==0)
  				{	
  					booktime = 	(String)details.elementAt(indexdate);
  				}					
  				sitem = new StringItem(sarray[i],(String)details.elementAt(indexdate));
  				statusform.append(sitem);
  				indexdate++;
  				i++;
  			}
  			displaymovielist.setCurrent(statusform);
  		}
  	}
}

⌨️ 快捷键说明

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