hello.pbaumgarten.com

Y12 CS students: Students with books exercise

oop
diploma
class-work

#1

Dear Year 12s CS students.

For your current students with books exercise, here is main() code that should work with your Student and Book classes once complete, so you can test your code.

package com.pbaumgarten.oopbasicsqu1;
import java.util.ArrayList;

public class Exercise {
   public static void main(String[] args) {
        // Create some students
        ArrayList<Student> students = new ArrayList<Student>();
        students.add( new Student("Bill") );
        students.add( new Student("Ted") );

        // Create some books
        Book war = new Book("War and Peace", "Leo Tolstoy", 5);
        Book huck = new Book("The Adventures of Huckleberry Finn", "Mark Twain", 10);
        Book hamlet = new Book("Hamlet", "William Shakespeare", 8);
        Book pride = new Book("Pride and Prejudice", "Jane Austen", 12);
        Book alice = new Book("Alice in wonderland", "Charles Dodgson", 11);
        Book murder = new Book("Murder on the Orient Express", "Agatha Christie", 9);

        // Exercise: Part 1 - Students buy books
        System.out.println("PART 1 - STUDENTS BUY BOOKS (ADD THE BOOK TO STUDENT)");
        students.get(0).addBook( war );
        students.get(1).addBook( huck );
        students.get(0).addBook( hamlet );
        students.get(0).addBook( pride );
        students.get(1).addBook( pride );
        students.get(0).addBook( murder );
        students.get(0).addBook( alice );
        students.get(1).addBook( alice );

        // Exercise: Part 5 - Print number of books
        System.out.println("PART 5 - PRINT NUMBER OF BOOKS");
        for (Student student : students ) {
            System.out.println( student.getName() + " has " + student.getBookCount() + " book(s).");
        }

        // Exercise: Part 2 - Students donate books
        System.out.println("PART 2 - STUDENTS DONATE BOOKS (REMOVE THE BOOK FROM STUDENT)");
        students.get(0).removeBook( war );
        students.get(0).removeBook( pride );
        students.get(1).removeBook( alice );

        // Exercise: Part 5 - Print number of books
        System.out.println("PART 5 - PRINT NUMBER OF BOOKS");
        for (Student student : students ) {
            System.out.println( student.getName() + " has " + student.getBookCount() + " book(s).");
        }

        // Exercise: Part 3 - Print list of books
        System.out.println("PART 3 - PRINT LIST OF BOOKS");
        for (Student student : students ) {
            System.out.println( student.getName() + " has the following books:");
            ArrayList<Book> studentsbooks = student.getBooks();
            for (Book b : studentsbooks) {
                System.out.println( " * "+b.getTitle() + " by " + b.getAuthor());
            }
        }

        // Exercise: Part 4 - Find the student(s) who have a book
        System.out.println("PART 4 - WHO HAS A GIVEN BOOK?");
        Book searchFor = alice;
        for (Student student : students ) {
            if (student.hasBook( searchFor )) {
                System.out.println(student.getName()+" has book "+searchFor.getTitle());
            } else {
                System.out.println(student.getName()+" does not has book "+searchFor.getTitle());
            }
        }

    }
}

Produces the following output on screen

PART 1 - STUDENTS BUY BOOKS (ADD THE BOOK TO STUDENT)
PART 5 - PRINT NUMBER OF BOOKS
Bill has 5 book(s).
Ted has 3 book(s).
PART 2 - STUDENTS DONATE BOOKS (REMOVE THE BOOK FROM STUDENT)
PART 5 - PRINT NUMBER OF BOOKS
Bill has 3 book(s).
Ted has 2 book(s).
PART 3 - PRINT LIST OF BOOKS
Bill has the following books:
 * Hamlet by William Shakespeare
 * Murder on the Orient Express by Agatha Christie
 * Alice in wonderland by Charles Dodgson
Ted has the following books:
 * The Adventures of Huckleberry Finn by Mark Twain
 * Pride and Prejudice by Jane Austen
PART 4 - WHO HAS A GIVEN BOOK?
Bill has book Alice in wonderland
Ted does not has book Alice in wonderland