Download Assignment 7 -- Collection Manager

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Assignment 7 – Collection Manager
Tom Jensen, Dean Zeller
CS102
Spring 2017
Objective
20 points
Early Submission: Friday, March 3rd
Due: Friday, March 10th
The student will implement a Java program to manage a collection through a text menu interface
and file input/output.
Background – Interface
All programs need some sort of interface to communicate with the user. For assignment 6, the test items were
entered manually through Java programming. This is fine for an early testing of the program, but a programmer
should always keep usability in mind. The text-menu interface, while simplistic, is an effective way to create a
interface with the program. Naturally, there are far more effective user interfaces, but this is an excellent
starting point.
Background – File I/O
Up until now, storage has been temporary, and all is lost when the program ends. Programs should have some
sort of method for saving data in permanent external storage. The template file does file I/O using comma
separated values (CSV) format, saved in an external file on the hard drive. This will allow for saving large
amounts of data without having to enter it every time for testing.
Basic Requirements
1. Download the files ComicBook.java, ComicCollection.java, and
ComicInterface.java from the course website. This is the initial template for keeping track of a
comic book collection. There are a few changes in the first two files from last time, but they are
otherwise the same. The interface file is new.
2. Before programming, read over and execute the template to understand everything it is doing and how it
works. Run the template, and identify the pieces necessary for your code. In the past, you simply had to
modify an existing template. But in this assignment, you will have to incorporate features from the
template into your existing code. Pay special attention to the menu-driven interface and the file I/O, as
these are the new topics for this assignment.
3. The initEmptyCollection does not read in the name of the collection from the user. Make sure it
does this correctly, and saves the name in the data file. Also modify the readFromFile in Collection to
overwrite the collection name with what is stored in the datafile.
4. A menu-driven interface for the user has been created in the template. Make the appropriate changes to
your collection code to incorporate a menu-driven text interface for the user to control the program.
Also incorporate commands to load and save data from a file supplied by the user. (Make note of the
new method in Comicbook, toCSV().)
5. The fileIO in the template is only applicable to comic books. Modify and test the fileIO commands to
work for your collection item. Perform rigorous testing with file saving and reading, as these tend to be
more complicated, from a technical standpoint.
6. Currently, addComic has no error-checking to make sure there is enough space for incoming comics.
Ensure that the user is not able to overfill the array space with items. This can be accomplished either
by preventing the item add, or finding a way to get more storage space.
7. Currently, removeComic does nothing. You will have to implement a remove method for your
collection object. This will involve multiple changes, as the space of the removed item is empty and
available for another. When an item is removed, you will have to figure out how to handle the empty
space.
8. Implement the search collection method to search for a specific target in the collection.
Page 1
9. In the Collection object, there are stubs for sortByTitle, sortByWriter, and sortByArtist.
These are only here in preparation for the next assignment. These methods are currently not
implemented or included within the interface. Modify the titles of the three sorting methods to be
appropriate for your collection object. Also modify the program interface to allow for calls to these
methods. It may be done with three new menu commands (one per sorting option), or have one new
menu command that asks which of the options the user would like. At this point, you do not need to
implement the sorting methods. Just mention that they were called, but do nothing. The next
assignment will cover the sorting algorithm.
10. Complete the method stub for printCollectionTable, based on PrintCollectionList.
Modify the output so it has a header, nicely formatted output in a table, and a summary footer. The list
should look similar to the following:
Title
#
Writer
Artist
Letterer
Colorist
--------------------------------------------------------------------------------------------------Great Lakes Avengers 3
Zac Gorman
Will Robson
Jessica Jones
3
Brian Michael Bendis Michael Gaydos
Cory Petit
Matt Hollingsworth
Avengers
3
Mark Waid
Mike Del Mundo
11. Modify the quitProgram method to ask the user if they would like to save the data, and does so if
requested.
12. Use your program to create two test files for different collections. Put 5-10 items in one data file, and
10-20 in another.
13. Create block documentation listing yourself as the programmer.
14. Use inline documentation within the code to the OOP structure, as shown in the template.
15. Have the assignment program up and running on a computer. You will review your program with the
instructor or TA, based on the rubric on the last page.
16. You should have a total of five files, two object definitions, one main program, and two test data files.
Submit your programs and data files to the appropriate Canvas folder.
Grading
You will be graded on the following criteria:
Effort
Making all appropriate modifications to the previous assignment
Readability
Block documentation and inline comments
Creativity
Imagination and originality in problem solving
There are many opportunities for extra credit on this assignment, through adding new features, interface
options, output formats, etc…
Page 2
ComicBook.java
Add this method to ComicBook.java. Otherwise, everything else should be the same.
//Return the entire object as a single String, comma separated values
public String toCSV(){
String result = "";
result += this.title + ",";
result += this.issueNum + ",";
result += this.writer + ",";
result += this.artist + ",";
result += this.letterer + ",";
result += this.colorist + ",";
return result;
}
ComicCollection.java
This is ComicCollection.java, in its entirety.
/**********************************************************************
*
Assignment 7 -- ComicCollection
*
*
*
* PROGRAMMER: (Your name) (current date)
*
* CLASS: CS102
*
* SEMESTER: Spring, 2017
*
* INSTRUCTOR: (Your instructor)
*
*
*
* DESCRIPTION:
*
* The following is an OOP definition for a comic book collection.
*
*
*
* EXTERNAL FILES AND LIBRARIES
*
*
ComicBook.Java -- Object definition for a comic book
*
*
*
* CREDITS:
*
* This program is copyright (c) 2017 (Your name).
*
* Based on a template written by Tom Jensen and Dean Zeller for
*
* CS102.
*
********************************************************************/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class ComicCollection
{
/********************************************************************
* ATTRIBUTES
*
******************************************************************/
private String name;
private ComicBook[] comics;
private int numComics;
/********************************************************************
* CONSTRUCTORS
*
******************************************************************/
// all necessary parameters for attributes
public ComicCollection(String name_, int size)
{
this.name = name_;
comics = new ComicBook[size];
Page 3
numComics=0;
}
// missing collection size
public ComicCollection(String name_)
{
this.name = name_;
comics = new ComicBook[50];
numComics=0;
}
// missing collection name
public ComicCollection(int size)
{
this.name = "My comic book collection";
comics = new ComicBook[size];
numComics=0;
}
// no initial parameters
public ComicCollection()
{
comics = new ComicBook[50];
numComics=0;
}
/********************************************************************
* GET and SET METHODS
*
******************************************************************/
// return a comic given the index
public ComicBook getComic(int index) { return comics[index]; }
// return the remaining space available in the comics array
public int spaceAvailable() { return comics.length-numComics; }
// return the number of comics in the array
public int getNumComics() { return numComics; }
public String getName(){ return name; }
/********************************************************************
* OUTPUT METHODS
*
******************************************************************/
// print the entire collection, in list format
public void printCollectionList()
{
System.out.println("\nCollection: "+this.name+"\n");
System.out.println("--------------------------------------------");
for (int i = 0; i < numComics; i++)
{
System.out.println((i+1)+": "+this.getComic(i).toString());
}
System.out.println(numComics+" in collection.");
System.out.println("--------------------------------------------");
}
// print the entire collection, in table format
Page 4
public void printCollectionTable()
{
}
// print a summary of the collection
public void printSummary()
{
for (int i = 0; i < numComics; i++)
{
System.out.println(this.getComic(i).description());
}
}
public void SearchByTitle()
{
}
public void SearchByCreator()
{
}
/********************************************************************
* OTHER METHODS
*
******************************************************************/
public void addComic(ComicBook newComic)
{
comics[numComics] = newComic;
numComics++;
}
public void readFromFile(String filename)
{
try
{
Scanner inputStream = new Scanner(new File(filename));
//Skip the first line of the file
int size = inputStream.nextInt();
String name = inputStream.nextLine();
for (int i =0; i < size; i++)
{
String line = inputStream.nextLine();
String[] temp = line.split(",");
String series =
temp[0];
int number =
Integer.parseInt(temp[1]);
String author =
temp[2].equals("null") ? null : temp[2];
String illustrator = temp[3].equals("null") ? null : temp[3];
String letters =
temp[4].equals("null") ? null : temp[4];
String colors =
temp[5].equals("null") ? null : temp[5];
this.addComic(new ComicBook(series, number, author, illustrator, letters,
colors));
}
inputStream.close();
System.out.println("Read "+size+" comics into collection.");
}
catch(FileNotFoundException e)
{
Page 5
System.out.println("Cannot find file " + filename);
}
}
public void writeToFile(String filename)
{
FileWriter fileWriter = null;
try
{
fileWriter = new FileWriter(filename);
fileWriter.write(this.getNumComics() + " " + this.getName()+"\n");
for (int i = 0; i < this.getNumComics(); i++)
{
ComicBook book = this.getComic(i);
if (book == null) continue;
fileWriter.write(book.toCSV()+"\n");
}
fileWriter.close();
}
catch (IOException e)
{
System.out.println("File does not exist.");
}
}
// sort collection by title
public void sortByTitle()
{
System.out.println("Sorting by title...");
}
// sort collection by writer
public void sortByWriter()
{
System.out.println("Sorting by writer...");
}
// sort collection by artist
public void sortByArtist()
{
System.out.println("Sorting by artist...");
}
}
Page 6
ComicInterface.java
This is ComicInterface.java, in its entirety.
/**********************************************************************
*
Assignment 7 -- ComicInterface
*
*
*
* PROGRAMMER: (Your name) (current date)
*
* CLASS: CS102
*
* SEMESTER: Spring, 2017
*
* INSTRUCTOR: (Your instructor)
*
*
*
* DESCRIPTION:
*
* The following is a text-based menu interface for the
*
* ComicCollection object.
*
*
*
* EXTERNAL FILES AND LIBRARIES
*
*
ComicCollection.Java -- Object definition for a comic book
*
*
collection.
*
*
*
* CREDITS:
*
* This program is copyright (c) 2017 (Your name).
*
* Based on a template written by Tom Jensen and Dean Zeller for
*
* CS102.
*
********************************************************************/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class ComicInterface
{
private static ComicCollection collection;
private static Scanner kb = new Scanner(System.in);
// main program
public static void main(String args[])
{
System.out.println("(Starting main program)");
String choice = "";
initEmptyCollection();
do
{
choice = getMenuChoice();
switch(choice.toLowerCase())
{
case("a"): addComic();
break;
case("d"): deleteComic();
break;
case("p"): printCollectionList();
break;
case("s"): searchCollection();
break;
case("r"): readFromFile();
break;
case("w"): writeToFile();
break;
case("q"): quitProgram();
break;
Page 7
default:
System.out.println(choice+" is not an available choice.");
}
} while (!choice.equalsIgnoreCase("q"));
}
// initialize an empty collection
private static void initEmptyCollection()
{
System.out.println("(Initializing empty collection)");
collection = new ComicCollection();
}
// display menu, and return the user selection
private static String getMenuChoice()
{
System.out.println("+------------------MENU------------------+");
System.out.println("| (A)dd new comic book to collection
|");
System.out.println("| (D)elete comic book from collection
|");
System.out.println("| (P)rint collection of comics
|");
System.out.println("| (S)earch collection of comics
|");
System.out.println("| (R)ead collection from file
|");
System.out.println("| (W)rite collection to file
|");
System.out.println("| (Q)uit program
|");
System.out.println("+----------------------------------------+");
System.out.print("Enter your choice => ");
String choice = kb.nextLine();
return choice;
}
// add a comic to the collection.
// (currently only inputs a hardcoded test comic.)
private static void addComic()
{
System.out.println("Adding comic to collection...");
ComicBook temp = new ComicBook ("Test Comic",
1,
"Test Writer",
"Test Artist",
"Test Letterer",
"Test Colorist");
collection.addComic(temp);
}
// deletes a comic from the collection
private static void deleteComic()
{
System.out.println("Deleting comic from collection...");
}
// print the collection as a list
private static void printCollectionList()
{
collection.printCollectionList();
}
// search the collection
private static void searchCollection()
{
System.out.println("Searching collection...");
Page 8
}
// read input from an external file
private static void readFromFile()
{
System.out.println("Reading from file...");
System.out.print("Enter input filename => ");
String filename = kb.nextLine();
collection.readFromFile(filename);
}
// write output to an external file
private static void writeToFile()
{
System.out.println("Writing to a file...");
if (collection!=null)
{
System.out.print("What file should "+collection.getName()+" be saved to? ");
String fileName = kb.nextLine();
collection.writeToFile(fileName);
}
}
// exit the program
private static void quitProgram()
{
System.out.println("(Quitting program)");
// <ask to save data in file>
}
}
comics.java
This can be used as a test input file. Any output file should also look similar.
4 null
Great Lakes Avengers,3,Zac Gorman,Will Robson,null,null
Squadron Supreme,14,null,null,null,null
Jessica Jones,3,Brian Michael Bendis,Michael Gaydos,Cory Petit,Matt Hollingsworth
Avengers,3,Mark Waid,Mike Del Mundo,null,null
Page 9
Assignment 7 Grading Rubric
CS102
Name: ____________________________
Tom Jensen
Dean Zeller
General Requirements
______ Program runs correctly
No credit if code does not run, for any reason
______ Block documentation
-1 to -2 for incorrect description or entries
-3 if omitted altogether
______ Inline documentation
-1 to -3 for incomplete or missing inline documentation
______ Coding style
Descriptive variable names
Proper indentation and line spacing
Assignment Requirements
______ Menu-driven interface incorporates all functionality
______ Add and Remove methods work appropriately
______ File IO works correctly
______ Search method displays target items
______ Three sorting commands given in interface (but not yet implemented)
______ Print collection as a table
______ Ask to save data before quitting program
______ Test data files (2)
Extra Credit
______ Early submission (+2)
______ Additional features
Page 10