Advertisement
Guest User

computersRayaan

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