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

    Lesson 12 – Create checkOut, checkIn Methods

    • Create test for checkOut, checkIn methods
    • Write checkout method
    • Introduce if / then / else syntax
    • Introduce boolean method
    • Write checkIn method

    1. testCheckOut Method

        Test both the checkOut and checkIn methods

    • Check Out a book
    • Check Out second book
    • Check in a book
    • Check in second book
    • assert  statements after each method call

    2. checkOut Method Design

    • Enter Person object into person field of Book
    • Use setPerson(Person) method
    • What if book is already checked out? Will not allow this.
    • Need to test for this in checkOut method
    • Method might succeed or fial, depending on situation.
    • Should we tell the calling method whether it succeeded of failed ? yes.

    3. checkOut Method Pseudo Code

    • Check that this book is not already checked out.
    • If it is not checked out, set the person field in the book to this person and let calling method know it worked.
    • Otherwise, Let calling method know it didn’t work.

    4. checkOut Method Signature

    • Will return a boolean (true or false) to indicate success.
    • In pseudo code, we had this book and this person – which book and person?
    • Book and Person will be method parameters – checkOut (Book b, Person P)
    • public boolean checkOut(Book b, Person p)

    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 public void testCheckOut() {
    62 // set up objects
    63 setup();
    64
    65 ml.addBook(b1);
    66 ml.addBook(b2);
    67 ml.addPerson(p1);
    68 ml.addPerson(p2);
    69
    70 assertTrun("Book did not check out correctly",
    71 ml.checkOut(b1,p1));
    72
    73 assertEquals("Fred",b1.getPerson().getName());
    74
    75 assertFalse("Book was already checked out",
    76 ml.checkOut(b1,p2));
    77
    78 assertTrue("Book check in failed", ml.checkIn(b1));
    79
    80 assertFalse("Book was already checked in", ml.checkIn(b1));
    81
    82 assertFalse("Book was never checked out", ml.checkIn(b2));
    83
    84
    85
    86
    87 }
    88 }
    89
    90 }

    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 p2) {
    51 if (b1.getPerson() == null) {
    52 b1.setPerson(p1);
    53 return true;
    54 }
    55 else {
    56 return false;
    57 }
    58 }
    59
    60 public boolean checkIn(Book b1) {
    61 if (bl.getPerson() != null) {
    62 b1.setPerson(null);
    63 return true;
    64 }
    65 else {
    66 return false;
    67 }
    68 }
    69 }

  • 相关阅读:
    C语言考点例题解析
    五笔打字
    常用快捷键
    网络基础知识
    人口增长
    8 封装
    9 绑定方法和非绑定方法
    6 抽象类
    7 多态和多态性
    5 组合
  • 原文地址:https://www.cnblogs.com/halflife/p/2079350.html
Copyright © 2011-2022 走看看