Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*** 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.
- *****************************************************/
- public class OverdueBooks
- {
- public OverdueBooks()
- {
- 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);
- System.out.println("Today = " + today);
- System.out.println("Due = " + dueDate);
- System.out.println("Days overdue = " + days);
- if(days<=0)
- { System.out.println("No fine"); }
- else if(days<=6)
- { System.out.println("Fine = " + (days*0.25)); }
- else if(days<=13)
- { System.out.println("Fine = " + 1.50); }
- else if(days<=20)
- { System.out.println("Fine = " + 3.00); }
- else if(days<=27)
- { System.out.println("Fine = " + 4.50); }
- else
- { System.out.println("Over 4 weeks late - talk to librarian"); }
- another = input("Press [Enter] for another book");
- }
- }
- int daysLate(String today,String dueDate)
- {
- int td = Integer.parseInt(today.substring(0,2)); // today's day
- int tm = Integer.parseInt(today.substring(3,5)); // today's month
- int dd = Integer.parseInt(dueDate.substring(0,2)); // dueDate's day
- int dm = Integer.parseInt(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 static void main(String[] args)
- { new OverdueBooks(); }
- public String input(String prompt)
- { return javax.swing.JOptionPane.showInputDialog(null,prompt); }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement