Advertisement
Rayaank

Rent_A_Car

Nov 11th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.45 KB | None | 0 0
  1. import java.util.*; //Import Scanner Class
  2. class Rent_A_Car{
  3.     public static void main(){
  4.         clr(); //Clearing the screen        
  5.         boolean flag = false, flag2 = true, car_flag = false;
  6.         String enteredModelName, customerName, dateRented;
  7.         int daysRented, renter = 0;
  8.         String renterName[] = new String[15]; //Array for storing names of customers who have rented cars
  9.         String rentedCars[] = new String[15]; //Array for storing Model names of the cars that have been rented
  10.         String rentedDate[] = new String[15]; //Array for storing the Date of when the cars were rented
  11.         int rentedDays[] = new int[15]; // Array for storing the number of days the cars have been rented
  12.         Scanner sc = new Scanner(System.in); //Initializing Scanner class
  13.         Scanner sc_str = new Scanner(System.in); //Initializing Scanner class for Strings
  14.         char again = 'n', mainmenu = 'n'; //Two variables to ask user if they want to retry or go back to the main menu
  15.         String carModels[] = {"Toyota Corolla", "Toyota Yaris", "Toyota Fortuner", "Toyota Land Cruiser", "Nissan Tiida", "Nissan Patrol", "Ford Explorer", "Ford Focus", "Mitsubishi Lancer", "Mitsubishi Pajero", "GMC Yukon XL"}; //Names of cars
  16.         int costPerDay[] = {50, 45, 100, 200, 50, 200, 100, 75, 50, 100, 250}; //Rental cost per day
  17.         String usernames[] = {"Rayaan", "Swarna", "1"}; //Array for storing usernames
  18.         String passwords[] = {"chicken", "computers", "1"}; //Array for storing passwords
  19.         welcome(); //Welcoming user
  20.         prt("\nUsername: ");
  21.         String username = sc.next(); //Entered username by user
  22.         prt("Password: ");
  23.         String password = sc.next(); //Entered password by user
  24.         for(int i = 0; i < 3; i++){ //For loop for index number of arrays because we have 3 usernames
  25.             if (username.equalsIgnoreCase(usernames[i]) && password.equals(passwords[i])){ //Verifying username and password match
  26.                 do{
  27.                     printMenu(usernames[i]); //Printing menu
  28.                     int choice = sc.nextInt(); //Variable for storing user's choice
  29.                     switch(choice){
  30.                         case 1:
  31.                         clr();
  32.                         for(int x = 0; x < costPerDay.length-1; x++){ //Sort the arrays in descending order of cost per day using bubble sort
  33.                             for(int j = x + 1; j < costPerDay.length; j++){
  34.                                 if(costPerDay[x] < costPerDay[j]){
  35.                                     int t = costPerDay[x]; //temporary variable
  36.                                     String s = carModels[x]; //temporary variable
  37.                                     costPerDay[x] = costPerDay[j];
  38.                                     costPerDay[j] = t;
  39.                                     carModels[x] = carModels[j];
  40.                                     carModels[j] = s;
  41.                                 }
  42.                             }
  43.                         }
  44.                         for(int x = 0; x < carModels.length; x++){ //Printing the Cars in descending order of cost per day
  45.                             prtln(carModels[x] + " - " + costPerDay[x] );
  46.                         }
  47.                         prtln("\nReturn to Main Menu? (y/n)"); //Asking user if they want to return to main menu
  48.                         mainmenu = sc.next().charAt(0); //Variable for storing choice
  49.                         break;
  50.                         //End of case 1//
  51.                         case 2:
  52.                         do{
  53.                             clr();
  54.                             prtln("Enter the name of the car");
  55.                             enteredModelName = sc_str.nextLine();
  56.                             for(int x = 0; x < carModels.length; x++){ //Validating model name and printing its details
  57.                                 if (carModels[x].equalsIgnoreCase(enteredModelName)){
  58.                                     car_flag = true;
  59.                                     clr();
  60.                                     prtln("Car name: " + carModels[x]);
  61.                                     prtln("Cost per day: " + costPerDay[x]);
  62.                                     break;
  63.                                 }
  64.                             }
  65.                             if(!car_flag){ //Error message if car wasn't found
  66.                                 prtln("Not Found!");
  67.                             }
  68.                             prtln("\nAgain? (y/n)"); //Asking user if they want to try again
  69.                             again = sc.next().charAt(0);
  70.                         }while(again == 'y' );
  71.                         prtln("\nReturn to Main Menu? (y/n)");
  72.                         mainmenu = sc.next().charAt(0);
  73.                         break;
  74.                         //End of case 2//
  75.                         case 3:
  76.                         do{
  77.                             clr();
  78.                             prtln("Rent A Car");
  79.                             sc = new Scanner(System.in); //Reinitializing scanner to prevent fall through
  80.                             sc_str = new Scanner(System.in);
  81.                             car_flag = false;
  82.                             do{
  83.                                 prt("Model Name: ");
  84.                                 enteredModelName = sc_str.nextLine();
  85.                                 for(int x = 0; x < carModels.length; x++){
  86.                                     if (carModels[x].equalsIgnoreCase(enteredModelName)){ //Validating car model
  87.                                         car_flag = true;
  88.                                         rentedCars[renter] = carModels[x]; //Storing car model in the array
  89.                                         break;
  90.                                     }
  91.                                 }
  92.                                 if(!car_flag){
  93.                                     prtln("Not Found!");
  94.                                 }
  95.                             }while(!car_flag);
  96.                             do{
  97.                                 prt("Customer Name: ");
  98.                                 customerName = sc_str.nextLine();
  99.                                 renterName[renter] = customerName; //Storing name in the array
  100.                             }while(!verifyName(customerName)); //Function for validating name
  101.                             do{
  102.                                 prt("Number of Days Rented: ");
  103.                                 daysRented = sc.nextInt();
  104.                                 rentedDays[renter] = daysRented; //Storing number of days rented in the array
  105.                             }while(!verifyNumber(daysRented)); //Function for validating number entered
  106.                             do{
  107.                                 prt("Date (dd/mm/yyyy): ");
  108.                                 dateRented = sc_str.next();
  109.                                 rentedDate[renter] = dateRented; //Storing date in the array
  110.                             }while(!verifyDate(dateRented)); //Function for validating date
  111.  
  112.                             //Printing Receipt
  113.                             clr();
  114.                             prtln("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
  115.                             prtln("Rayaan Rent A Car");
  116.                             prtln("------------------------------------------------------------------------------------------------------------------------------------------------------------------");        
  117.                             prtln("Customer name: " + renterName[renter]);
  118.                             prtln("Date: " + rentedDate[renter]);
  119.                             prtln("Car Model: " + rentedCars[renter]);
  120.                             prtln("Rent Per Day: " + costPerDay[renter]);
  121.                             prtln("Number of Days Rented: " + rentedDays[renter]);
  122.                             prtln("Total Cost: " + costPerDay[renter]*rentedDays[renter]);
  123.                             prtln("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
  124.                             renter++;
  125.                             prtln("\nAgain? (y/n)");
  126.                             again = sc.next().charAt(0);
  127.                         }while(again == 'y');
  128.                         prtln("\nReturn to Main Menu? (y/n)");
  129.                         mainmenu = sc.next().charAt(0);
  130.                         break;
  131.                         //End of case 3//
  132.                         case 4:
  133.                         clr();
  134.                         for(int x = 0; x < rentedDays.length-1; x++){ //Sort the arrays in descending order of number of days rented using bubble sort
  135.                             for(int j = x + 1; j < rentedDays.length; j++){
  136.                                 if(rentedDays[x] < rentedDays[j]){
  137.                                     //Temporary variables
  138.                                     int newDays = rentedDays[x];
  139.                                     int newCost = costPerDay[x];
  140.                                     String newRentedCars = rentedCars[x];
  141.                                     String newDate = rentedDate[x];
  142.                                     String newName = renterName[x];
  143.                                     //Sorting
  144.                                     costPerDay[x] = costPerDay[j];
  145.                                     costPerDay[j] = newCost;
  146.                                     rentedCars[x] = rentedCars[j];
  147.                                     rentedCars[j] = newRentedCars;
  148.                                     renterName[x] = renterName[j];
  149.                                     renterName[j] = newName;
  150.                                     rentedDate[x] = rentedDate[j];
  151.                                     rentedDate[j] = newDate;
  152.                                 }
  153.                             }
  154.                         }
  155.                         //Printing the rented cars
  156.                         for(int x = 0; x < renter; x++){
  157.                             prtln(rentedCars[x] + "\t" + renterName[x] + "\t" + rentedDays[x] + "\t" + rentedDate[x]);
  158.                         }
  159.                         prtln("\nReturn to Main Menu? (y/n)");
  160.                         mainmenu = sc.next().charAt(0);
  161.                         break;
  162.                         //End of case 4//
  163.                         case 5:
  164.                         exit();//Cool Exit
  165.                         default: //if user enters NOTA
  166.                         prtln("Wrong input");
  167.                         prtln("\nReturn to Main Menu? (y/n)");
  168.                         mainmenu = sc.next().charAt(0);
  169.                     }//End of switch//
  170.                     flag = true; flag2 = false; //For knowing if username and/or password is vaild
  171.                     if(mainmenu == 'n')
  172.                         exit(); // I added an extra if construct so that if the user enters n it exits instead of doing nothing
  173.                 }while(mainmenu == 'y');
  174.             }else if(username.equalsIgnoreCase(usernames[i]) && !password.equals(passwords[i])){
  175.                 prtln("Invalid Password!");
  176.                 flag2 = false;
  177.                 break;
  178.             }else if(!username.equalsIgnoreCase(usernames[i]) && password.equals(passwords[i])){
  179.                 prtln("Invalid Username!");
  180.                 flag2 = false;
  181.                 break;
  182.             }
  183.         }
  184.         if(flag2){
  185.             prtln("Invalid Username and Password!");
  186.         }
  187.         if(!flag){
  188.             prtln("\nDo you want to try again? (y/n)");
  189.             char restart = sc.next().charAt(0);
  190.             if (restart == 'y'){
  191.                 main(); //Start all over again
  192.             }else{
  193.                 exit();//Cool Exit
  194.             }
  195.         }
  196.     }
  197.  
  198.     //Function for System.out.println
  199.     public static void prtln(String s){
  200.         System.out.println(s);
  201.     }
  202.  
  203.     //Function for System.out.print
  204.     public static void prt(String s){
  205.         System.out.print(s);
  206.     }
  207.  
  208.     //Function for clearing screen
  209.     public static void clr(){
  210.         prt("\f");
  211.     }
  212.    
  213.     //Function for welcoming
  214.     public static void welcome(){
  215.         prtln("Hello");
  216.         try{ //Cool Welcome
  217.             Thread.sleep(700);
  218.         }catch (InterruptedException e){
  219.             e.printStackTrace();
  220.         }
  221.         clr(); //Clearing the screen
  222.         prtln("------------------------------------------------------------------------------------------------------------------------------------------------------------------"); //Cool effect        
  223.         prtln("Welcome to Rayaan Rent A Car"); //:)
  224.         prtln("------------------------------------------------------------------------------------------------------------------------------------------------------------------"); //Cool effect
  225.     }
  226.  
  227.     //Function for printing menu
  228.     public static void printMenu(String s){
  229.         prtln("\f"+"Welcome, " + s +"\n"); //Welcoming users, \f for clearing the screen
  230.         prtln("Select an option:");
  231.         prtln("1. Display the details of the car arranged in the descending order of rent");
  232.         prtln("2. Search for a specific model and display the details");
  233.         prtln("3. Rent a Car");
  234.         prtln("4. Display the details of the cars that have been rented, arranged in the descending order of number of days for rent");
  235.         prtln("5. Exit");
  236.     }
  237.    
  238.     //Function for verifying name
  239.     public static boolean verifyName(String s){
  240.         boolean name_flag = false;
  241.         s = s.toUpperCase();
  242.         for(int i = 0; i < s.length(); i++){
  243.             char x = s.charAt(i);
  244.             if(x >= 'A' && x <= 'Z' || x == ' '){
  245.                 name_flag = true;
  246.             }else{
  247.                 prtln("Name can only contain letters! Try Again");
  248.                 name_flag = false;
  249.             }
  250.         }
  251.         return name_flag;
  252.     }
  253.  
  254.     //Function for verifying number
  255.     public static boolean verifyNumber(int n){
  256.         if(n > 0){
  257.             return true;
  258.         }else{
  259.             prtln("Has to be more than zero! Try Again");
  260.             return false;
  261.         }
  262.     }
  263.  
  264.     //Function for verifying date(very complex)
  265.     public static boolean verifyDate(String date){
  266.         Scanner sc = new Scanner(System.in);
  267.         int d, m, y;
  268.         String dd, mm, yy;
  269.         boolean flag = false;
  270.         int maxDays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//Array for storing the maximum days of every month
  271.         if(date.length() == 10){ //Performing the task only when number of digits is 10
  272.             dd = date.substring(0,2); //Extracting the day
  273.             mm = date.substring(3,5); //Extracting the month
  274.             yy = date.substring(6); //Extracting the year
  275.             d = Integer.parseInt(dd); //Day in Integer format
  276.             m = Integer.parseInt(mm); //Month in Integer format
  277.             y = Integer.parseInt(yy); //Year in Integer format
  278.  
  279.             if((y%400==0) || ((y%100!=0)&&(y%4==0))){ // condition for leap year
  280.                 maxDays[2]=29;
  281.             }
  282.  
  283.             /* checking whether the day, month and year are within acceptable range
  284.             i.e. there cannot be an input like 35/01/2013 because it is unacceptable*/
  285.  
  286.             if(m<0 || m>12 || d<0 || d>maxDays[m] || y<0 || y>9999){ // Performing Date Validation
  287.                 prtln("The day, month or year are outside acceptable limit");
  288.                 flag = false;
  289.             }
  290.             else{
  291.                 flag = true;
  292.             }
  293.         }
  294.         else{
  295.             prtln("Wrong Input");
  296.             flag = false;
  297.         }
  298.         return flag;
  299.     }
  300.  
  301.     public static void exit(){//Coolest Exit function
  302.         try{//Cool Exit
  303.             clr();
  304.             prt("Exiting");
  305.             Thread.sleep(100);
  306.             prt(".");
  307.             Thread.sleep(600);
  308.             prt(".");
  309.             Thread.sleep(600);
  310.             prt(".");
  311.             Thread.sleep(100);  
  312.         }catch (InterruptedException e){
  313.             e.printStackTrace();
  314.         }
  315.         System.exit(0);
  316.     }
  317. }
  318.  
  319. /*
  320.  ****************************************************************************************************************************************************************************************************
  321.  ******************************************* Variable Description ***********************************************************************************************************************************
  322.  ****************************************************************************************************************************************************************************************************
  323.  Variable           Type        Location        Purpose
  324.  ========           ====        ========        =======
  325.  flag               boolean     main()          for validating username and password
  326.  flag2              boolean     main()          for validating username and password
  327.  enteredModelName   String      main()          for accepting and storing model name entered by the user
  328.  customerName       String      main()          for accepting and storing Customer's name
  329.  dateRented         String      main()          for accepting and storing date entered by the user
  330.  daysRented         int         main()          for accepting and storing number of days for rent entered by user
  331.  renter             int         main()          for renting cars and storing them in arrays. This is for the array index
  332.  renterName[]       String[]    main()          for storing the name of the customer who is renting the car
  333.  rentedCars[]       String[]    main()          for storing the model names of the cars that have been rented
  334.  rentedDate[]       String[]    main()          for storing the date the car has been rented
  335.  rentedDays[]       int[]       main()          for storing the number of days the car has been rented for
  336.  again              char        main()          for storing the user's choice whether they want to repeat a function
  337.  mainmenu           char        main()          for storing the user's choice whether they want to go to the main menu or exit
  338.  carModels[]        String[]    main()          for storing the names of the car models that are available for rent
  339.  costPerDay[]       int[]       main()          for storing the cost per day of a car
  340.  usernames[]        String[]    main()          for storing the usernames
  341.  passwords[]        String[]    main()          for storing the passwords
  342.  choice             int         main()          for storing user's choice
  343.  ------             ---         ------          -------------------------
  344.  dd, mm, yy         String      verifyDate()    for storing the values entered by the user after Substring-ing them eliminating the '/'
  345.  d, m, y            int         verifyDate()    for storing the values by parsing them in Integer form from the date entered by the user
  346.  maxDays[]          int[]       verifyDate()    for storing the maximum number of days of each month
  347.  =========          =====       ============    ========================================================================================
  348.  ****************************************************************************************************************************************************************************************************
  349.  */
  350. //:)
  351. //Hope you liked the "neatness" (of the output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement