Hey i need a few pointers on how to get my total charge to display in my OverdueChecker program I'm out of ideas... The app simulates an over due book that takes an input price from the user and based on the difference between the return date and due date it is supposed to calculate the total charge. I got all my other methods right but this one is makin my head itch.... Thanks in advance for the insight
This is the main class...
/* * Program Development: Library Book Overdue Checker * File: OverdueChecker.java */ import java.io.*; import java.text.*; import java.util.*; import javax.swing.*; class OverdueCheckerV2 { //------------------ //DATE MEMBERS //------------------ private static final String DATE_SEPARATOR = "/"; private BookTracker bookTracker; //------------------ //CONSTRUCTORS //------------------ public OverdueCheckerV2(){ bookTracker = new BookTracker(); } public static void main(String[] args) { OverdueCheckerV2 checker = new OverdueCheckerV2(); checker.start(); checker.thanks(); } //------------------ //PUBLIC METHODS //------------------ public void start(){ GregorianCalendar returnDate; String table; int runAgn; double charge; inputBooks(); table = bookTracker.getList(); System.out.println("\t\t\t Charge Per Day\t Max Charge Due Date"); System.out.println("\t\t\t --------------\t ---------- ---------"); System.out.println(table); System.out.println("Now checking the over due charges...\n"); //read return date returnDate = readDate("Return Date (mm/dd/yy):"); charge = bookTracker.getCharge(returnDate); displayTotalCharge(charge); runAgn = JOptionPane.showConfirmDialog(null, "Run Again?", "Run Again", JOptionPane.YES_NO_OPTION); if(runAgn == JOptionPane.YES_OPTION){ start(); } } public void thanks(){ System.out.println("\n*** Thank you for using Library Overdue " + "Checker ***\n"); } //------------------ //PRIVATE METHODS //------------------ private LibraryBook createBook(String title, double chargePerDay, double maxCharge, GregorianCalendar dueDate){ if(dueDate == null){ dueDate = new GregorianCalendar(); //Set today as due date } LibraryBook book = new LibraryBook(dueDate); if(title.length() > 0){ book.setTitle(title); } if(chargePerDay > 0.0){ book.setChargePerDay(chargePerDay); } if(maxCharge > 0.0){ book.setMaximumCharge(maxCharge); } return book; } private void displayTotalCharge(double charge){ System.out.format("\nTOTAL CHARGE:\t $%8.2f\n\n", charge); System.out.println("--------------------------------------"); } private void inputBooks(){ double chargePerDay, maxCharge; String title; GregorianCalendar dueDate; LibraryBook book; //Keeps on reading input from a console until stopped by the end user while(isContinue()){ title = readString("Title: "); chargePerDay = readDouble("Charge Per Day: "); maxCharge = readDouble("Maximum Charge: "); dueDate = readDate("Due Date (mm/dd/yy): "); book = createBook(title, chargePerDay, maxCharge, dueDate); bookTracker.add(book); } } private boolean isContinue(){ boolean selection = true; int reply = JOptionPane.showConfirmDialog(null, "More books to " + "enter?", "Book Entry", JOptionPane.YES_NO_OPTION); return selection = (reply == JOptionPane.YES_OPTION); } private double readDouble(String prompt){ double result; String input = JOptionPane.showInputDialog(null, prompt); result = Double.parseDouble(input); return result; } private GregorianCalendar readDate(String prompt){ GregorianCalendar cal; String yearStr, monthStr, dayStr, line; int year, month, day, sep1, sep2; line = JOptionPane.showInputDialog(null, prompt); if(line.length() == 0){ cal = null; } else{ sep1 = line.indexOf(DATE_SEPARATOR); sep2 = line.lastIndexOf(DATE_SEPARATOR); monthStr = line.substring(0, sep1); dayStr = line.substring(sep1 + 1, sep2); yearStr = line.substring(sep2 + 1, line.length()); cal = new GregorianCalendar(Integer.parseInt(yearStr), Integer.parseInt(monthStr) - 1, Integer.parseInt(dayStr)); } return cal; } private String readString(String prompt){ String input = JOptionPane.showInputDialog(null, prompt); return input; } }
This is the class that creates library book objects... Focus on the computeCharge() method because it gets called in BookTracker
import java.util.*; import java.text.*; class LibraryBook { //------------------ //DATA MEMBERS //------------------ private static final double CHARGE_PER_DAY = 0.50; private static final double MAX_CHARGE = 50.00; private static final double MILLISEC_TO_DAY = 1.0 / 1000/ 60 / 60 / 24; private static final String DEFAULT_TITLE = "Title Unknown"; private GregorianCalendar dueDate; private String title; private double chargePerDay; private double maximumCharge; //------------------ //CONSTRUCTORS //------------------ public LibraryBook(GregorianCalendar dueDate){ this(dueDate, CHARGE_PER_DAY); } public LibraryBook(GregorianCalendar dueDate, double chargePerDay){ this(dueDate, chargePerDay, MAX_CHARGE); } public LibraryBook(GregorianCalendar dueDate, double chargePerDay, double maximumCharge){ this(dueDate, chargePerDay, maximumCharge, DEFAULT_TITLE); } public LibraryBook(GregorianCalendar dueDate, double chargePerDay, double maximumCharge, String title){ setDueDate(dueDate); setChargePerDay(chargePerDay); setMaximumCharge(maximumCharge); setTitle(title); } //------------------ //PUBLIC METHODS //------------------ public double getChargePerDay(){ return chargePerDay; } public GregorianCalendar getDueDate(){ return dueDate; } public double getMaxCharge(){ return maximumCharge; } public String getTitle(){ return title; } public void setChargePerDay(double charge){ chargePerDay = charge; } public void setDueDate(GregorianCalendar date){ dueDate = date; } public void setMaximumCharge(double charge){ maximumCharge = charge; } public void setTitle(String title){ this.title = title; } public double computeCharge(GregorianCalendar returnDate){ double charge = 0.0; long dueTime = dueDate.getTimeInMillis(); long returnTime = dueDate.getTimeInMillis(); long diff = returnTime - dueTime; if(diff > 0){ charge = chargePerDay * diff * MILLISEC_TO_DAY; if(charge > maximumCharge){ charge = maximumCharge; } } return charge; } public String toString(){ String tab = "\t"; DecimalFormat df = new DecimalFormat("0.00"); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy"); return String.format("%-30s $%5.2f $%7.2f %4$tm/%4$td/%4$ty", getTitle(), getChargePerDay(), getMaxCharge(), dueDate.getTime()); } }
This is the class to keep track of the books created and where the total charge is to be calculated. Focus on the getCharge() methods and the totalCharge() method
import java.util.*; import java.text.*; class BookTracker { //----------------- //DATA MEMBERS //----------------- /**Error condition for asking total charge when no book is added */ public static final int ERROR = -1; /**Maintains a list of library books*/ private List books; //----------------- //CONSTRUCTORS //----------------- public BookTracker(){ books = new LinkedList(); } //----------------- //PUBLIC METHODS //----------------- /** * Adds the book to the list * * @param book the book to add to the list */ public void add(LibraryBook book){ books.add(book); } /** * Returns the total charge of the overdue books. Return * date is set to today * * @return the total charge. ERROR if no books are entered */ public double getCharge(){ return getCharge(new GregorianCalendar()); //sets today as due date } /** * Returns the total charge of the overdue books * * @param returnDate date the books are returned * * @return the total charge. ERROR if no books are entered */ public double getCharge(GregorianCalendar returnDate){ if(books.isEmpty()){ return ERROR; } else{ return totalCharge(returnDate); } } /**Returns a list of books with its data * * @return the summary book list */ public String getList(){ StringBuffer result = new StringBuffer(""); String lineSeparator = System.getProperty("line.separator"); Iterator itr = books.iterator(); while(itr.hasNext()){ LibraryBook book = (LibraryBook) itr.next(); result.append(book.toString() + lineSeparator); } return result.toString(); } //------------------ //PRIVATE METHODS //------------------ /** * Computes the total charge of over due for the books * in the list * * @param returnDate date the books are returned * * @return the total charge of overdue books */ private double totalCharge(GregorianCalendar returnDate){ double totalCharge = 0.0; Iterator itr = books.iterator(); while(itr.hasNext()){ LibraryBook book = (LibraryBook) itr.next(); totalCharge += book.computeCharge(returnDate); } return totalCharge; } }