Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package TicketChecker;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- public class TicketChecker {
- static Scanner userInput = new Scanner(System.in); //Scanner for user input
- static ArrayList<Tickets> TicketList = new ArrayList<>(); //declaring and initializing the arrayList
- static boolean exit; //whether the user wants to exit or not
- static String type; //the type of ticket to be distributed
- static double fine; // the fine or cost of the ticket distributed
- private static void addTickets()
- {
- //declaration of variables
- String offence, Fname, Lname; //the offence, first name and last name of the person
- int day, month, year; //the day, month and year when the ticket is distributed
- System.out.println("\nPlease Enter First Name Of The Driver: ");
- Fname = userInput.nextLine();
- userInput.nextLine();
- System.out.println("\nPlease Enter Last Name Of The Driver: ");
- Lname = userInput.nextLine();
- System.out.println("\nPlease Enter The Type Of The Ticket: ");
- chooseTickType();
- System.out.println("\nPlease Enter The Offence Pertaining To The Ticket: ");
- offence = userInput.nextLine();
- userInput.nextLine();
- System.out.println("\nPlease Enter the Day Of The Ticket Distributed: ");
- day = userInput.nextInt();
- System.out.println("\nPlease Enter the Month Of The Ticket Distributed: ");
- month = userInput.nextInt();
- System.out.println("\nPlease Enter the Year Of The Ticket Distributed: ");
- year = userInput.nextInt();
- TicketList.add(new Tickets(Fname, Lname, type, offence, day, month, year, fine));
- }
- //prints the options available in menu
- private static void printMenu()
- {
- System.out.println("------------------------------------------------------------------------\n");
- System.out.println("\t\t\tWelcome To The Menu");
- System.out.println("\n------------------------------------------------------------------------");
- System.out.println("\t\t1. Add a Ticket Object\n");
- System.out.println("\t\t2. Display Tickets\n");
- System.out.println("\t\t3. Remove Ticket From The List\n");
- System.out.println("\t\t4. Quit\n");
- System.out.println("------------------------------------------------------------------------");
- }
- //perform the actions according to the choice the officer chose from the menu
- private static void performMenu(int choice)
- {
- switch (choice)
- {
- case 1:
- //calls the addTickets() method
- addTickets();
- break;
- case 2:
- //displays the tickets according how the officer wants to sort them by
- System.out.println("How do you want the tickets to be sorted?");
- System.out.println("1. Types of tickets");
- System.out.println("2. Last name");
- System.out.println("3. First name");
- choice = userInput.nextInt();
- switch(choice) {
- case 1:
- //sorts the tickets by the types of the tickets
- Collections.sort(TicketList, Tickets.TicketType);
- System.out.println("\nTickets Being Sorted By The Type: ");
- for(Tickets type: TicketList) {
- System.out.println(type.toString());
- System.out.println("\n-------------------------------------------------------------------------------------------------\n");
- }
- break;
- case 2:
- //sorts the tickets by the first names of the people
- Collections.sort(TicketList, Tickets.TicketLname);
- System.out.println("\nTickets Being Sorted By The Last Name: ");
- for(Tickets Lname: TicketList) {
- System.out.println(Lname.toString());
- System.out.println("\n-------------------------------------------------------------------------------------------------\n");
- }
- break;
- case 3:
- //sorts the tickets by the first name of the people
- Collections.sort(TicketList, Tickets.TicketFname);
- System.out.println("\nTickets Being Sorted By The First Name: ");
- for(Tickets Fname: TicketList) {
- System.out.println(Fname.toString());
- System.out.println("\n-------------------------------------------------------------------------------------------------\n");
- }
- break;
- }
- //updates the ticket cost if the person does not pay within 10 days
- System.out.println("If There Are Tickets That Is Overdue (Time Limit: 10 Days), ");
- System.out.println("Enter 1 For 'YES' And 2 For 'NO'");
- choice = userInput.nextInt();
- switch(choice) {
- case 1:
- updateFine();
- break;
- case 2:
- System.out.println("Thank You For Your Service\n\n");
- break;
- default:
- System.out.println("Choose Either Option 1 Or Option 2");
- runMenu();
- }
- break;
- case 3:
- //calls the removeTickets() method
- removeTickets();
- break;
- case 4:
- //calls the terminating method
- System.out.println("The Program Has Been Terminated\n");
- System.exit(0);
- break;
- default:
- System.out.println("An Error Has Occured\n");
- }
- }
- //allow the menu to run
- private static void runMenu()
- {
- while (!exit)
- {
- printMenu();
- int choice = userInput.nextInt();
- performMenu(choice);
- }
- }
- //removes a ticket from the array list
- private static void removeTickets()
- {
- int position;
- System.out.println("Enter The Positon of The Person You Desire To Remove: ");
- System.out.println("");
- position = userInput.nextInt();
- TicketList.remove(position -1);
- }
- //adds $50.00 to the cost of the ticket
- private static void updateFine() {
- System.out.println("Enter the position of the person who did not pay within 10 days: ");
- int position = (userInput.nextInt()) - 1;
- Tickets newTicket = TicketList.get(position);
- newTicket.updateFine();
- TicketList.set(position, newTicket);
- }
- //allow the user to choose the types of tickets to be distributed
- private static void chooseTickType()
- {
- //asks the officer to choose the type of ticket to be distributed
- System.out.println("1. Speeding Offence\n");
- System.out.println("2. Parking Offense\n");
- System.out.println("3. Driving Without Seatbelt\n");
- System.out.print("Choose The Type of Ticket To Be Distributed: \n");
- int choice = userInput.nextInt();
- switch (choice)
- {
- case 1:
- speeding();
- break;
- case 2:
- parking();
- break;
- case 3:
- seatbelt();
- break;
- default:
- System.out.println("This Option Does Not Exist\n");
- }
- }
- //the types of tickets are as follow with the fine
- private static void speeding() {
- type= "Speeding Offence\n";
- fine = 250.00;
- }
- private static void parking() {
- type = "Parking Offence\n";
- fine = 100.00;
- }
- private static void seatbelt() {
- type = "Driving Without Seatbelt\n";
- fine = 150.00;
- }
- public static void main(String[] args)
- {
- //objects created already for the purpose of displaying
- TicketList.add (new Tickets("Bob","Real", "Speeding Offence", "Driving Over Speed Limit", 24, 5, 2017, 150.0));
- TicketList.add (new Tickets("William","Hearse", "Parking Offence", "Parking In Reserved Zone", 13, 2, 2018, 100.0));
- //calls the runMenu() method which runs the whole program
- runMenu();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement