【笔记】Eclipse and Java for Total Beginners—016

Lesson 16 – Create main Method and JAR File

  • Introduce main method
  • Write a main method
  • Run MyLibrary as Java application
  • Export to JAR file and run from Windows

在MyLibrary.java里添加main method。

1 public static void main(String[] args) {
2 // create a new MyLibrary
3   MyLibrary testLibrary = new MyLibrary("Test Drive Library");
4 Book b1 = new Book("War and Peace");
5 Book b2 = new Book("Great Expectations");
6 b1.setAuthor("Tolstoy");
7 b2.setAuthor("Dickens");
8 Person jim = new Person();
9 Person sue = new Person();
10 jim.setName("Jim");
11 sue.setName("Sue");
12
13 testLibrary.addBook(b1);
14 testLibrary.addBook(b2);
15 testLibrary.addPerson(jim);
16 testLibrary.addPerson(sue);
17
18 System.out.println("Just creat new library");
19 testLibrary.printStatus();
20
21 System.out.println("Check out War and Peace to Sue");
22 testLibrary.checkOut(b1, sue);
23 testLibrary.printStatus();
24
25 System.out.println("Do some more stuff");
26 testLibrary.checkIn(b1);
27 testLibrary.checkOut(b2, jim);
28 testLibrary.printStatus();
29
30 }
31
32 private void printStatus() {
33
34 System.out.println("Status Report of MyLibrary \n" +
35 this.toString());
36 for (Book thisBook : this.getBooks()) {
37 System.out.println(thisBook);
38
39 }
40
41 for (Person p : this.getPeople()) {
42 int count = this.getBooksForPerson(p).size();
43 System.out
44 .println(p + " (has " + count + " of my books)");
45
46 }
47 System.out.println("Books Available: "
48 + this.getAvailableBooks().size());
49 System.out.println("--- End of Status Report ---");
50
51
52 }

posted on 2011-06-13 15:29  yf.x  阅读(321)  评论(0编辑  收藏  举报

导航