We want to realize a program to be used in a book store to manage the book archive. For each book, the following information is stored: title (a string), author (a string), number of copies that are available (if the number of copies is 0, this means that the book is not available), and price (a double). If the book is not available, the price information is not significant (e.g., you may assume that it is 0).
Part 1.
Write a class Book
that implements the following methods:
Book(String tit, String aut)
: constructor that, given
title, and author, constructs a book that is initially not available;
String getTitle()
: that returns the title of the book;
String getAuthor()
: that returns the author of the book;
int getCopies()
: that returns the number of copies of the
book that are available;
double getPrice()
: that returns the price of the book, if
the book is available, and 0 otherwise;
void buyCopies(int n, double p)
: that increments the number
of available copies by n
(a positive integer), and sets the
price of the book to p
(the previous price, even if
significant, is ignored);
void sellCopy()
: that decrements the number of copies by 1,
if the book is available, and does nothing otherwise;
String toString()
: that returns a string with all the
information about the book.
Use the example program
TestBook.Java
to test the class you have developed.
Part 2.
Write a class UseBook
, client of Book
, that contains
the following public static method:
static String expensiveBook(Book b1, Book b2)
: that, given
two books b1
and b2
, returns the title of the
more expensive book among b1
and b2
. If
b1
and b2
have the same price, the method
should return the title of b1
. If one of b1
or
b2
is not available, the method should return the title of
the available book. If neither b1
nor b2
are
available, the method should return null
.
Use the example program
TestUseBook.java
to test the method you have developed.
Solutions:
Book.java
,
UseBook.java