Advertisement
mrScarlett

Library Overdue

Feb 25th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. /*** Overdue Library Books **************************
  2. Sheila works in the school library.  She needs a quick way
  3. to calcuate a fine for overdue books, based on this table:
  4.    0.25 per day, up to 6 days.
  5.    1.50 for 7-13 days
  6.    3.00 for 14-20 days
  7.    etc, 1.50 per week for each week
  8. The logic is not difficult, but it's much simpler if she
  9. can type in the original due date and the current date,
  10. and the computer tells the appropriate fine.
  11. *****************************************************/
  12. public class OverdueBooks
  13. {
  14.   public OverdueBooks()
  15.   {
  16.     String today = input("Today's date (dd.mm)");
  17.     String another = "";
  18.     while(another.equals(""))
  19.     {
  20.       String dueDate = input("Due date (dd.mm)");
  21.       int days = daysLate(today, dueDate);
  22.       System.out.println("Today = " + today);
  23.       System.out.println("Due = " + dueDate);
  24.       System.out.println("Days overdue = " + days);
  25.      
  26.       if(days<=0)
  27.       { System.out.println("No fine"); }
  28.       else if(days<=6)
  29.       { System.out.println("Fine = " + (days*0.25)); }
  30.       else if(days<=13)
  31.       { System.out.println("Fine = " + 1.50); }
  32.       else if(days<=20)
  33.       { System.out.println("Fine = " + 3.00); }
  34.       else if(days<=27)
  35.       { System.out.println("Fine = " + 4.50); }
  36.       else
  37.       { System.out.println("Over 4 weeks late - talk to librarian"); }
  38.       another = input("Press [Enter] for another book");
  39.     }
  40.   }
  41.  
  42.   int daysLate(String today,String dueDate)
  43.   {
  44.     int td = Integer.parseInt(today.substring(0,2));    // today's day
  45.     int tm = Integer.parseInt(today.substring(3,5));    // today's month
  46.    
  47.     int dd = Integer.parseInt(dueDate.substring(0,2));  // dueDate's day
  48.     int dm = Integer.parseInt(dueDate.substring(3,5));  // dueDate's month
  49.    
  50.     if(tm == dm)
  51.     {
  52.       if(dd >= td)                      
  53.       {  return -1; }                     // not overdue
  54.       else
  55.       {  return td-dd; }                  // days overdue
  56.     }
  57.     else if (dm > tm)
  58.     {  return -1; }                        // not overdue
  59.     else
  60.     {                                      // tm > dm, due last month
  61.       return (tm-dm)*30 + (td-dd);        // count 30 days per month    
  62.     }
  63.   }
  64.  
  65.   public static void main(String[] args)
  66.   {  new OverdueBooks();  }
  67.  
  68.   public String input(String prompt)
  69.   { return javax.swing.JOptionPane.showInputDialog(null,prompt); }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement