This assignment is to be done individually and handed in electronically (using submit) before 12:00pm on Tuesday, February 8. |
You are to write a Java program to maintain an inventory of sales of popular books, grouped by author.
You are given a file of book sales, each entry of which includes the name of the author, the title of the book, and the number of copies sold by one store. You must track total sales for each book, and be prepared to report these totals as well as calculating the total sales for each author.
Your employer has dictated that you are to store the sales information using a list structure that keeps its entries in the order in which they were most recently changed. Using such a list, whenever an entry is to be modified, it must first be moved to the head of the list, so the most recently changed entry appears first, the entry that was second most recently changed appears second, and the entry that was least recently changed appears last on the list.
A list organized in this fashion can be described using two interfaces: one describes a key-value pair and the other describes the list itself, which is accessed based on matching the key.
public interface Pair {
/* object pairing a key and some value */
public Object getKey();
//pre: returns the key for this
public Object getValue();
//pre: returns the value for this
public void setKey(Object newKey);
//pre: newKey is not null
//post: the key field contains newKey
public void setValue(Object newValue);
//pre: newValue is not null
//post: the value field contains newValue
}
public interface FrontList {
/* finite (possibly empty) collection of Pair objects, ordered by most recent use
public boolean isEmpty();
//post: returns true iff this is empty
public void add(Pair newItem);
//pre: newItem is not null
//post: inserts newItem at the head of the list
// (before any Pairs that may already be present)
public boolean moveToFront(Object key);
//pre: key is not null
//post: if a Pair matching the key is an element of this,
// that Pair is moved to the head of this and the method returns true;
// otherwise the method returns false (and the list is unchanged)
public Pair getFirst();
//pre: this is not empty
//post: returns the Pair at the head of this
public void replaceFirst(Pair newItem);
//pre: key is not null
// this is not empty
//post: replaces the Pair at the head of the this by newItem
public Object[] dump();
//post: returns an array holding the Pairs from this in the same order
}
The information for a particular book title can then be stored as a pair for which the key is the book title and the associated value is the total number of sales for that book. The information for an author can be stored as a pair for which the key is the author’s name and the associated value is the list of books (title-sales pairs) written by that author. Finally, the sales inventory is a list of authors (each of which is then associated with a list of title-sales pairs). For simplicity, we’ll assume that each book has only one author and that the author is uniquely identified by his or her last name.
A sample file of sales records can be found in sales.txt, resulting in an inventory structure illustrated in inventory.txt, where lists are shown with their heads towards the top of the page. Notice that the ordering of each list from head to tail is determined by how recently each item in the list was last updated.
The input to your program will be a file of sales records, one per line, arbitrarily interspersed with a “report” line consisting of a single question mark (“?”) only. Each time you read a sales record, you are to update the inventory (updating one of the book lists as well as the author list). Each time you read a report line, you are to append a complete sales report to report.txt, one line per book as well as a totals line for each author, in the order that the books and authors appear in the inventory. For the inventory shown in inventory.txt, a corresponding report can be found in report.txt.
As part of your solution, you must design and implement classes to
represent
both interfaces described above. The class implementing FrontList
must
use singly-linked lists, which
means
the methods manipulate objects from the Node
class as defined in the notes and
text.
This Node
class should not be
changed. We will provide the exact same Node
class used when executing your code
upon submission, so you need not submit or change this class.
public class DataFactory {
/* generator of instances for each abstract data type */
public static Pair makePair(){
//post: returns an instance of the ADT Pair
return (Pair) new PairClass(); /* use the name of your class here */
}
public static FrontList makeFrontList(){
//post: returns an instance of the ADT FrontList
return (FrontList) new FrontListUsingLinks (); /* use your class name */
}
}
Important Notes! You may only use the becker, java.lang and java.util packages. Contact the tutor before using any other packages. Not doing so may cause your program to fail compilation. For this and all subsequent programming assignments in CS124 you are not allowed to use package names. That is, your files cannot begin with something like package Assign02; This causes marking scripts to fail. If you make package names in this way and the marking scripts fail, you will be penalized. |
method: isEmpty()
condition: the FrontList is empty
input: The first sales record processed is ‘Brown "The Da Vinci Code" 12’ followed by ‘?’
output: The
output shows ‘Brown
"The Da Vinci Code"
12’ followed by ‘Brown 12’
explanation: Before recording the first author record and first title record for that author, the system checks for the empty list.
method isEmpty()
condition: the FrontList is not empty
….
Design |
15% |
Documentation (including pre- and post-conditions) |
28% |
Correctness |
45% |
Testing |
12% |
You can also view the marking scheme for Assignment 2.
For every file you submit, include your name and ID number. Submit:
Note: Late assignments will NOT be accepted. |