We want to realize a program to handle a document archive. For each document the following information is stored: name of the author, title of the document, and number of available copies.
Part 1.
Write a class Document
that implements the following methods:
Document(String a, String t)
: costructor that, given author
and title, constructs a document for which the number of available copies
is 0;
String toString()
: that returns a string with all the
information about the document;
void createCopies(int n)
: that increments by n
the number of available copies;
void sellCopies(int n)
: that decrements by n
the number of available copies; (if n
is greater than the
number of available copies, then such number should become 0);
String getAuthor()
: that returns the name of the author of
the document;
String getTitle()
: that returns the title of the document;
int getCopies()
: that returns the number of available
copies of the document.
Example program:
TestDocument.java
Part 2.
Write a class UseDocument
, client of Document
, that
contains the following public static methods:
static String authorMaxCopies(Document d1, Document d2)
: that, given two documents d1
and d2
, returns
the author of the document among d1
and d2
for
which more copies are available. If for both documents the same number
of copies is available, the method should return the concatenation of the
names of the two authors, separated by " and "
.
static int countCopiesAuthor(Document[] docs, String auth)
:
that, given an archive docs
of documents, rappresented
simply as an array of documents, and an author auth
, returns
the total number of copies of all documents with author auth
present in docs
.
Example program:
TestUseDocument.java
Solutions:
Document.java
,
UseDocument.java