Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Rent_A_Ca{
- public static void main(){
- int mainmenu = 1;
- System.out.print("\f"); //Clearing the screen
- boolean flag = false, flag2 = true, flag3 = false;int zzz = 1;
- Scanner sc = new Scanner(System.in); //Initializing Scanner class
- System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
- System.out.println("Welcome to Chicken Rent A Car");
- System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
- String usernames[] = {"Rayaan", "Ali", "Ben"}; //Array for stroing usernames
- String passwords[] = {"chicken", "cat", "ball"}; //Array for storing passwords
- System.out.print("Username: ");
- String username = sc.next(); //Entered username by user
- System.out.print("Password: ");
- String password = sc.next(); //Entered password by user
- String carModel[] = {"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
- for(int i = 0; i < 3; i++){ //For loop for index number of arrays
- if (username.equalsIgnoreCase(usernames[i]) && password.equals(passwords[i])){ //Verifying username and password match
- do{
- System.out.println("\f"+"Welcome, " + usernames[i] +"\n"); //Welcoming users, \f for clearing the screen
- System.out.println("Select an option:");
- System.out.println("1. Display the details of the car arranged in the descending order of rent");
- System.out.println("2. Search for a specific model and display the details");
- System.out.println("3. Rent a Car");
- System.out.println("4. Display the details of the cars that have been rented, arranged in the descending order of number of days for rent");
- System.out.println("5. Exit");
- int choice = sc.nextInt(); //Variable for storing user's choice
- switch(choice){
- case 1:
- System.out.print("\f");
- for(int x = 0; x < costPerDay.length-1; x++){ //Sort the Cost Per Day Array in descending order using bubble sort
- for(int j = x + 1; j < costPerDay.length; j++){
- if(costPerDay[x] < costPerDay[j]){
- int t = costPerDay[x];
- String s = carModel[x];
- costPerDay[x] = costPerDay[j];
- costPerDay[j] = t;
- carModel[x] = carModel[j];
- carModel[j] = s;
- }
- }
- }
- for(int z = 0; z < carModel.length; z++){
- System.out.println(carModel[z] + " - " + costPerDay[z] );
- }
- System.out.println("\nPress 1 to return to the main menu");
- mainmenu = sc.nextInt();
- break;
- //End of case 1//
- case 2:
- System.out.print("\f");
- String space = sc.nextLine();
- System.out.println("Enter the name of the car");
- String enteredName = sc.nextLine();
- for(int c = 0; c < carModel.length; c++){
- if (carModel[c].equalsIgnoreCase(enteredName)){
- flag3 = true;
- System.out.print("\f");
- System.out.println("Car name: " + carModel[c]);
- System.out.println("Cost per day: " + costPerDay[c]);
- break;
- }
- }
- if(!flag3){
- System.out.println("Not Found!");
- }
- System.out.println("\nPress 1 to return to the main menu");
- mainmenu = sc.nextInt();
- break;
- //End of case 2//
- case 3:
- System.out.print("\f");
- sc.nextLine();
- boolean name_flag = false;
- String customerName = "HI";
- int daysRented;
- boolean daysRented_flag = false;
- String model;
- boolean model_flag = false;
- chkname();
- chkint();
- do{
- System.out.print("Car Model: ");
- model = sc.nextLine();
- for(int f = 0; f < carModel.length; f++){
- if (model.equalsIgnoreCase(carModel[f])){
- model_flag = true;
- zzz = f;
- break;
- }else{
- System.out.println("Not found! Try again");
- model_flag = false;
- break;
- }
- }
- }while(!model_flag);
- System.out.print("Date: ");
- String date = sc.nextLine();
- System.out.println("");
- System.out.print("\f");
- System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
- System.out.println("Chicken Rent A Car");
- System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
- System.out.println("Customer name: " + customerName);
- System.out.println("Date: " + date);
- System.out.println("Car Model: " + carModel[zzz]);
- System.out.println("Rent Per Day: " + costPerDay[zzz]);
- System.out.println("Number of Days Rented: " + daysRented);
- System.out.println("Total Cost: " + costPerDay[zzz]*daysRented);
- System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
- System.out.println("\nPress 1 to return to the main menu");
- mainmenu = sc.nextInt();
- break;
- //End of case 3//
- case 4:
- System.out.println("\nPress 1 to return to the main menu");
- mainmenu = sc.nextInt();
- break;
- case 5:
- System.exit(0);
- }
- flag = true; flag2 = false;
- }while(mainmenu == 1);
- }else if(username.equalsIgnoreCase(usernames[i]) && !password.equals(passwords[i])){
- System.out.println("Invalid Password!");
- flag2 = false;
- break;
- }else if(!username.equalsIgnoreCase(usernames[i]) && password.equals(passwords[i])){
- System.out.println("Invalid Username!");
- flag2 = false;
- break;
- }
- }
- if(flag2 == true){
- System.out.println("Invalid Username and Password!");
- }
- if(flag == false){
- System.out.println("Do you want to try again? 1 for yes and 0 for no");
- int restart = sc.nextInt();
- if (restart == 1){
- main();
- }else{
- System.exit(0);
- }
- }
- }
- public static void chkname (){
- Scanner sc = new Scanner(System.in);
- boolean c = false;
- do{
- System.out.print("Customer Name: ");
- String customerName = sc.nextLine().toUpperCase();
- for(int checkr = 0; checkr < customerName.length(); checkr++){
- char case3i = customerName.charAt(checkr);
- if(case3i >= 'A' && case3i <= 'Z' || case3i == ' '){
- c = true;
- }else{
- System.out.println("Name can only contain letters! Try Again");
- c = false;
- break;
- }
- }
- }while(!c);
- }
- public static void chkint(){
- Scanner sc = new Scanner(System.in);
- boolean daysRented_flag;
- do{
- System.out.print("Number of Days Rented: ");
- int daysRented = sc.nextInt();
- sc.nextLine();
- if(daysRented > 0){
- daysRented_flag = true;
- }else{
- System.out.println("Has to be more than zero! Try Again");
- }
- }while(!daysRented_flag);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement