/**
  * Creates a new NameSurferDataBase and initializes it using the data in the specified file. The
  * constructor throws an error exception if the requested file does not exist or if an error
  * occurs as the file is being read.
  */
 public NameSurferDataBase(String filename) {
   File file = new File(filename);
   try {
     Scanner scanner = new Scanner(new FileReader(file));
     while (scanner.hasNextLine()) {
       NameSurferEntry entry = new NameSurferEntry(scanner.nextLine());
       String name = entry.getName().toLowerCase();
       if (!name.equals("error")) data.put(name, entry);
     }
     scanner.close();
   } catch (IOException ex) {
     // don't care do nothing
   }
 }
  /**
   * Creates a new NameSurferDataBase and initializes it using the data in the specified file. The
   * constructor throws an error exception if the requested file does not exist or if an error
   * occurs as the file is being read.
   */
  public NameSurferDataBase(String filename) {
    try {
      BufferedReader br = new BufferedReader(new FileReader(filename));
      while (true) {
        String line = br.readLine();
        if (line == null) break;
        NameSurferEntry entry = new NameSurferEntry(line);
        dataBase.put(entry.getName(), entry);
      }
      br.close();

    } catch (IOException e) {
      System.out.println("file not found: " + filename);
    }
  }
  /**
   * Creates a new NameSurferDataBase and initializes it using the data in the specified file. The
   * constructor throws an error exception if the requested file does not exist or if an error
   * occurs as the file is being read.
   */
  public NameSurferDataBase(String filename) {

    try {
      BufferedReader rd = new BufferedReader(new FileReader(filename));
      while (true) {
        String line = rd.readLine();
        if (line == null) break;
        NameSurferEntry entry = new NameSurferEntry(line);
        namesDataBase.put(entry.getName(), entry);
      }
      rd.close();

    } catch (IOException ex) {
      throw new ErrorException(ex);
    }
  }
Exemplo n.º 4
0
 /**
  * Creates a new NameSurferDataBase and initializes it using the data in the specified file. The
  * constructor throws an error exception if the requested file does not exist or if an error
  * occurs as the file is being read.
  */
 public NameSurferDataBase(String filename) { // a class with a particular input parameter
   BufferedReader rd = null;
   try {
     rd = new BufferedReader(new FileReader(filename));
     while (true) {
       String line = rd.readLine();
       if (line == null) break;
       /**
        * 这里,NameSurferEntry必须读入String类型的数据,所以要确保NameSurferEntry(line) 中的line有数据,所以if (line ==
        * null) break; 必须放在前面,否则当读取到最后一行 的时候,line == null了,NameSurferEntry就会报错
        */
       NameSurferEntry obj = new NameSurferEntry(line);
       data.put(obj.getName(), obj); // corresponding name and NameSurferEntry
     }
     rd.close();
   } catch (IOException ex) {
     System.out.println("no file!");
   }
 }
 /* Draw graph line with the name and rating */
 private void drawEntry(NameSurferEntry entry, int entryNumber) {
   /* Draws graph line */
   for (int i = 0; i < NDECADES - 1; i++) {
     int position1 = entry.getRank(i);
     int position2 = entry.getRank(i + 1);
     double x1 = i * (getWidth() / NDECADES);
     double x2 = (i + 1) * (getWidth() / NDECADES);
     double y1 = 0;
     double y2 = 0;
     if (position1 != 0 && position2 != 0) {
       y1 = GRAPH_MARGIN_SIZE + (getHeight() - GRAPH_MARGIN_SIZE * 2) * position1 / MAX_RANK;
       y2 = GRAPH_MARGIN_SIZE + (getHeight() - GRAPH_MARGIN_SIZE * 2) * position2 / MAX_RANK;
     } else if (position1 == 0 && position2 == 0) {
       y1 = getHeight() - GRAPH_MARGIN_SIZE;
       y2 = y1;
     } else if (position1 == 0) {
       y1 = getHeight() - GRAPH_MARGIN_SIZE;
       y2 = GRAPH_MARGIN_SIZE + (getHeight() - GRAPH_MARGIN_SIZE * 2) * position2 / MAX_RANK;
     } else {
       y1 = GRAPH_MARGIN_SIZE + (getHeight() - GRAPH_MARGIN_SIZE * 2) * position1 / MAX_RANK;
       y2 = getHeight() - GRAPH_MARGIN_SIZE;
     }
     GLine line = new GLine(x1, y1, x2, y2);
     /* Set line color */
     if (entryNumber % 4 == 0) {
       line.setColor(Color.BLUE);
     } else if (entryNumber % 4 == 1) {
       line.setColor(Color.RED);
     } else if (entryNumber % 4 == 2) {
       line.setColor(Color.MAGENTA);
     } else if (entryNumber % 4 == 3) {
       line.setColor(Color.BLACK);
     }
     add(line);
   }
   /* Add label with the name and ranking */
   for (int i = 0; i < NDECADES; i++) {
     String name = entry.getName();
     int position = entry.getRank(i);
     String positionString = Integer.toString(position);
     String label = name + " " + positionString;
     double x = i * (getWidth() / NDECADES) + 10;
     double x1 = x - NDECADES;
     double y;
     int R = 5;
     if (position != 0) {
       y = GRAPH_MARGIN_SIZE + (getHeight() - GRAPH_MARGIN_SIZE * 2) * position / MAX_RANK;
     } else {
       /* Add "*" if name was not used and remove marker point */
       label = name + " *";
       y = getHeight() - GRAPH_MARGIN_SIZE - 10;
       R = 0;
     }
     /* Add marker point */
     GOval marker = new GOval(x1, y - 2, R, R); /* Got "y-2" by scientific research =) */
     marker.setFilled(true);
     GLabel nameLabel = new GLabel(label, x, y);
     nameLabel.setFont(new Font("Times Roman", Font.BOLD, 12));
     /* Set label color */
     if (entryNumber % 4 == 0) {
       nameLabel.setColor(Color.BLUE);
       marker.setColor(Color.BLUE);
     } else if (entryNumber % 4 == 1) {
       nameLabel.setColor(Color.RED);
       marker.setColor(Color.RED);
     } else if (entryNumber % 4 == 2) {
       nameLabel.setColor(Color.MAGENTA);
       marker.setColor(Color.MAGENTA);
     } else if (entryNumber % 4 == 3) {
       nameLabel.setColor(Color.BLACK);
       marker.setColor(Color.BLACK);
     }
     add(nameLabel);
     add(marker);
   }
 }