Tuesday, November 10, 2009

Homework 7 Solution

Below is my solution for the Field Notebook homework:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.Scanner;


public class Notebook {

 private Reading[] reading;

 private int nextIndex = 0;

 /** Maximum number of readings. */
 private static final int SIZE = 100;

 public Notebook(){
  reading = new Reading[SIZE]; 
 }

 /**
  * Creates a notebook by reading its contents from the given file.
  * @param filename
  * @throws IOException
  */
 public Notebook(String filename) throws IOException {
  try {
   ObjectInputStream input = new ObjectInputStream (new FileInputStream(filename));
   nextIndex = input.readInt();
   reading = (Reading[])input.readObject();
   input.close();
  }  catch (ClassNotFoundException e) {
   System.out.println("ERROR: File corrupted.");
   e.printStackTrace();
  }
 }

 /**
  * Adds a reading to the notebook
  * @param species name of the species
  * @param count how many you saw
  * @param comment 
  * @throws Exception if the notebook gets too big
  */
 public void addReading(String species, int count, String comment) throws Exception{
  //The right thing to do is use a data structure that expands as needed, but that's CSCE 146
  //  so we just crash.
  try {
   if (nextIndex >= SIZE) 
    throw new Exception("Too many readings!");
   reading[nextIndex] = new Reading(new Date(), species, count, comment);
   nextIndex++;
  }
  catch (NegativeCountException e) {
   System.out.println("Sorry, no negative animals allowed.");
  }
 }

 public String toString(){
  String result = "";
  for(int i=0; i< nextIndex; i++){
   result += reading[i].toString() + "\n";
  }
  return result;
 }

 /**
  * Write this notebook out to a file. It can be read back using the constructor.
  * @param filename name of the file
  * 
  */
 public void writeToFile(String filename) {
  try {
   ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(filename));
   out.writeInt(nextIndex);
   out.writeObject(reading);
   out.close();
  } catch (FileNotFoundException e) {
   System.out.println("ERROR: No such file. Did not write.");
  } catch (IOException e) {
   System.out.println("ERROR: IO. Did not write to file.");
  }
 }

 public static void main(String[] args) throws Exception {

  Scanner keyboard = new Scanner(System.in);
  Notebook notebook = new Notebook();


  while (true){
   System.out.println("Command list: \n");
   System.out.println("r - enter new Reading");
   System.out.println("l - List all readings");
   System.out.println("o - Open file with readings");
   System.out.println("w - Write readings to a file");
   System.out.println("q - Quit");
   System.out.print("\nEnter command:");
   String command = keyboard.nextLine();

   if (command.equalsIgnoreCase("r")) {
    System.out.print("Species name:");
    String species = keyboard.nextLine();
    System.out.print("Count:");
    int count = Integer.parseInt(keyboard.nextLine());
    System.out.print("Comment:");
    String comment = keyboard.nextLine();
    notebook.addReading(species, count, comment);
   }
   else if (command.equalsIgnoreCase("l")) {
    System.out.println(notebook);   
   }
   else if (command.equalsIgnoreCase("o")) {
    System.out.print("Enter name of file to read:");
    String filename = keyboard.nextLine();
    try {
     notebook = new Notebook(filename);
    }
    catch (FileNotFoundException e) {
     System.out.println("ERROR: File " + filename +  " does not exists.");
    }
   }
   else if (command.equalsIgnoreCase("w")) {
    System.out.print("Enter name of file to write to:");
    String filename = keyboard.nextLine();
    notebook.writeToFile(filename);    
   }
   else if (command.equalsIgnoreCase("q")) {
    System.out.println("Bye bye.");
    System.exit(0);
   }
  }


 }

}

import java.io.Serializable;
import java.util.Date;

public class Reading implements Serializable {

 private Date date;

 private String species;

 private int count;

 private String comment;

 public Reading(Date date, String species, int count, String comment) throws NegativeCountException {
  super();
  if (count < 0)
   throw new NegativeCountException("There are no negative animals you dummy.");
  this.date = date;
  this.species = species;
  this.count = count;
  this.comment = comment;
 }

 public String toString() {
  return date + "\t" + species + "\t" + count + "\t" + comment;
 }

}


public class NegativeCountException extends Exception {

 public NegativeCountException(String m) {
  super(m);
 }

}






No comments: