Advertisement
Guest User

RentalApplication

a guest
Nov 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class RentalApplication
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         int option;
  7.         String contractNum;
  8.         int minutes;
  9.         final int QUIT = 9;
  10.         RentalAppt[] rentals = new RentalAppt[2]; //change back to 8 from 2
  11.         int x;
  12.  
  13.         for(x = 0; x < rentals.length; ++x)
  14.         {
  15.             contractNum = getContractNumber();
  16.             minutes = getMinutes();
  17.             rentals[x] = new RentalAppt(contractNum, minutes);
  18.             rentals[x].setContactPhone(getPhone());
  19.             rentals[x].setEquipType(getType());
  20.         }
  21.         System.out.println("\n\nNow display the rentals");
  22.         option = getOption(QUIT);
  23.         while(option != QUIT)
  24.         {
  25.             if(option == 1)
  26.                 sortByContractNumber(rentals);
  27.             else
  28.                 if(option == 2)
  29.                     sortByPrice(rentals);
  30.                 else
  31.                     if(option == 3)
  32.                         sortByType(rentals);
  33.                     else
  34.                         System.out.print("Invalid entry - please reenter.");
  35.             option = getOption(QUIT);
  36.         }
  37.  
  38.     }
  39.     public static int getOption(final int QUIT)
  40.     {
  41.         Scanner input = new Scanner(System.in);
  42.         int option;
  43.         System.out.println("\nPlease enter an option");
  44.         System.out.print("   1 - by contract number\n   2 - by price\n" +
  45.                 "   3 - by equipment type\n   " + QUIT + " - to quit           >> ");
  46.         option = input.nextInt();
  47.         input.nextLine();
  48.         return option;
  49.     }
  50.     public static void sortByContractNumber(RentalAppt[] array)
  51.     {
  52.         int a,b;
  53.         RentalAppt temp;
  54.         String stringB, stringBPlus;
  55.         int highSubscript = array.length  - 1;
  56.  
  57.         for(a = 0; a < highSubscript; ++a)
  58.  
  59.             for(b = 0; b < highSubscript; ++b)
  60.             {  
  61.                 stringB = array[b].getContractNumber();
  62.                 stringBPlus = array[b + 1].getContractNumber();
  63.  
  64.                 if(stringB.compareTo(stringBPlus) > 0)
  65.                 {
  66.                     temp = array[b];
  67.                     array[b] = array[b + 1];
  68.                     array[b + 1] = temp;
  69.                 }
  70.             }
  71.         for(a = 0; a < array.length; ++a)
  72.         {
  73.             displayDetails(array[a]);
  74.         }
  75.     }
  76.     public static void sortByPrice(RentalAppt[] array)
  77.     {
  78.         int a,b;
  79.         RentalAppt temp;
  80.         int highSubscript = array.length  - 1;
  81.  
  82.         for(a = 0; a < highSubscript; ++a)
  83.  
  84.             for(b = 0; b < highSubscript; ++b)
  85.             {  
  86.                 if(array[b].getPrice() > array[b + 1].getPrice())
  87.                 {
  88.                     temp = array[b];
  89.                     array[b] = array[b + 1];
  90.                     array[b + 1] = temp;
  91.                 }
  92.             }
  93.         for(a = 0; a < array.length; ++a)
  94.         {
  95.             displayDetails(array[a]);
  96.         }
  97.     }
  98.     public static void sortByType(RentalAppt[] array)
  99.     {
  100.         int a,b;
  101.         RentalAppt temp;
  102.         int highSubscript = array.length  - 1;
  103.  
  104.         for(a = 0; a < highSubscript; ++a)
  105.  
  106.             for(b = 0; b < highSubscript; ++b)
  107.             {  
  108.                 if(array[b].getEquipType() > array[b + 1].getEquipType())
  109.                 {
  110.                     temp = array[b];
  111.                     array[b] = array[b + 1];
  112.                     array[b + 1] = temp;
  113.                 }
  114.             }
  115.         for(a = 0; a < array.length; ++a)
  116.         {
  117.             displayDetails(array[a]);
  118.         }
  119.     }
  120.     public static String getContractNumber()
  121.     {
  122.         String num;
  123.         Scanner input = new Scanner(System.in);
  124.         System.out.print("\nEnter contract number >> ");
  125.         num = input.nextLine();
  126.         return num;
  127.     }  
  128.     public static int getMinutes()
  129.     {
  130.         int minutes;
  131.         final int LOW_MIN = 60;
  132.         final int HIGH_MIN = 7200;
  133.         Scanner input = new Scanner(System.in);
  134.         System.out.print("Enter minutes >> ");
  135.         minutes = input.nextInt();
  136.         while(minutes < LOW_MIN || minutes > HIGH_MIN)
  137.         {
  138.             System.out.println("Time must be between " + LOW_MIN +
  139.                     " minutes and " + HIGH_MIN + " minutes");
  140.             System.out.print("Please reenter minutes >> ");
  141.             minutes = input.nextInt();
  142.         }
  143.         return minutes;
  144.     }
  145.     public static int getType()
  146.     {
  147.         int eType;
  148.         Scanner input = new Scanner(System.in);
  149.         System.out.println("Equipment types:");
  150.         for(int x = 0; x < RentalAppt.EQUIP_TYPES.length; ++x)
  151.             System.out.println("   " + x + "  " + RentalAppt.EQUIP_TYPES[x]);    
  152.         System.out.print("Enter equipment type >> ");
  153.         eType = input.nextInt();
  154.         return eType;
  155.     }
  156.     public static void displayDetails(RentalAppt r)
  157.     {
  158.         System.out.println("\nContract #" + r.getContractNumber());
  159.         System.out.println("For a rental of " + r.getHours() +
  160.                 " hours and " + r.getExtraMinutes() +
  161.                 " minutes,\n    at a rate of $" + r.HOUR_RATE +
  162.                 " per hour and $1 per minute,\n    the price is $" + r.getPrice());
  163.         System.out.println("Contact phone number is: " + r.getContactPhone());
  164.         System.out.println("Equipment rented is type #" + r.getEquipType() +
  165.                 "  " + r.getEquipTypeString());
  166.     
  167.         }   
  168.     public static void displayMotto()
  169.     {
  170.         System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS");
  171.         System.out.println("S                                    S");
  172.         System.out.println("S  Sammy's makes it fun in the sun.  S");
  173.         System.out.println("S                                    S");
  174.         System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS");
  175.     }
  176.     public static RentalAppt getLongerRental(RentalAppt r1, RentalAppt r2)
  177.     {
  178.         RentalAppt longer = new RentalAppt(null, 0);
  179.         int minutes1;
  180.         int minutes2;
  181.         minutes1 = r1.getHours() * RentalAppt.MINUTES_IN_HOUR + r1.getExtraMinutes();
  182.         minutes2 = r2.getHours() * RentalAppt.MINUTES_IN_HOUR + r2.getExtraMinutes();
  183.         if(minutes1 >= minutes2)
  184.             longer = r1;
  185.         else
  186.             longer = r2;           
  187.         return longer;
  188.     }
  189.     public static String getPhone()
  190.     {
  191.         String phone;
  192.         Scanner input = new Scanner(System.in);
  193.         System.out.print("Enter contact phone number >> ");
  194.         phone = input.nextLine();
  195.         return phone;
  196.     } 
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement