Software Design
classes and Objects
Due Date: Thursday, September 10
Points: 20 points
Suppose the local library has asked you to develop a program to
help them track their books. In order to develop this program you decide to develop a class called Book
which stores information about a single book (not multiple books). This class will have the following
instance variables (all of which are declared to be private) and methods defined:
Instance Variables of the class book
- bookName
- totalCopies (number of copies the library owns)
- copiesOut (number of copies currently checked out)
Methods of the class Book
- default constructor that initializes the name to empty string,and both copy counts to 0
- a constructor that takes a name and total copies as parameters to intialize those variables, but
still initializes copiesOut to 0
- getName(), getTotalCopies(), getCopiesOut() - returns the value for each variable
- setName(String newName), setTotalCopies(int total) - sets the values for these variables
- addCopies(int moreCopies) - adds moreCopies to the total number of copies
- checkOut() - checks out a copy of the book - adds one to copies out. Only does this if a
copy is available. Returns true if the copy was checked out, false if there wasn't a copy
available
- checkAvailable() - returns true if there is a copy available to be checked out (copiesOut is
less than totalCopies)
- return() - returns a copy of the book - subtracts 1 from the copiesOut variable
Now suppose that the driver class for your program (the class that has the main method in it) has an array of books declared (or an ArrayList if
you want to study the java API and learn how to use that class) and these other variables:
Book[] libraryBooks = new Book[500]; (or ArrayList libraryBooks;)
int totalBooks; //total number of books being stored in the array
Using Netbeans, create a Java project with a Book class and a driver class with a main method that includes a menu which allows the user to do the
following:
- Add a new book to the library. This code should prompt the user for the
name and number of copies of the book, then create a new book at the appropriate
position of the libraryBooks array (you will need to use one of the constructor
methods of the Book class to first initialize that position of the array)
- Checkout a book. This should prompt the user for the name of the book. Then
update the appropriate position of the libraryBooks array. If the book is not
in the library print a message to the user telling them that. If there is not a
copy of that book available print a message to the user.
- Return a book. This should prompt the user for the name of the book and then
update the appropriate position of the libraryBooks array.
Return to the Software Design Home
Page