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

📄 simplephonebook.java

📁 本光盘是《J2ME无线移动游戏开发》一书的配套光盘
💻 JAVA
字号:
package ch12;

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

public class SimplePhoneBook
    extends MIDlet
    implements
    CommandListener, RecordFilter, RecordComparator {

  private Command exitCommand; // The exit command
  private Command nextCommand;
  private Command newCommand;
  private TextBox t1;
  private TextBox t;
  private Display display; // The display for this MIDlet
  private String _name;
  private String _number;

  private RecordStore recordStore = null;

  public static String nameFilter = "Srikanth";

  public SimplePhoneBook() {
    display = Display.getDisplay(this);
    nextCommand = new Command("Next", Command.SCREEN, 2);
    exitCommand = new Command("Exit", Command.SCREEN, 2);
    newCommand = new Command("NewNumber", Command.SCREEN, 2);

    // Create a new record store for this example
    try {
      recordStore = RecordStore.openRecordStore("SimplePhoneBook", true);
      System.out.println("RMS Name:"+recordStore.getName());
      System.out.println("RMS Version:"+recordStore.getVersion());
      System.out.println("RMS Record Num:"+recordStore.getNumRecords());
      System.out.println("RMS Size:"+recordStore.getSize());
      System.out.println("RMS Available Size:"+recordStore.getSizeAvailable());
      System.out.println("RMS LastModified Time:"+recordStore.getLastModified());


    }
    catch (RecordStoreException rse) {
      System.out.println("Record Store Exception in the ctor." + rse);
      rse.printStackTrace();
    }

  }

  public void startApp() {
    t = new TextBox("Name", "", 256, TextField.ANY);
    t.addCommand(nextCommand);
    t.setCommandListener(this);

    t1 = new TextBox("Number", "", 256, TextField.PHONENUMBER);
    t1.addCommand(newCommand);
    t1.addCommand(exitCommand);
    t1.setCommandListener(this);

    display.setCurrent(t);

  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
    try {
      if (recordStore != null) {
        recordStore.closeRecordStore();
      }
    }
    catch (Exception ignore) {
      // ignore this
    }

  }

  /**
   * Stores a new phone number to the storage.
   *
   * @param phoneNum the number to store.
   * @param name Name
   */
  public void storeData(String name, String phoneNum) {
    // Each name/number is stored in a separate record, formatted with
    // the name and the phone number

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream outputStream = new DataOutputStream(baos);
    try {
      // Push the name into a byte array.
      outputStream.writeUTF(name);

      // Then push the phoneNumber.
      outputStream.writeUTF(phoneNum);
    }
    catch (IOException ioe) {
      System.out.println(ioe);
      ioe.printStackTrace();
    }

    // Extract the byte array
    byte[] b = baos.toByteArray();
    try {
      // Add it to the record store
      recordStore.addRecord(b, 0, b.length);
    }
    catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    }
  }

  /*
   * Part of the RecordFilter interface.
   */
  public boolean matches(byte[] nameArray) throws IllegalArgumentException {

    ByteArrayInputStream bais = new ByteArrayInputStream(nameArray);
    DataInputStream inputStream = new DataInputStream(bais);
    String name = null;

    try {
      name = inputStream.readUTF();
      String score = inputStream.readUTF();
    }
    catch (EOFException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    }
    catch (IOException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    }

    return (this.nameFilter.equals(name));
  }

  /*
   * Part of the RecordComparator interface.
   */
  public int compare(byte[] rec1, byte[] rec2) {

    // Construct DataInputStreams for extracting the data from the records.
    ByteArrayInputStream bais1 = new ByteArrayInputStream(rec1);
    DataInputStream inputStream1 = new DataInputStream(bais1);
    ByteArrayInputStream bais2 = new ByteArrayInputStream(rec2);
    DataInputStream inputStream2 = new DataInputStream(bais2);
    int retVal = 0;
    try {
      // Extract the names
      String name1 = inputStream1.readUTF();
      String name2 = inputStream2.readUTF();
      if (name1.compareTo(name2) < 0) {
        retVal = RecordComparator.PRECEDES;
      }
      else if (name1.compareTo(name2) > 0) {
        retVal = RecordComparator.FOLLOWS;
      }
      else {
        retVal = RecordComparator.EQUIVALENT;
      }
    }
    catch (EOFException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    }
    catch (IOException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    }
    return retVal;
  }

  /**
   * This method prints all the name/numbers sorted by game score.
   */
  public void printSimplePhoneBook() {
    try {
      // Enumerate the records
      RecordEnumeration re = recordStore.enumerateRecords(null, this, true);
      printHelper(re);
    }
    catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    }

  }

  /**
   * This method prints the name/number for a given name
   * sorted by game score.
   */
  public void printSimplePhoneBook(String Name) {

    try {
      // Enumerate the records using the comparator and filter
      // implemented above to sort by game score.
      RecordEnumeration re = recordStore.enumerateRecords(this, this, true);
      printHelper(re);
    }
    catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    }
  }

  /**
   * A helper method for the printSimplePhoneBook methods.
   */
  private void printHelper(RecordEnumeration re) {

    try {
      while (re.hasNextElement()) {
        int id = re.nextRecordId();
        ByteArrayInputStream bais = new ByteArrayInputStream(recordStore.
            getRecord(id));
        DataInputStream inputStream = new DataInputStream(bais);
        try {
          String name = inputStream.readUTF();
          String number = inputStream.readUTF();
          System.out.println(name + " = " + number);
        }
        catch (EOFException eofe) {
          System.out.println(eofe);
          eofe.printStackTrace();
        }
      }
    }
    catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    }
    catch (IOException ioe) {
      System.out.println(ioe);
      ioe.printStackTrace();
    }
  }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      _name = t.getString();
      _number = t1.getString();
      System.out.println("Name = " + t.getString() + ", Number = " +
                         t1.getString());
      System.out.println("Storing data... ");
      storeData(_name, _number);

      System.out.println("Printing name/phone numbers for all... ");
      printSimplePhoneBook();

      System.out.println("Print phone number for Srikanth ");

      printSimplePhoneBook("Srikanth");
      destroyApp(false);
      notifyDestroyed();
    }
    if (c == nextCommand) {
      t1.setString(" ");
      display.setCurrent(t1);
    }
    if (c == newCommand) {
      display.setCurrent(t);
      _name = t.getString();
      _number = t1.getString();
      System.out.println("Name = " + t.getString() + ", Number = " +
                         t1.getString());
      System.out.println("Storing data... ");
      storeData(_name, _number);
      t.setString("");
      t1.setString("");
    }
  }
}

⌨️ 快捷键说明

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