练习1BusStation

来源:互联网 发布:公司业务员定位软件 编辑:程序博客网 时间:2024/06/05 19:44

 import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
//import javax.microedition.io.*;
import java.io.*;
import java.lang.String;

public class BusStation extends MIDlet implements CommandListener {

 private Display display;

 private Form InputForm, LineInputForm;

 private List MainList, ResaultList, DetailList;

 private TextField Start, End, Number;

 private String strStart, strEnd, strNumber;

 private String[] LD;

 private boolean bL;

 private boolean bNormal;

 private Command OkCommand, BackCommand, ExitCommand, DetailCommand;

 // private String[] strArray;

 public BusStation() {

  LD = new String[20];
  bNormal = true;

  OkCommand = new Command("查找", Command.OK, 0);
  BackCommand = new Command("返回", Command.BACK, 0);
  ExitCommand = new Command("退出", Command.EXIT, 0);
  DetailCommand = new Command("详情", Command.OK, 0);

  Start = new TextField("出发站", "", 14, TextField.ANY);
  End = new TextField("目的站", "", 14, TextField.ANY);
  Number = new TextField("", "", 14, TextField.NUMERIC);

  InputForm = new Form("输入站名");
  InputForm.append(Start);
  InputForm.append(End);

  InputForm.addCommand(BackCommand);
  InputForm.addCommand(OkCommand);
  InputForm.setCommandListener(this);

  LineInputForm = new Form("输入公交线路号");
  LineInputForm.append(Number);
  LineInputForm.addCommand(BackCommand);
  LineInputForm.addCommand(OkCommand);
  LineInputForm.setCommandListener(this);

  String[] strArray = { "站站查询", "线路查询" };
  MainList = new List("选择查询方式", Choice.IMPLICIT, strArray, null);

  MainList.addCommand(ExitCommand);
  MainList.addCommand(OkCommand);
  MainList.setCommandListener(this);

  // String[] s={""};
  ResaultList = new List("查询结果:(0表示上行,1表示下行)", Choice.IMPLICIT);// ,s,null);
  ResaultList.addCommand(DetailCommand);
  ResaultList.addCommand(BackCommand);
  ResaultList.setCommandListener(this);

  DetailList = new List("线路详情:", Choice.IMPLICIT);// ,null,null);
  DetailList.addCommand(BackCommand);
  DetailList.setCommandListener(this);

  // ResaultList.getString(1);
  // System.out.print("haha");

  // TODO Auto-generated constructor stub

 }

 public void commandAction(Command c, Displayable d) {

  if (c == ExitCommand) {
   try {
    destroyApp(false);
    notifyDestroyed();
   } catch (MIDletStateChangeException e) {
    e.printStackTrace();
   }

  }
  if (d.equals(MainList)) {
   if (c == OkCommand) {
    switch (((List) d).getSelectedIndex()) {
    case 0:
     display.setCurrent(InputForm);
     break;
    case 1:
     display.setCurrent(LineInputForm);
     break;

    }

   }
  }
  if (d.equals(LineInputForm)) {
   if (c == OkCommand) {
    bL = true;
    strNumber = Number.getString();
    for (int i = 0; i < 20; i++)
     LD[i] = null;
    showResault(strNumber, LD);
    display.setCurrent(ResaultList);
   }
   if (c == BackCommand) {
    display.setCurrent(MainList);
   }

  }

  if (d.equals(InputForm)) {
   if (c == OkCommand) {

    bL = false;

    strStart = Start.getString();
    strEnd = End.getString();
    for (int i = 0; i < 20; i++)
     LD[i] = null;
    // ///////////////////////////////////deleteAll()//////////////////////////////////////////////
    boolean bflag = true;
    while (bflag) {
     try {
      ResaultList.getString(0);
     } catch (IndexOutOfBoundsException e) {
      bflag = false;
     }
     if (bflag == true) {
      // System.out.println(ResaultList.getString(0));
      ResaultList.delete(0);
     }
    }
    // ///////////////////////////////////////////////////////////////////////////////////////

    for (int j = 0; j <= 10; j++)
     showLines("/" + String.valueOf(j) + ".txt", LD);
    adjust(LD);
    display.setCurrent(ResaultList);

   }

   if (c == BackCommand) {
    display.setCurrent(MainList);
   }
  }

  if (d.equals(ResaultList)) {
   // System.out.println("ResaultList");
   if (c == DetailCommand) {
    if (!bNormal)
     return;
    int i = ResaultList.getSelectedIndex();

    String s = ResaultList.getString(i);

    showDetail(s, LD);
    display.setCurrent(DetailList);

   }
   if (c == BackCommand) {
    bNormal = true;
    if (!bL)
     display.setCurrent(InputForm);
    else
     display.setCurrent(LineInputForm);
   }

  }
  if (d.equals(DetailList)) {
   if (c == BackCommand) {

    display.setCurrent(ResaultList);
   }
  }
 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub

 }

 protected void pauseApp() {
  // TODO Auto-generated method stub

 }

 protected void startApp() throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  display = Display.getDisplay(this);
  display.setCurrent(MainList);

 }

 public void showLines(String name, String[] LineDetail) {

  int index = 0;
  int s = 0;
  int e = 0;

  String strLine = "";
  InputStream in = null;
  int count = 0;
  int testEnt;
  long skip = 0;

  try {
   in = getClass().getResourceAsStream(name);

   while (true) {

    while (true) {

     testEnt = in.read();
     count++;
     if (testEnt == 10)
      break;

     if (testEnt == -1) {
      break;
     }

    }

    skip = skip + count;

    in.reset();
    in.skip(skip - count);
    byte[] word_utf = new byte[count];
    in.read(word_utf);
    strLine = new String(word_utf);
    String strTemp = strLine.substring(7);
    s = strTemp.indexOf(strStart);
    e = strTemp.indexOf(strEnd);

    if (s != -1 && e != -1 && s < e) {
     // System.out.println(strLine);
     if (index >= 20)
      break;
     while (LineDetail[index] != null) {
      index++;
     }
     if (LineDetail[index] == null) {
      LineDetail[index] = strLine;
      strLine = strLine.substring(0, 7);
      ResaultList.append(strLine, null);
      System.out.println(strLine);
     }

    }
    /*
     * else{ String Warnning="没有从"+strStart+"直达"+strEnd+"的公交车";
     * ResaultList.append(Warnning, null); }
     */

    if (testEnt == -1)
     break;

    count = 0;

   }

   in.close();

  } catch (IOException ioe) {
   ioe.printStackTrace();

  } finally {
   in = null;
  }
  // System.out.print(LineDetail[0]);

 }

 public void showDetail(String strLineNumber, String[] LineDetail) {

  // ////////////////////////////////////////////////////////////////////////////////////////////
  boolean bflag = true;
  while (bflag) {
   try {
    DetailList.getString(0);
   } catch (IndexOutOfBoundsException e) {
    bflag = false;
   }
   if (bflag == true) {
    // System.out.println(ResaultList.getString(0));
    DetailList.delete(0);
   }
  }
  // ////////////////////////////////////////////////////////////////////////////////////////////
  String temp = null;
  strLineNumber = strLineNumber.substring(0, 7);

  for (int i = 0; LineDetail[i] != null && i <= 20; i++) {
   temp = LineDetail[i].substring(0, 7);

   if (strLineNumber.equals(temp)) {
    // System.out.println("true");
    String strLine = LineDetail[i].substring(7);
    int indexof = 0;
    String sub;
    int end = 0;
    while (true) {

     indexof = strLine.indexOf(" ");
     if (indexof == -1) {
      break;
     }

     end = indexof;

     sub = strLine.substring(0, end);
     strLine = strLine.substring(end + 1);

     DetailList.append(sub, null);
    }// while

    break;
   }// if
  }// for

 }

 public void showResault(String strL, String[] L) {
  String name = null;
  int n = Integer.parseInt(strL);
  if (n <= 0)
   return;
  else {
   n = n / 10;
   if (n > 10)
    name = "/10.txt";
   else
    name = "/" + String.valueOf(n) + ".txt";
  }

  String str = null;

  int m = strL.length();
  if (m == 1) {
   str = "00" + strL + "路 ";
  }
  if (m == 2) {
   str = "0" + strL + "路 ";

  }
  if (m == 3) {
   str = strL + "路 ";
  }
  if (m > 3) {
   adjust(L);
   return;
  }

  showLines(str, name, L);
  adjust(L);

 }

 public void showLines(String sl, String sn, String[] sls) {

  // ///////////////////////////////////deleteAll()//////////////////////////////////////////////
  boolean bflag = true;
  while (bflag) {
   try {
    ResaultList.getString(0);
   } catch (IndexOutOfBoundsException e) {
    bflag = false;
   }
   if (bflag == true) {
    // System.out.println(ResaultList.getString(0));
    ResaultList.delete(0);
   }
  }
  // ////////////////////////////////////////////////////////////////////////////////////////

  int i = 0;
  String strLine = "";
  InputStream in = null;
  int count = 0;
  int testEnt = 0;
  long skip = 0;

  try {
   in = getClass().getResourceAsStream(sn);

   while (true) {

    while (true) {

     if (testEnt == -1) {
      break;
     }

     testEnt = in.read();
     count++;
     if (testEnt == 10)
      break;

    }

    skip = skip + count;

    in.reset();
    in.skip(skip - count);
    byte[] word_utf = new byte[count];
    in.read(word_utf);
    strLine = new String(word_utf);

    if (sl.equals(strLine.substring(0, 5))) {
     while (sls[i] != null) {
      i++;
     }
     if (i >= 20)
      break;

     sls[i] = strLine;

     ResaultList.append(strLine.substring(0, 7), null);
     strLine = strLine.substring(7, 8);
     if (strLine.equals(" ") || strLine.equals("1")) {
      testEnt = -1;
      break;
     }

    }

    if (testEnt == -1)
     break;

    count = 0;
   }

   in.close();

  } catch (IOException ioe) {
   ioe.printStackTrace();

  } finally {
   in = null;
  }

 }

 public void adjust(String[] LineDetail) {
  String Warnning = null;
  if (LineDetail[0] == null) {
   bNormal = false;
   // ///////////////////////////////////deleteAll()//////////////////////////////////////////////
   boolean bflag = true;
   while (bflag) {
    try {
     ResaultList.getString(0);
    } catch (IndexOutOfBoundsException e) {
     bflag = false;
    }
    if (bflag == true) {
     // System.out.println(ResaultList.getString(0));
     ResaultList.delete(0);
    }
   }
   // ////////////////////////////////////////////////////////////////////////////////////////
   if (!bL) {
    Warnning = "没有从 " + strStart + " 直达 " + strEnd + " 的公交车";
    ResaultList.append(Warnning, null);
   } else {
    Warnning = "没有线路号为 " + strNumber + " 的公交车";
    ResaultList.append(Warnning, null);
   }
  }
 }

}

原创粉丝点击