public static void main(String[] args) {
    // create three President objects
    President president44 = new President("Barack", "Hussein", "Obama");
    President president43 = new President("George", "Walker", "Bush");
    President president42 = new President("William", "Jefferson", "Clinton");

    // output three President objects
    String programOutput =
        "The three most recent presidents are:\n"
            + president44.toString()
            + "\n"
            + president43.toString()
            + "\n"
            + president42.toString();
    JOptionPane.showMessageDialog(null, programOutput);
  }
示例#2
0
文件: Main.java 项目: hexiaochun/GoF
 public static void main(String[] args) {
   President president = new President();
   president.callBoss();
   president.execute("开发SX系统");
 }
  public void parse(String fileName) throws IOException, ParseException {
    BufferedReader reader =
        new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(fileName)));
    boolean seenGroverCleveland = false;
    President cleveland1 = null;
    String line = reader.readLine().trim();
    while (line != null) {
      if (!line.startsWith("#") && line.length() > 0) {
        String[] fields = line.split(",");
        President p = new President();
        p.name = fields[1];
        p.born = parseDate(p.sBorn = fields[2]);
        p.died = parseDate(p.sDied = fields[3]);
        p.ascension = parseDate(p.sAscension = fields[4]);
        p.endOfTerm = parseDate(p.sEndOfTerm = fields[5]);
        p.alive = fields[3].length() == 0;
        if (p.name.equals("Grover Cleveland")) { // the president with two non-consecutive terms
          if (seenGroverCleveland) {
            p.groverCleveland2 = true;
            p.otherCleveland = cleveland1;
            cleveland1.otherCleveland = p;
          } else {
            p.groverCleveland1 = true;
            cleveland1 = p;
            seenGroverCleveland = true;
          }
        }
        presidents.add(p);
      }
      line = reader.readLine();
      if (line != null) line = line.trim();
    }
    presidents.get(presidents.size() - 1).inOffice = true;

    mccain = new President();
    mccain.name = "John McCain";
    mccain.born = parseDate(mccain.sBorn = "8/29/1936");
    mccain.died = parseDate("");
    mccain.ascension = parseDate("1/20/2009");
    mccain.alive = true;
    mccain.candidate = true;
    presidents.add(mccain);

    obama = new President();
    obama.name = "Barack Obama";
    obama.born = parseDate(obama.sBorn = "8/4/1961");
    obama.died = parseDate("");
    obama.ascension = parseDate("1/20/2009");
    obama.alive = true;
    obama.candidate = true;
    presidents.add(obama);
  }