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

📄 veterinarytags.java

📁 Java经典例程 从外国一大学计算机教授出版物下载的代码 经典
💻 JAVA
字号:
import java.io.*;
import javagently.*;

class VeterinaryTags {

    /* The Vet tagging program   by J M Bishop Jan 1997
     * -----------------------   Java 1.1 Oct 1997
     *                           Display class July 1999
     * Keeps a register of pets' tags and enables it to be
     * checked if a stray pet is found.
     * Uses two types of tags.
     * Illustrates inheritance.
     */

  public static void main (String args []) throws IOException {
     TagDataHandler vetAssoc = new TagDataHandler ();
     vetAssoc.initialize();
     vetAssoc.makeTags();
     vetAssoc.showTags();
     while (true)
       // ends when close button is pressed on display
       vetAssoc.checkTags();
  }

  static class TagDataHandler {

    int index;
    Tags[] register = new Tags[100];
    Display display = new Display ("Veterinary tag system");
    BufferedReader fin = Text.open(System.in);

    void initialize () {
      // fin defaults to the keyboard in case there is no file

      display.println("Savanna Pet Tag System");
      display.prompt ("Pet file name", "tags.data");
      while (true) {
        display.ready("Set file name and press ready");
        String filename = display.getString("Pet file name");
        try {
          fin = Text.open(filename);
          break;
        } catch (FileNotFoundException e) {
          display.println("No such file, try again");
        }
      }
    }

    void makeTags( ) throws IOException {
      String petsName, ownersPhone;
      while (true) {
        try {
          char kind = Text.readChar(fin);
          petsName = Text.readString(fin);
          ownersPhone = Text.readString(fin);
          Tags tag;
          switch (kind) {
            case 'p':
            case 'P':
              tag = new Tags(petsName, ownersPhone);
              break;
            default:
            case 'x':
            case 'X':
              String vetsPhone = Text.readString(fin);
              tag = new XTags(petsName, ownersPhone, vetsPhone);
          }
          register[index] = tag;
          index++;
        }
        catch (EOFException e) {
          break;
        }
      }
      display.println("All "+index+" pets read in.\n");
    }

    void showTags() {
      display.println("The Pet Register\n");
      display.println("Pet No.  Name and phone no.");
      for (int i = 0; i < index; i++)
        display.println(i+"   "+register[i]);
      display.prompt("Found pet's tag name","Buster");
    }

    void checkTags() {
      display.ready("\nEnter pet's name as on tag and press ready");
      String info = display.getString("Found pet's tag name");
      boolean found = false;
      for (int i = 0; i < index; i++)
        if (register[i].name.equals(info)) {
          display.println("The pet called "+
            info+" is registered no. "+i);
          display.println("Full info: "+register[i]);
          found = true;
          break;
        }
        if (!found)
           display.println("The pet called "+
             info+" is not registered.");
     }
  }
}

⌨️ 快捷键说明

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