Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need Help Displaying Double

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Need Help Displaying Double

    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;
        }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need Help Displaying Double

    As I sit here chatting with my friend... Man that is just so much code to look over... Could you post a smaller sample

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Need Help Displaying Double

    Quote Originally Posted by C++kingKnowledge View Post
    Hey i need a few pointers
    You could put print statements at strategic points among the calculations functions.

    You gave some excellent italicized bold-face advice. Did you follow it yourself?
    Quote Originally Posted by C++kingKnowledge
    ...Focus on the computeCharge() method...
    So...

    Start here: Show date values used in calculations...
        public double computeCharge(GregorianCalendar returnDate){
            double charge = 0.0;
     
            long dueTime = // some stuff
            long returnTime = // some stuff
     
            long diff = //some stuff
            // For debugging purposes: print returnTime and dueTime and the difference...
    .
    .
    .

    If the result looks OK here, then instrument further calculations.


    Cheers!

    Z
    Last edited by Zaphod_b; August 15th, 2012 at 02:36 PM.

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Need Help Displaying Double

    okay i see wut u mean i changed the return time variable and got it to compile... it works now!!! thanks, why didnt i see that before???

Similar Threads

  1. Displaying the other
    By Twoacross in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 08:58 AM
  2. Not displaying
    By toksiks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:32 AM
  3. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  4. Not Displaying what I need
    By rob3097 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2010, 10:31 PM
  5. Displaying things in gui
    By KrisTheSavage in forum AWT / Java Swing
    Replies: 2
    Last Post: March 29th, 2010, 12:21 PM