zoukankan      html  css  js  c++  java
  • 【笔记】Eclipse and Java for Total Beginners—015

    Lesson 15 – Finish MyLibrary Methods

    • Create test for getAvailableBooks
    • Create getAvailableBooks
    • Create getUnavailableBooks

    1 package org.totalbeginner.tutorial;
    2
    3  import java.util.ArrayList;
    4
    5  import org.omg.CORBA.PUBLIC_MEMBER;
    6 import org.totoalbeginner.tutorial.Person;
    7
    8 import junit.framework.TestCase;
    9
    10 public class MyLibraryTest extends TestCase {
    11 private Book b1;
    12 private Book b2;
    13 private Person p1;
    14 private Person p2;
    15 private MyLibrary ml;
    16
    17 public void testMyLibrary() {
    18 MyLibrary ml = new MyLibrary("Test");
    19
    20 assertEquals("Test", ml.name);
    21
    22 assertTrue(ml.books instanceof ArrayList);
    23 assertTrue(ml.people instanceof ArrayList);
    24
    25
    26 }
    27
    28 public void setup(){
    29 b1 = new Book("Book1");
    30 b2 = new Book("Book2");
    31
    32 p1 = new Person();
    33 p2 = new Person();
    34 p1.setName("Fred");
    35 p2.setName("Sue");
    36
    37 ml = new MyLibrary("Test");
    38 }
    39
    40 public void testAddBook(){
    41 //create test objects
    42 setup();
    43
    44 //test initial size is 0
    45 assertEquals(0,ml.getBooks().size());
    46
    47 ml.addBook(b1);
    48 ml.addBook(b2);
    49
    50 assertEquals(2, ml.getBooks().size());
    51 assertEquals(0, ml.getBooks().indexOf(b1));
    52 assertEquals(1,ml.getBooks().indexOf(b2));
    53
    54 ml.removeBook(b1);
    55 assertEquals(1, ml.getBooks().size());
    56 assertEquals(0, ml.getBooks().indexOf(b2));
    57
    58 ml.removeBook(b2);
    59 assertEquals(0, ml.getBooks().size());
    60 }
    61
    62 public void testCheckOut() {
    63 // set up objects
    64 setup();
    65
    66 addItems();
    67
    68 assertTrue("Book did not check out correctly",
    69 ml.checkOut(b1,p1));
    70
    71 assertEquals("Fred",b1.getPerson().getName());
    72
    73 assertFalse("Book was already checked out",
    74 ml.checkOut(b1,p2));
    75
    76 assertTrue("Book check in failed", ml.checkIn(b1));
    77
    78 assertFalse("Book was already checked in", ml.checkIn(b1));
    79
    80 assertFalse("Book was never checked out", ml.checkIn(b2));
    81
    82 // additional test for maximumBooks
    83 setup();
    84 p1.setMaximumBooks(1);
    85 addItems();
    86
    87 assertTrue("First book did not check out",
    88 ml.checkOut(b2, p1));
    89 assertFalse("Second book should not have checked out",
    90 ml.checkOut(b1, p1));
    91 }
    92
    93 private void addItems() {
    94 ml.addBook(b1);
    95 ml.addBook(b2);
    96 ml.addPerson(p1);
    97 ml.addPerson(p2);
    98 }
    99
    100 public void testGetBooksForPerson() {
    101 setup();
    102 addItems();
    103 assertEquals(0, ml.getBooksForPerson(p1).size());
    104
    105 ml.checkOut(b1, p1);
    106
    107 ArrayList<Book> testBooks = ml.getBooksForPerson(p1);
    108 assertEquals(1, testBooks.size());
    109 assertEquals(0, testBooks.indexOf(b1));
    110
    111 ml.checkOut(b2, p1);
    112 testBooks = ml.getBooksForPerson(p1);
    113 assertEquals(2, testBooks.size());
    114 assertEquals(1, testBooks.indexOf(b2));
    115
    116 }
    117
    118 public void testGetAvailableBooks() {
    119 setup();
    120 addItems();
    121 ArrayList<Book> testBooks = ml.getAvailableBooks();
    122 assertEquals(2, testBooks.size());
    123 assertEquals(1, testBooks.indexOf(b2));
    124
    125 ml.checkOut(b1, p1);
    126 testBooks = ml.getAvailableBooks();
    127 assertEquals(1, testBooks.size());
    128 assertEquals(0, testBooks.indexOf(b2));
    129
    130 ml.checkOut(b2, p1);
    131 testBooks = ml.getAvailableBooks();
    132 assertEquals(0, testBooks.size());
    133 }
    134
    135 public void testGetUnavailableBooks() {
    136 setup();
    137 addItems();
    138 assertEquals(0, ml.getUnavailableBooks(p1).size());
    139
    140 ml.checkOut(b1, p1);
    141
    142 ArrayList<Book> testBooks = ml.getUnavailableBooks(p1);
    143 assertEquals(1, testBooks.size());
    144 assertEquals(0, testBooks.indexOf(b1));
    145
    146 ml.checkOut(b2, p1);
    147 testBooks = ml.getUnavailableBooks(p1);
    148 assertEquals(2, testBooks.size());
    149 assertEquals(1, testBooks.indexOf(b2));
    150
    151 }
    152
    153 public void testToString() {
    154 setup();
    155 addItems();
    156 assertEquals("Test: 2 books; 2 people.",
    157 ml.toString());
    158 }
    159
    160
    161 }

    *******************************************************************************************

    1 package org.totalbeginner.tutorial;
    2
    3 import java.util.ArrayList;
    4
    5 import org.totoalbeginner.tutorial.Person;
    6
    7 public class MyLibrary {
    8
    9 String name;
    10 public String getName() {
    11 return name;
    12 }
    13
    14 public ArrayList<Person> getPeople() {
    15 return people;
    16 }
    17
    18 public ArrayList<Book> getBooks() {
    19 return books;
    20 }
    21
    22 ArrayList<Person> people;
    23 ArrayList<Book> books;
    24
    25 public MyLibrary(String name) {
    26 this.name = name;
    27 books = new ArrayList<Book>();
    28 people = new ArrayList<Person>();
    29
    30 }
    31
    32 public void addBook(Book b1) {
    33 this.books.add(b1);
    34
    35 }
    36
    37 public void removeBook(Book b1) {
    38 this.books.remove(b1);
    39
    40 }
    41
    42 public void addPerson(Person p1){
    43 this.people.add(p1);
    44 }
    45
    46 public void removePerson(Person p1){
    47 this.people.remove(p1);
    48 }
    49
    50 public boolean checkOut(Book b1, Person p1) {
    51 int booksOut = this.getBooksForPerson(p1).size();
    52 if ((b1.getPerson() == null) &&
    53 (booksOut < p1.getMaximumBooks())) {
    54 b1.setPerson(p1);
    55 return true;
    56 }
    57 else {
    58 return false;
    59 }
    60 }
    61
    62 public boolean checkIn(Book b1) {
    63 if (b1.getPerson() != null) {
    64 b1.setPerson(null);
    65 return true;
    66 }
    67 else {
    68 return false;
    69 }
    70 }
    71
    72 public ArrayList<Book> getBooksForPerson(Person p1) {
    73 ArrayList<Book> result = new ArrayList<Book>();
    74 for (Book aBook : this.getBooks()) {
    75 if (aBook.getPerson() != null &&
    76 (aBook.getPerson().getName().equals(p1.getName()))) {
    77 result.add(aBook);
    78 }
    79
    80 }
    81 return result;
    82 }
    83
    84 public ArrayList<Book> getAvailableBooks() {
    85 ArrayList<Book> result = new ArrayList<Book>();
    86 for (Book aBook : this.getBooks()) {
    87 if (aBook.getPerson() == null) {
    88 result.add(aBook);
    89 }
    90
    91 }
    92 return result;
    93 }
    94
    95 public ArrayList<Book> getUnavailableBooks(Person p1) {
    96 ArrayList<Book> result = new ArrayList<Book>();
    97 for (Book aBook : this.getBooks()) {
    98 if (aBook.getPerson() != null) {
    99 result.add(aBook);
    100 }
    101
    102 }
    103 return result;
    104 }
    105
    106 public String toString() {
    107 return this.getName() + ": " +
    108 this.getBooks().size() + " books; " +
    109 this.getPeople().size() + " people.";
    110 }
    111 }

    ******************************************************************************************

    1 package org.totalbeginner.tutorial;
    2
    3 import org.totoalbeginner.tutorial.Person;
    4
    5 import junit.framework.TestCase;
    6
    7 public class BookTest extends TestCase {
    8
    9 public void testBook() {
    10 Book b1 = new Book("Great Expectations");
    11 assertEquals("Great Expectations", b1.title);
    12 assertEquals("unknown author", b1.author);
    13 }
    14
    15 public void testGetPerson() {
    16 Book b2 = new Book("War and Peace");
    17 Person p2 = new Person();
    18 p2.setName("Elvis");
    19
    20 // method to say book is loaded to this person
    21 b2.setPerson(p2);
    22
    23 // get the name of the person who has the book
    24 // Person testPerson = b2.getPerson();
    25 // String testName = testPerson.getName();
    26
    27 String testName = b2.getPerson().getName();
    28 assertEquals("Elvis", testName);
    29 }
    30
    31 public void testToString() {
    32 Book b2 = new Book("War and Peace");
    33 Person p2 = new Person();
    34 p2.setName("Elvis");
    35
    36 assertEquals("War and Peace by unknown author; Available",
    37 b2.toString());
    38
    39 b2.setPerson(p2);
    40 assertEquals("War and Peace by unknown author; Checked out to Elvis",
    41 b2.toString());
    42 }
    43 }

    ******************************************************************************************

    1 package org.totalbeginner.tutorial;
    2
    3 import org.totoalbeginner.tutorial.Person;
    4
    5 public class Book {
    6
    7 String title;
    8 String author;
    9 Person person;
    10
    11 public Book(String string) {
    12 this.title = string;
    13 this.author = "unknown author";
    14 }
    15
    16 public String getAuthor() {
    17 return author;
    18 }
    19
    20 public void setAuthor(String author) {
    21 this.author = author;
    22 }
    23
    24 public String getTitle() {
    25 return title;
    26 }
    27
    28 public void setPerson(Person p2) {
    29 this.person = p2;
    30
    31 }
    32
    33 public Person getPerson() {
    34
    35 return this.person;
    36 }
    37
    38 public String toString() {
    39 String available;
    40 if (this.getPerson() == null) {
    41 available = "Available";
    42 }
    43 else {
    44 available = "Checked out to " +
    45 this.getPerson().getName();
    46 }
    47 return this.getTitle() + " by " + this.getAuthor() +
    48 "; " + available;
    49 }
    50
    51 }

  • 相关阅读:
    在R语言中轻松创建关联网络
    在R语言中显示美丽的数据摘要summary统计信息
    R语言中不同类型的聚类方法比较
    R语言中的划分聚类模型
    R语言解释生存分析中危险率和风险率的变化
    Stata估算观测数据的风险比
    Stata 中Mata的st_view函数
    R语言多臂试验
    R语言使用倾向评分提高RCT(随机对照试验)的效率
    R语言在RCT中调整基线时对错误指定的稳健性
  • 原文地址:https://www.cnblogs.com/halflife/p/2079686.html
Copyright © 2011-2022 走看看