Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*; //Import Scanner Class
- class Rent_A_Car{
- public static void main(){
- clr(); //Clearing the screen
- boolean flag = false, flag2 = true, car_flag = false;
- String enteredModelName, customerName, dateRented;
- int daysRented, renter = 0;
- String renterName[] = new String[15]; //Array for storing names of customers who have rented cars
- String rentedCars[] = new String[15]; //Array for storing Model names of the cars that have been rented
- String rentedDate[] = new String[15]; //Array for storing the Date of when the cars were rented
- int rentedDays[] = new int[15]; // Array for storing the number of days the cars have been rented
- Scanner sc = new Scanner(System.in); //Initializing Scanner class
- Scanner sc_str = new Scanner(System.in); //Initializing Scanner class for Strings
- char again = 'n', mainmenu = 'n'; //Two variables to ask user if they want to retry or go back to the main menu
- 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
- int costPerDay[] = {50, 45, 100, 200, 50, 200, 100, 75, 50, 100, 250}; //Rental cost per day
- String usernames[] = {"Rayaan", "Swarna", "1"}; //Array for storing usernames
- String passwords[] = {"chicken", "computers", "1"}; //Array for storing passwords
- welcome(); //Welcoming user
- prt("\nUsername: ");
- String username = sc.next(); //Entered username by user
- prt("Password: ");
- String password = sc.next(); //Entered password by user
- for(int i = 0; i < 3; i++){ //For loop for index number of arrays because we have 3 usernames
- if (username.equalsIgnoreCase(usernames[i]) && password.equals(passwords[i])){ //Verifying username and password match
- do{
- printMenu(usernames[i]); //Printing menu
- int choice = sc.nextInt(); //Variable for storing user's choice
- switch(choice){
- case 1:
- clr();
- for(int x = 0; x < costPerDay.length-1; x++){ //Sort the arrays in descending order of cost per day using bubble sort
- for(int j = x + 1; j < costPerDay.length; j++){
- if(costPerDay[x] < costPerDay[j]){
- int t = costPerDay[x]; //temporary variable
- String s = carModels[x]; //temporary variable
- costPerDay[x] = costPerDay[j];
- costPerDay[j] = t;
- carModels[x] = carModels[j];
- carModels[j] = s;
- }
- }
- }
- for(int x = 0; x < carModels.length; x++){ //Printing the Cars in descending order of cost per day
- prtln(carModels[x] + " - " + costPerDay[x] );
- }
- prtln("\nReturn to Main Menu? (y/n)"); //Asking user if they want to return to main menu
- mainmenu = sc.next().charAt(0); //Variable for storing choice
- break;
- //End of case 1//
- case 2:
- do{
- clr();
- prtln("Enter the name of the car");
- enteredModelName = sc_str.nextLine();
- for(int x = 0; x < carModels.length; x++){ //Validating model name and printing its details
- if (carModels[x].equalsIgnoreCase(enteredModelName)){
- car_flag = true;
- clr();
- prtln("Car name: " + carModels[x]);
- prtln("Cost per day: " + costPerDay[x]);
- break;
- }
- }
- if(!car_flag){ //Error message if car wasn't found
- prtln("Not Found!");
- }
- prtln("\nAgain? (y/n)"); //Asking user if they want to try again
- again = sc.next().charAt(0);
- }while(again == 'y' );
- prtln("\nReturn to Main Menu? (y/n)");
- mainmenu = sc.next().charAt(0);
- break;
- //End of case 2//
- case 3:
- do{
- clr();
- prtln("Rent A Car");
- sc = new Scanner(System.in); //Reinitializing scanner to prevent fall through
- sc_str = new Scanner(System.in);
- car_flag = false;
- do{
- prt("Model Name: ");
- enteredModelName = sc_str.nextLine();
- for(int x = 0; x < carModels.length; x++){
- if (carModels[x].equalsIgnoreCase(enteredModelName)){ //Validating car model
- car_flag = true;
- rentedCars[renter] = carModels[x]; //Storing car model in the array
- break;
- }
- }
- if(!car_flag){
- prtln("Not Found!");
- }
- }while(!car_flag);
- do{
- prt("Customer Name: ");
- customerName = sc_str.nextLine();
- renterName[renter] = customerName; //Storing name in the array
- }while(!verifyName(customerName)); //Function for validating name
- do{
- prt("Number of Days Rented: ");
- daysRented = sc.nextInt();
- rentedDays[renter] = daysRented; //Storing number of days rented in the array
- }while(!verifyNumber(daysRented)); //Function for validating number entered
- do{
- prt("Date (dd/mm/yyyy): ");
- dateRented = sc_str.next();
- rentedDate[renter] = dateRented; //Storing date in the array
- }while(!verifyDate(dateRented)); //Function for validating date
- //Printing Receipt
- clr();
- prtln("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
- prtln("Rayaan Rent A Car");
- prtln("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
- prtln("Customer name: " + renterName[renter]);
- prtln("Date: " + rentedDate[renter]);
- prtln("Car Model: " + rentedCars[renter]);
- prtln("Rent Per Day: " + costPerDay[renter]);
- prtln("Number of Days Rented: " + rentedDays[renter]);
- prtln("Total Cost: " + costPerDay[renter]*rentedDays[renter]);
- prtln("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
- renter++;
- prtln("\nAgain? (y/n)");
- again = sc.next().charAt(0);
- }while(again == 'y');
- prtln("\nReturn to Main Menu? (y/n)");
- mainmenu = sc.next().charAt(0);
- break;
- //End of case 3//
- case 4:
- clr();
- for(int x = 0; x < rentedDays.length-1; x++){ //Sort the arrays in descending order of number of days rented using bubble sort
- for(int j = x + 1; j < rentedDays.length; j++){
- if(rentedDays[x] < rentedDays[j]){
- //Temporary variables
- int newDays = rentedDays[x];
- int newCost = costPerDay[x];
- String newRentedCars = rentedCars[x];
- String newDate = rentedDate[x];
- String newName = renterName[x];
- //Sorting
- costPerDay[x] = costPerDay[j];
- costPerDay[j] = newCost;
- rentedCars[x] = rentedCars[j];
- rentedCars[j] = newRentedCars;
- renterName[x] = renterName[j];
- renterName[j] = newName;
- rentedDate[x] = rentedDate[j];
- rentedDate[j] = newDate;
- }
- }
- }
- //Printing the rented cars
- for(int x = 0; x < renter; x++){
- prtln(rentedCars[x] + "\t" + renterName[x] + "\t" + rentedDays[x] + "\t" + rentedDate[x]);
- }
- prtln("\nReturn to Main Menu? (y/n)");
- mainmenu = sc.next().charAt(0);
- break;
- //End of case 4//
- case 5:
- exit();//Cool Exit
- default: //if user enters NOTA
- prtln("Wrong input");
- prtln("\nReturn to Main Menu? (y/n)");
- mainmenu = sc.next().charAt(0);
- }//End of switch//
- flag = true; flag2 = false; //For knowing if username and/or password is vaild
- if(mainmenu == 'n')
- exit(); // I added an extra if construct so that if the user enters n it exits instead of doing nothing
- }while(mainmenu == 'y');
- }else if(username.equalsIgnoreCase(usernames[i]) && !password.equals(passwords[i])){
- prtln("Invalid Password!");
- flag2 = false;
- break;
- }else if(!username.equalsIgnoreCase(usernames[i]) && password.equals(passwords[i])){
- prtln("Invalid Username!");
- flag2 = false;
- break;
- }
- }
- if(flag2){
- prtln("Invalid Username and Password!");
- }
- if(!flag){
- prtln("\nDo you want to try again? (y/n)");
- char restart = sc.next().charAt(0);
- if (restart == 'y'){
- main(); //Start all over again
- }else{
- exit();//Cool Exit
- }
- }
- }
- //Function for System.out.println
- public static void prtln(String s){
- System.out.println(s);
- }
- //Function for System.out.print
- public static void prt(String s){
- System.out.print(s);
- }
- //Function for clearing screen
- public static void clr(){
- prt("\f");
- }
- //Function for welcoming
- public static void welcome(){
- prtln("Hello");
- try{ //Cool Welcome
- Thread.sleep(700);
- }catch (InterruptedException e){
- e.printStackTrace();
- }
- clr(); //Clearing the screen
- prtln("------------------------------------------------------------------------------------------------------------------------------------------------------------------"); //Cool effect
- prtln("Welcome to Rayaan Rent A Car"); //:)
- prtln("------------------------------------------------------------------------------------------------------------------------------------------------------------------"); //Cool effect
- }
- //Function for printing menu
- public static void printMenu(String s){
- prtln("\f"+"Welcome, " + s +"\n"); //Welcoming users, \f for clearing the screen
- prtln("Select an option:");
- prtln("1. Display the details of the car arranged in the descending order of rent");
- prtln("2. Search for a specific model and display the details");
- prtln("3. Rent a Car");
- prtln("4. Display the details of the cars that have been rented, arranged in the descending order of number of days for rent");
- prtln("5. Exit");
- }
- //Function for verifying name
- public static boolean verifyName(String s){
- boolean name_flag = false;
- s = s.toUpperCase();
- for(int i = 0; i < s.length(); i++){
- char x = s.charAt(i);
- if(x >= 'A' && x <= 'Z' || x == ' '){
- name_flag = true;
- }else{
- prtln("Name can only contain letters! Try Again");
- name_flag = false;
- }
- }
- return name_flag;
- }
- //Function for verifying number
- public static boolean verifyNumber(int n){
- if(n > 0){
- return true;
- }else{
- prtln("Has to be more than zero! Try Again");
- return false;
- }
- }
- //Function for verifying date(very complex)
- public static boolean verifyDate(String date){
- Scanner sc = new Scanner(System.in);
- int d, m, y;
- String dd, mm, yy;
- boolean flag = false;
- int maxDays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//Array for storing the maximum days of every month
- if(date.length() == 10){ //Performing the task only when number of digits is 10
- dd = date.substring(0,2); //Extracting the day
- mm = date.substring(3,5); //Extracting the month
- yy = date.substring(6); //Extracting the year
- d = Integer.parseInt(dd); //Day in Integer format
- m = Integer.parseInt(mm); //Month in Integer format
- y = Integer.parseInt(yy); //Year in Integer format
- if((y%400==0) || ((y%100!=0)&&(y%4==0))){ // condition for leap year
- maxDays[2]=29;
- }
- /* checking whether the day, month and year are within acceptable range
- i.e. there cannot be an input like 35/01/2013 because it is unacceptable*/
- if(m<0 || m>12 || d<0 || d>maxDays[m] || y<0 || y>9999){ // Performing Date Validation
- prtln("The day, month or year are outside acceptable limit");
- flag = false;
- }
- else{
- flag = true;
- }
- }
- else{
- prtln("Wrong Input");
- flag = false;
- }
- return flag;
- }
- public static void exit(){//Coolest Exit function
- try{//Cool Exit
- clr();
- prt("Exiting");
- Thread.sleep(100);
- prt(".");
- Thread.sleep(600);
- prt(".");
- Thread.sleep(600);
- prt(".");
- Thread.sleep(100);
- }catch (InterruptedException e){
- e.printStackTrace();
- }
- System.exit(0);
- }
- }
- /*
- ****************************************************************************************************************************************************************************************************
- ******************************************* Variable Description ***********************************************************************************************************************************
- ****************************************************************************************************************************************************************************************************
- Variable Type Location Purpose
- ======== ==== ======== =======
- flag boolean main() for validating username and password
- flag2 boolean main() for validating username and password
- enteredModelName String main() for accepting and storing model name entered by the user
- customerName String main() for accepting and storing Customer's name
- dateRented String main() for accepting and storing date entered by the user
- daysRented int main() for accepting and storing number of days for rent entered by user
- renter int main() for renting cars and storing them in arrays. This is for the array index
- renterName[] String[] main() for storing the name of the customer who is renting the car
- rentedCars[] String[] main() for storing the model names of the cars that have been rented
- rentedDate[] String[] main() for storing the date the car has been rented
- rentedDays[] int[] main() for storing the number of days the car has been rented for
- again char main() for storing the user's choice whether they want to repeat a function
- mainmenu char main() for storing the user's choice whether they want to go to the main menu or exit
- carModels[] String[] main() for storing the names of the car models that are available for rent
- costPerDay[] int[] main() for storing the cost per day of a car
- usernames[] String[] main() for storing the usernames
- passwords[] String[] main() for storing the passwords
- choice int main() for storing user's choice
- ------ --- ------ -------------------------
- dd, mm, yy String verifyDate() for storing the values entered by the user after Substring-ing them eliminating the '/'
- d, m, y int verifyDate() for storing the values by parsing them in Integer form from the date entered by the user
- maxDays[] int[] verifyDate() for storing the maximum number of days of each month
- ========= ===== ============ ========================================================================================
- ****************************************************************************************************************************************************************************************************
- */
- //:)
- //Hope you liked the "neatness" (of the output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement