Advertisement
Guest User

Untitled

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