Advertisement
Guest User

TicketChecker

a guest
May 22nd, 2018
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.55 KB | None | 0 0
  1. package TicketChecker;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Scanner;
  6.  
  7. public class TicketChecker {
  8.  
  9.     static Scanner userInput = new Scanner(System.in);              //Scanner for user input
  10.     static ArrayList<Tickets> TicketList = new ArrayList<>();       //declaring and initializing the arrayList
  11.     static boolean exit;                                            //whether the user wants to exit or not
  12.     static String type;                                             //the type of ticket to be distributed
  13.     static double fine;                                             // the fine or cost of the ticket distributed
  14.    
  15.     private static void addTickets()
  16.     {
  17.         //declaration of variables
  18.         String offence, Fname, Lname;                               //the offence, first name and last name of the person
  19.         int day, month, year;                                       //the day, month and year when the ticket is distributed
  20.      
  21.  
  22.         System.out.println("\nPlease Enter First Name Of The Driver: ");
  23.         Fname = userInput.nextLine();
  24.         userInput.nextLine();
  25.        
  26.         System.out.println("\nPlease Enter Last Name Of The Driver: ");
  27.         Lname = userInput.nextLine();
  28.  
  29.         System.out.println("\nPlease Enter The Type Of The Ticket: ");
  30.         chooseTickType();
  31.  
  32.         System.out.println("\nPlease Enter The Offence Pertaining To The Ticket: ");
  33.         offence = userInput.nextLine();
  34.         userInput.nextLine();
  35.  
  36.         System.out.println("\nPlease Enter the Day Of The Ticket Distributed: ");
  37.         day = userInput.nextInt();
  38.        
  39.         System.out.println("\nPlease Enter the Month Of The Ticket Distributed: ");
  40.         month = userInput.nextInt();
  41.        
  42.         System.out.println("\nPlease Enter the Year Of The Ticket Distributed: ");
  43.         year = userInput.nextInt();
  44.    
  45.         TicketList.add(new Tickets(Fname, Lname, type, offence, day, month, year, fine));
  46.     }
  47.    
  48.     //prints the options available in menu
  49.     private static void printMenu()
  50.     {
  51.         System.out.println("------------------------------------------------------------------------\n");
  52.         System.out.println("\t\t\tWelcome To The Menu");
  53.         System.out.println("\n------------------------------------------------------------------------");
  54.         System.out.println("\t\t1. Add a Ticket Object\n");
  55.         System.out.println("\t\t2. Display Tickets\n");
  56.         System.out.println("\t\t3. Remove Ticket From The List\n");
  57.         System.out.println("\t\t4. Quit\n");
  58.         System.out.println("------------------------------------------------------------------------");
  59.     }
  60.    
  61.     //perform the actions according to the choice the officer chose from the menu
  62.     private static void performMenu(int choice)
  63.     {
  64.         switch (choice)
  65.         {
  66.             case 1:
  67.                 //calls the addTickets() method
  68.                 addTickets();
  69.                 break;
  70.             case 2:
  71.                 //displays the tickets according how the officer wants to sort them by
  72.                 System.out.println("How do you want the tickets to be sorted?");
  73.                 System.out.println("1. Types of tickets");
  74.                 System.out.println("2. Last name");
  75.                 System.out.println("3. First name");
  76.                 choice = userInput.nextInt();
  77.                
  78.                 switch(choice) {
  79.                 case 1:
  80.                     //sorts the tickets by the types of the tickets
  81.                     Collections.sort(TicketList, Tickets.TicketType);
  82.                    
  83.                     System.out.println("\nTickets Being Sorted By The Type: ");
  84.                         for(Tickets type: TicketList) {
  85.                             System.out.println(type.toString());
  86.                             System.out.println("\n-------------------------------------------------------------------------------------------------\n");
  87.                         }
  88.                     break;
  89.                
  90.                 case 2:
  91.                     //sorts the tickets by the first names of the people
  92.                     Collections.sort(TicketList, Tickets.TicketLname);
  93.                    
  94.                     System.out.println("\nTickets Being Sorted By The Last Name: ");
  95.                         for(Tickets Lname: TicketList) {
  96.                             System.out.println(Lname.toString());
  97.                             System.out.println("\n-------------------------------------------------------------------------------------------------\n");
  98.                         }
  99.                     break;
  100.                    
  101.                 case 3:
  102.                     //sorts the tickets by the first name of the people
  103.                     Collections.sort(TicketList, Tickets.TicketFname);
  104.                    
  105.                     System.out.println("\nTickets Being Sorted By The First Name: ");
  106.                         for(Tickets Fname: TicketList) {
  107.                             System.out.println(Fname.toString());
  108.                             System.out.println("\n-------------------------------------------------------------------------------------------------\n");
  109.                         }
  110.                     break;
  111.                 }
  112.                
  113.                 //updates the ticket cost if the person does not pay within 10 days
  114.                 System.out.println("If There Are Tickets That Is Overdue (Time Limit: 10 Days), ");
  115.                 System.out.println("Enter 1 For 'YES' And 2 For 'NO'");
  116.                 choice = userInput.nextInt();
  117.                
  118.                 switch(choice) {
  119.                 case 1:
  120.                     updateFine();
  121.                     break;
  122.                    
  123.                 case 2:
  124.                     System.out.println("Thank You For Your Service\n\n");
  125.                     break;
  126.                    
  127.                 default:
  128.                     System.out.println("Choose Either Option 1 Or Option 2");
  129.                     runMenu();
  130.                 }
  131.                 break;
  132.             case 3:
  133.                 //calls the removeTickets() method
  134.                 removeTickets();
  135.                 break;
  136.  
  137.             case 4:
  138.                 //calls the terminating method
  139.                 System.out.println("The Program Has Been Terminated\n");
  140.                 System.exit(0);
  141.                 break;
  142.             default:
  143.                 System.out.println("An Error Has Occured\n");
  144.         }
  145.     }
  146.    
  147.     //allow the menu to run
  148.     private static void runMenu()
  149.     {
  150.         while (!exit)
  151.         {
  152.           printMenu();
  153.           int choice = userInput.nextInt();
  154.           performMenu(choice);
  155.         }
  156.     }
  157.    
  158.     //removes a ticket from the array list
  159.     private static void removeTickets()
  160.     {
  161.         int position;
  162.  
  163.         System.out.println("Enter The Positon of The Person You Desire To Remove: ");
  164.         System.out.println("");
  165.         position = userInput.nextInt();
  166.  
  167.         TicketList.remove(position -1);
  168.     }
  169.    
  170.     //adds $50.00 to the cost of the ticket
  171.     private static void updateFine() {
  172.         System.out.println("Enter the position of the person who did not pay within 10 days: ");
  173.          int position = (userInput.nextInt()) - 1;
  174.  
  175.         Tickets newTicket = TicketList.get(position);
  176.         newTicket.updateFine();
  177.         TicketList.set(position, newTicket);
  178.     }
  179.    
  180.     //allow the user to choose the types of tickets to be distributed
  181.     private static void chooseTickType()
  182.     {
  183.         //asks the officer to choose the type of ticket to be distributed
  184.         System.out.println("1. Speeding Offence\n");
  185.         System.out.println("2. Parking Offense\n");
  186.         System.out.println("3. Driving Without Seatbelt\n");
  187.        
  188.         System.out.print("Choose The Type of Ticket To Be Distributed: \n");
  189.         int choice = userInput.nextInt();
  190.  
  191.         switch (choice)
  192.         {
  193.             case 1:
  194.                 speeding();
  195.                 break;
  196.             case 2:
  197.                 parking();
  198.                 break;
  199.             case 3:
  200.                 seatbelt();
  201.                 break;
  202.             default:
  203.                 System.out.println("This Option Does Not Exist\n");
  204.         }
  205.     }
  206.    
  207.     //the types of tickets are as follow with the fine
  208.     private static void speeding() {
  209.         type= "Speeding Offence\n";
  210.         fine = 250.00;
  211.     }
  212.    
  213.     private static void parking() {
  214.         type = "Parking Offence\n";
  215.         fine = 100.00;
  216.     }
  217.    
  218.     private static void seatbelt() {
  219.         type = "Driving Without Seatbelt\n";
  220.         fine = 150.00;
  221.     }
  222.    
  223.     public static void main(String[] args)
  224.     {
  225.         //objects created already for the purpose of displaying
  226.         TicketList.add (new Tickets("Bob","Real", "Speeding Offence", "Driving Over Speed Limit", 24, 5, 2017, 150.0));
  227.         TicketList.add (new Tickets("William","Hearse", "Parking Offence", "Parking In Reserved Zone", 13, 2, 2018, 100.0));
  228.        
  229.         //calls the runMenu() method which runs the whole program
  230.         runMenu();
  231.     }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement