package downey.hw1; import java.io.Serializable; import java.util.ArrayList; /** *The required record to use in Hw1 * *@author Tim Downey *@version 08/29/04 */ public class Hw1Record implements Serializable, Cloneable { // instance variables private double price; private int quantity; private String isbn; private String author; private ArrayList optionList; /** *Default constructor for objects of class Hw1Record */ public Hw1Record() { // call constructor with String argument this(""); } /** *Constructor for objects of class Hw1Record with a String argument * *@param isbn The String isbn for this record */ public Hw1Record(String isbn) { // initialise instance variables quantity = 0; price = 0.0; this.isbn = isbn; author = ""; optionList = new ArrayList(); } /** *setQuantity - set the quantity for this book * *@param quantity the quantity of the book */ public void setQuantity(int quantity) { this.quantity = quantity; } /** *getQuantity - get the quantity for this book * *@return the quantity of the book as an int */ public int getQuantity() { return quantity; } /** *getPrice - set the price for this book * *@param price the price of the book as a double */ public void setPrice(double price) { this.price = price; } /** *getPrice - get the price for this book * *@return the price of the book as a double */ public double getPrice() { return price; } /** *setIsbn - set the isbn for this book * *@param isbn the isbn of the book as a String */ public void setIsbn(String isbn) { this.isbn = isbn; } /** *getIsbn - get the isbn for this book * *@return the isbn of the book as a String */ public String getIsbn() { return isbn; } /** *getAuthor - get the author for this book * *@return the author of the book as a String */ public String getAuthor() { return author; } /** *Set the author for the record * *@param author The author as a String */ public void setAuthor(String author) { this.author = author; } /** *displayTable - display the data on separate lines * */ public void displayTable() { String result = ""; String spacer = "\n"; result += "\nISBN: " + getIsbn() + spacer + "Author: " + getAuthor() + spacer + "Quantity: " + getQuantity() + spacer + "Price: " + getPrice(); result += listOption(spacer) + "\n"; System.out.println(result); } /** *displayRow - display the data on one line * */ public void displayRow() { System.out.println(listRecord("\t")); } /*Helper method to display the record data. *The 'spacer' determines the appearance of the data. *"\n" to get separate lines *"\t" to get one line *Creative tricks can be played, like using "" to get HTML * */ private String listRecord(String spacer) { String result = ""; result += getIsbn() + spacer + getAuthor() + spacer + getQuantity() + spacer + getPrice(); result += listOption(spacer) + "\n"; return result; } /*Helper method to list the options in a comma separated list * */ private String listOption(String firstSpacer) { String result = ""; String option[] = getOption(); String spacer = firstSpacer + "Options: "; if (option == null) return result; for(int i=0; i= 0 && i < optionList.size()) { optionList.remove(i); } } /**Remove all the options from the list * */ public void removeOption() { if (optionList == null || optionList.size() == 0) return; optionList.clear(); } public Object clone() { try { Hw1Record rec = (Hw1Record) super.clone(); rec.optionList = (ArrayList) optionList.clone(); return rec; } catch (CloneNotSupportedException e) { System.out.println("Clone not supported, returning a null Hw1Record\n"); return null; } } }