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

    Lesson 11 – Create first methods in MyLibrary class

    • Create test method for addBook, removeBook methods
    • Create addBook, removeBook methods and test
    • Create addPerson, removePerson methods
    • Introduce Eclipse refactoring – move local variable to field
    1 package org.totalbeginner.tutorial;
    2
    3  import java.util.ArrayList;
    4
    5  import org.totoalbeginner.tutorial.Person;
    6
    7  import junit.framework.TestCase;
    8
    9  public class MyLibraryTest extends TestCase {
    10 private Book b1;
    11 private Book b2;
    12 private Person p1;
    13 private Person p2;
    14 private MyLibrary ml;
    15
    16 public void testMyLibrary() {
    17 MyLibrary ml = new MyLibrary("Test");
    18
    19 assertEquals("Test", ml.name);
    20
    21 assertTrue(ml.books instanceof ArrayList);
    22 assertTrue(ml.people instanceof ArrayList);
    23
    24
    25 }
    26
    27 public void setup(){
    28 b1 = new Book("Book1");
    29 b2 = new Book("Book2");
    30
    31 p1 = new Person();
    32 p2 = new Person();
    33 p1.setName("Fred");
    34 p2.setName("Sue");
    35
    36 ml = new MyLibrary("Test");
    37 }
    38
    39 public void testAddBook(){
    40 //create test objects
    41   setup();
    42
    43 //test initial size is 0
    44   assertEquals(0,ml.getBooks().size());
    45
    46 ml.addBook(b1);
    47 ml.addBook(b2);
    48
    49 assertEquals(2, ml.getBooks().size());
    50 assertEquals(0, ml.getBooks().indexOf(b1));
    51 assertEquals(1,ml.getBooks().indexOf(b2));
    52
    53 ml.removeBook(b1);
    54 assertEquals(1, ml.getBooks().size());
    55 assertEquals(0, ml.getBooks().indexOf(b2));
    56
    57 ml.removeBook(b2);
    58 assertEquals(0, ml.getBooks().size());
    59 }
    60
    61 }
    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 }
  • 相关阅读:
    canvas直线学习
    移动端页面练习
    IOS 本地推送(UILocalNotification)
    IOS 社交分享
    IOS 通讯录 (访问,添加,修改)
    IOS 蓝牙(GameKit、Core Bluetooth)
    IOS 获取更多的设备信息
    IOS 摇一摇的方法
    IOS Core Motion、UIAccelerometer(加速计使用)
    IOS UIDevice距离传感器(打开 关闭)
  • 原文地址:https://www.cnblogs.com/halflife/p/2078900.html
Copyright © 2011-2022 走看看