Assignment: OOP: PR1 - Fall 2021
Assignment: OOP: PR1 - Fall 2021
Assignment: OOP
IMPORTANT: This assignment must be done individually.
A. Description
In this assignment, you will create a menu-driven console program to manage books in a library
named “BookMan”.
-----------------------------------
1. list all books
2. add a new book
3. edit book
4. delete a book
5. search books by name
6. sort books descending by price
In a fixed-length format file, you are provided with Book details in "books.txt". Whenever
starting the program, it reads the file then parses the data for Book objects and add to the list of
books. The length and position of attributes are as follows.
1|P a g e
Input format:
Output format:
Sample input:
Sample output:
Loading books...
Display a list of all books in a tabular form in the console. If no books, print out “(empty)”.
Output format:
Sample output:
- Case: not empty
ID Name Price
1 Think big and Grow rich 10.95
2 The 7 habits of highly effective people 15.97
3 How to win friends and influence people 14.95
- Case: empty
(empty)
2|P a g e
3. Feature: Add a new book
Get details from user to create a new Book object, then add it into the list of books.
Input format:
Output format:
Sample:
- Case: success
- Case: error
Ask user to enter book id to edit. If the Book object with entered id does not exist in the list of
books, notify user “Invalid ID!”, otherwise get other details from user then update the Book
object.
Input format:
Output format:
3|P a g e
Sample:
- Case: success
- Case: error
Ask user to enter book id to edit. If the Book object with entered id does not exist in the list of
books, notify user “Invalid ID!”, otherwise remove the Book object from the list of books.
Input format:
Output format:
Sample:
- Case: success
- Case: error
4|P a g e
6. Feature: Search books by name
Get keyword from user, filter Book objects in the list of books with name contains keyword
(case insensitive), then print out in a tabular form in the console. If no books, print out
“(empty)”.
Input format:
Output format:
Sample:
- Case: matches
- Case: no result
Sort books descending by price then print out in a tabular form in the console. If no books, print
out “(empty)”.
Output format:
5|P a g e
- Print 2 digits after the decimal for the double datatype
- Refer to the sample output for the formatting specification
Sample output:
After sorting:
ID Name Price
2 The 7 habits of highly effective people 15.97
3 How to win friends and influence people 14.95
1 Think big and Grow rich 10.95
- Case: empty
Save all books into file books.txt in required format, then end program. If no books, write
nothing to the file.
The books.txt is a fixed-length format file. The length and position of attributes are as follows.
Output format:
- Use "%5d %-45s %10.2f" for displaying Book details
- Print 2 digits after the decimal for the double datatype
- Refer to the sample output for the formatting specification
Sample output:
Saving to file...
Bye!
6|P a g e
B. Programing tasks
Attribute Datatype
id Integer
name String
price Double
Method Description
public Book(int id, String name, This constructor is to create a new Book object
double price) from provided book details
This setter updates this.name with provided
public void setName(String name)
value
This setter updates this.price with provided
public void setPrice(double price)
value
This method returns string representation of this
Book in the required format. Refer to the sample
public String toString()
outputs in Section A for the formatting
specification.
Consider the class BookManager and define the following attribute & methods
Attribute Datatype
books ArrayList<Book>
7|P a g e
Method Description
This constructor initializes this.books as an
public BookManager()
empty list
public ArrayList<Book> getBooks() This getter returns all books managed by this
This method reads the file books.txt, parses the
public void loadFromFile() data in the file to Book objects and adds them to
this.books.
This method accepts a list of Book objects as
public void the argument and prints out the Book details in
printBooks(ArrayList<Book> books) required format by iterating the list. If the input
list books is empty, print “(empty)”
This method accepts a Book object as an input
argument and adds it to this.books if book.id
public boolean add(Book book)
is not duplicated, then returns whether added or
not
This method accepts an id of Book as input and
public Book getBookById(int id) returns the Book object from this.books with
the corresponding id.
This method accepts a Book object as the
public void remove(Book book)
argument and remove it from this.books.
This method modifies this.books to be sorted
public void sortDescByPrice()
in the descending order of price.
This method accepts a keyword string as
public List<Book> argument and returns the list of Book objects
searchByName(String keyword) whose name contains the keyword
(IMPORTANT: case-insensitive)
This method writes Book objects managed by
public void saveToFile() this into the file books.txt in the required
format.
Consider a program class called Main to show the menu, get user inputs, invoke appropriate
methods from other classes to carry out functionalities & print out suitable messages in the main
method. This method also prints out “Bye!” when user exit the program.
C. Screenshots
Loading books...
-----------------------------------
1. list all books
2. add a new book
3. edit book
4. delete a book
5. search books by name
8|P a g e
6. sort books descending by price
9|P a g e
5. search books by name
6. sort books descending by price
10 | P a g e
5 Harry Potter and the Death Hallows 23.00
-----------------------------------
1. list all books
2. add a new book
3. edit book
4. delete a book
5. search books by name
6. sort books descending by price
11 | P a g e
3 How to win friends and influence people 14.95
-----------------------------------
1. list all books
2. add a new book
3. edit book
4. delete a book
5. search books by name
6. sort books descending by price
12 | P a g e
Saving to file...
Bye!
D. Submission
You must submit a single zip file containing the application to the portal by the due date. The zip
file name must be of the form a1_Sid.zip, where Sid is your student identifier (the remaining bits
of the file name must not be changed!). For example, if your student id is 2001040001 then your
zip file must be named a1_2001040001.zip.
IMPORTANT: failure to name the file as shown will result in no marks being given!
13 | P a g e