/*** Overdue Library
Books ************************** Sheila works in the school library. She needs a quick way to calcuate a fine for overdue books, based on this table: 0.25 per day, up to 6 days. 1.50 for 7-13 days 3.00 for 14-20 days etc, 1.50 per week for each week The logic is not difficult, but it's much simpler if she can type in the original due date and the current date, and the computer tells the appropriate fine. *****************************************************/ void setup() { String today = input("Today's date (dd.mm)"); String another = ""; while(another.equals("")) { String dueDate = input("Due date (dd.mm)"); int days = daysLate(today, dueDate); println("Today = " + today); println("Due = " + dueDate); println("Days overdue = " + days); if(days<=0) { println("No fine"); } else if(days<=6) { println("Fine = " + (days*0.25)); } else if(days<=13) { println("Fine = " + 1.50); } else if(days<=20) { println("Fine = " + 3.00); } else if(days<=27) { println("Fine = " + 4.50); } else { println("Over 4 weeks late - talk to librarian"); } another = input("Press [Enter] for another book"); } } int daysLate(String today,String dueDate) { int td = int(today.substring(0,2)); // today's day int tm = int(today.substring(3,5)); // today's month int dd = int(dueDate.substring(0,2)); // dueDate's day int dm = int(dueDate.substring(3,5)); // dueDate's month if(tm == dm) { if(dd >= td) { return -1; } // not overdue else { return td-dd; } // days overdue } else if (dm > tm) { return -1; } // not overdue else { // tm > dm, due last month return (tm-dm)*30 + (td-dd); // count 30 days per month } } public String input(String prompt) { return javax.swing.JOptionPane.showInputDialog(null,prompt); } |
Today = 15.07 Due = 01.07 Days overdue = 14 Fine = 3.0 |
The program inputs two Strings
that represent dates, in the format dd.mm, e.g. 25.12 for 25 Dec, or 01.01 for New
Year's day.
If the user types a different format, like 12/25 for Dec 25, then
the program will not function correctly. A really good
program
would check whether the date is VALID (in the correct format)
before processing the data, but that part is left as a practice
exercise for the reader.
The program parses each
date String. Parse means to take apart the String into
smaller meaningful pieces. Each String
starts with 2 digits for the day, followed by a period '.', and
finally 2 digits for the month. The substring method extracts
part of a String, for example:
String S = "29.10";
String D = S.substring(0,2); ==> "29"
String M = S.substring(3,5); ==> "10"
Converting from String to
Integer
After extracting the small Strings containing numbers, the
program uses the Processing int(
) function to change these
to actual numbers that can be used in a calculation, to determine
the number over due days, or be used in a comparison
like:
if(D > 31)
{ output("The day part of the date is
too large"); }
if(M < 0 || M > 12)
{ output("The month value is not
valid."); }
Complex Logic
Calculating the overdue charge is not a simple calculation, but
rather uses various categories
to determine the cost.
The correct category is determined by a complex if..else
if..else if..else command.
This sort of complex logic is known as business rules. It has nothing to do with
natural laws or mathematics,
as these rules are determined by a business and can be changed
whenever they wish. In this program, books that are
over 4 weeks overdue require a human consultation with the
librarian to determine the charge (the else part).
Methods
The calculation of DaysLate is done in a separate method. This calculation
could have been done inside
the main setup method,
but writing a separate method makes the program a bit easier to
read. It also
creates a method that could be re-used
in other programs, or called more than once in this
program. Notice that
the method uses return to
send an answer back to the main program.
Programming
Practice