Advertisement
Guest User

RayaanRentACar

a guest
Oct 18th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.20 KB | None | 0 0
  1. import java.util.*;
  2. class Rayaan_Rent_A_Car{
  3. public static void main(){
  4. System.out.print("\f"); //Clearing the screen
  5. boolean flag = false, flag2 = true, flag3 = false;int zzz = 1; int mainmenu = 1;
  6. Scanner sc = new Scanner(System.in); //Initializing Scanner class
  7. System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
  8. System.out.println("Welcome to Chicken Rent A Car");
  9. System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
  10. String usernames[] = {"Rayaan", "Ali", "Ben"}; //Array for stroing usernames
  11. String passwords[] = {"chicken", "cat", "ball"}; //Array for storing passwords
  12. System.out.print("Username: ");
  13. String username = sc.next(); //Entered username by user
  14. System.out.print("Password: ");
  15. String password = sc.next(); //Entered password by user
  16. String carModel[] = {"Toyota Corolla", "Toyota Yaris", "Toyota Fortuner", "Toyota Land Cruiser", "Nissan Tiida",
  17. "Nissan Patrol", "Ford Explorer", "Ford Focus", "Mitsubishi Lancer", "Mitsubishi Pajero", "GMC Yukon XL"}; //Names of cars
  18. int costPerDay[] = {50, 45, 100, 200, 50, 200, 100, 75, 50, 100, 250}; //Rental cost per day
  19. for(int i = 0; i < 3; i++){ //For loop for index number of arrays
  20. if (username.equalsIgnoreCase(usernames[i]) && password.equals(passwords[i])){ //Verifying username and password match
  21. System.out.println("\f"+"Welcome, " + usernames[i] +"\n"); //Welcoming users, \f for clearing the screen
  22. System.out.println("Select an option:");
  23. System.out.println("1. Display the details of the car arranged in the descending order of rent");
  24. System.out.println("2. Search for a specific model and display the details");
  25. System.out.println("3. Rent a Car");
  26. 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");
  27. System.out.println("5. Exit");
  28. int choice = sc.nextInt(); //Variable for storing user's choice
  29. switch(choice){
  30. case 1:
  31. System.out.print("\f");
  32. for(int x = 0; x < costPerDay.length-1; x++){ //Sort the Cost Per Day Array in descending order using bubble sort
  33. for(int j = x + 1; j < costPerDay.length; j++){
  34. if(costPerDay[x] < costPerDay[j]){
  35. int t = costPerDay[x];
  36. String s = carModel[x];
  37. costPerDay[x] = costPerDay[j];
  38. costPerDay[j] = t;
  39. carModel[x] = carModel[j];
  40. carModel[j] = s;
  41. }
  42. }
  43. }
  44. for(int z = 0; z < carModel.length; z++){
  45. System.out.println(carModel[z] + " - " + costPerDay[z] );
  46. }
  47. System.out.println("Press 1 to return to the main menu");
  48. mainmenu = sc.nextInt();
  49. break;
  50. //End of case 1//
  51. case 2:
  52. System.out.print("\f");
  53. String space = sc.nextLine();
  54. System.out.println("Enter the name of the car");
  55. String enteredName = sc.nextLine();
  56. for(int c = 0; c < carModel.length; c++){
  57. if (carModel[c].equalsIgnoreCase(enteredName)){
  58. flag3 = true;
  59. System.out.print("\f");
  60. System.out.println("Car name: " + carModel[c]);
  61. System.out.println("Cost per day: " + costPerDay[c]);
  62. break;
  63. }
  64. }
  65. if(!flag3){
  66. System.out.println("Not Found!");
  67. }
  68. break;
  69. //End of case 2//
  70. case 3:
  71. System.out.print("\f");
  72. sc.nextLine();
  73. boolean name_flag = false;
  74. String customerName;
  75. int daysRented;
  76. boolean daysRented_flag = false;
  77. String model;
  78. boolean model_flag = false;
  79. do{
  80. System.out.print("Customer Name: ");
  81. customerName = sc.nextLine().toUpperCase();
  82. for(int checkr = 0; checkr < customerName.length(); checkr++){
  83. char case3i = customerName.charAt(checkr);
  84. if(case3i >= 'A' && case3i <= 'Z' || case3i == ' '){
  85. name_flag = true;
  86. }else{
  87. System.out.println("Name can only contain letters! Try Again");
  88. name_flag = false;
  89. break;
  90. }
  91. }
  92. }while(!name_flag);
  93. do{
  94. System.out.print("Number of Days Rented: ");
  95. daysRented = sc.nextInt();
  96. sc.nextLine();
  97. if(daysRented > 0){
  98. daysRented_flag = true;
  99. }else{
  100. System.out.println("Has to be more than zero! Try Again");
  101. }
  102. }while(!daysRented_flag);
  103. do{
  104. System.out.print("Car Model: ");
  105. model = sc.nextLine();
  106. for(int f = 0; f < carModel.length; f++){
  107. if (model.equalsIgnoreCase(carModel[f])){
  108. model_flag = true;
  109. zzz = f;
  110. break;
  111. }else{
  112. System.out.println("Not found! Try again");
  113. model_flag = false;
  114. break;
  115. }
  116. }
  117. }while(!model_flag);
  118. System.out.print("Date: ");
  119. String date = sc.nextLine();
  120. System.out.println("");
  121. System.out.print("\f");
  122. System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
  123. System.out.println("Chicken Rent A Car");
  124. System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
  125. System.out.println("Customer name: " + customerName);
  126. System.out.println("Date: " + date);
  127. System.out.println("Car Model: " + carModel[zzz]);
  128. System.out.println("Rent Per Day: " + costPerDay[zzz]);
  129. System.out.println("Number of Days Rented: " + daysRented);
  130. System.out.println("Total Cost: " + costPerDay[zzz]*daysRented);
  131. System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------");
  132. break;
  133. //End of case 3//
  134. case 4:
  135.  
  136.  
  137. break;
  138. case 5:
  139. System.exit(0);
  140. }
  141. flag = true; flag2 = false;
  142. if(mainmenu == 1)
  143. continue;
  144. break;
  145. }else if(username.equalsIgnoreCase(usernames[i]) && !password.equals(passwords[i])){
  146. System.out.println("Invalid Password!");
  147. flag2 = false;
  148. break;
  149. }else if(!username.equalsIgnoreCase(usernames[i]) && password.equals(passwords[i])){
  150. System.out.println("Invalid Username!");
  151. flag2 = false;
  152. break;
  153. }
  154. }
  155. if(flag2 == true){
  156. System.out.println("Invalid Username and Password!");
  157. }
  158. if(flag == false){
  159. System.out.println("Do you want to try again? 1 for yes and 0 for no");
  160. int restart = sc.nextInt();
  161. if (restart == 1){
  162. main();
  163. }else{
  164. System.exit(0);
  165. }
  166. }
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement