练习1BusStation

 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);
   }
  }
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public class BusManagementSystem { private List<BusLine> busLines; // 线路列表 private List<BusStation> busStations; // 站点列表 private List<Bus> buses; // 车辆列表 // 构造函数 public BusManagementSystem() { this.busLines = new ArrayList<>(); this.busStations = new ArrayList<>(); this.buses = new ArrayList<>(); } // 添加线路 public void addBusLine(BusLine busLine) { busLines.add(busLine); } // 删除线路 public void removeBusLine(BusLine busLine) { busLines.remove(busLine); } // 添加站点 public void addBusStation(BusStation busStation) { busStations.add(busStation); } // 删除站点 public void removeBusStation(BusStation busStation) { busStations.remove(busStation); } // 添加车辆 public void addBus(Bus bus) { buses.add(bus); } // 删除车辆 public void removeBus(Bus bus) { buses.remove(bus); } // 查询线路 public BusLine getBusLine(String lineName) { for (BusLine busLine : busLines) { if (busLine.getLineName().equals(lineName)) { return busLine; } } return null; } // 查询站点 public BusStation getBusStation(String stationName) { for (BusStation busStation : busStations) { if (busStation.getName().equals(stationName)) { return busStation; } } return null; } // 查询车辆 public Bus getBus(String plateNumber) { for (Bus bus : buses) { if (bus.getPlateNumber().equals(plateNumber)) { return bus; } } return null; } }错误: 在类 项目训练.BusManagementSystem 中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args),解决这个错误,并生成正确代码
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值