grhey

WaterBillingStsyemV2

Jun 26th, 2022 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.85 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList; //arraylist uses non prmitive data type
  3.  
  4.  
  5. public class Defense_WaterBilling { //class
  6.  
  7. static ArrayList<String> name = new ArrayList<String> (); //setting arraylist as a global varibale to access in the whole parts of the program
  8. static ArrayList<String> address = new ArrayList<String> ();
  9. static ArrayList<Double> prevMreading = new ArrayList<Double> (); //Uses wrapper classes
  10. static ArrayList<Double> currMreading = new ArrayList<Double> ();
  11. static ArrayList<Double> Dues = new ArrayList<Double> ();
  12. static ArrayList<Double> consumption = new ArrayList<Double> ();
  13. static ArrayList<String> discount = new ArrayList<String> ();
  14. static Scanner scr = new Scanner(System.in); // initializing the scanner as a global variable also
  15.  
  16. public static void main(String[] args) { //the main method includes the while loop for looping the main menu and uses switch statement
  17.  
  18. int mainMenu = -1;
  19. while (mainMenu != 0) { //while loop for looping the menu until the user press exit
  20. menu(); //calling the menu user defined method
  21. mainMenu = scr.nextInt(); //getting user choice
  22. scr.nextLine();
  23. switch (mainMenu) {
  24. case 0:
  25. exit();
  26. break;//calling the userdefined methods
  27. case 1:
  28. add();
  29. break;//calling the userdefined methods
  30. case 2:
  31. update();
  32. break;//calling the userdefined methods
  33. case 3:
  34. display();
  35. break;//calling the userdefined methods
  36. case 4:
  37. search();
  38. break;//calling the userdefined methods
  39. default: System.out.println("Out of range");
  40. }
  41. }
  42. }
  43.  
  44. static void menu() { //the menu output
  45. System.out.println("-------[Water Billing System]-------");
  46. System.out.println("[1] Create a new user account.");
  47. System.out.println("[2] Update a current account");
  48. System.out.println("[3] Show accounts for all users.");
  49. System.out.println("[4] Try looking for an account here.");
  50. System.out.println("[0] Exit");
  51. System.out.print("Enter Your Choice: ");
  52.  
  53. }
  54.  
  55. static void add() { //method to add data to the arraylist
  56. String Name, Address;
  57. double prev, curr,consumed, due, sub = 0.0;
  58. int rate;
  59. System.out.println("------Create a new user account!------");
  60. System.out.println("Name: ");
  61. Name = scr.nextLine();
  62. Defense_WaterBilling.name.add(Name); //insert the element to the arraylist
  63. System.out.println("Address: ");
  64. Address = scr.nextLine(); //getting String input from the user
  65. Defense_WaterBilling.address.add(Address); //insert the element to the arraylist
  66. System.out.println("Previous reading: ");
  67. prev = scr.nextDouble(); //getting input from the user
  68. prevMreading.add(prev); //insert the element to the arraylist
  69. System.out.println("Current reading: ");
  70. curr = scr.nextDouble(); //getting input from the user
  71. currMreading.add(curr); //insert the element to the arraylist
  72. System.out.println("[1] Residential");
  73. System.out.println("[2] Business");
  74. rate = scr.nextInt(); //getting input from the user
  75. consumed = curr - prev;
  76. consumption.add(consumed); //insert the element to the arraylist
  77. if (rate == 1) { //nested if to get the sub value
  78. if (consumed > 10) {
  79. sub = 200 + ((consumed - 10) * 30);
  80. }
  81. if (consumed <= 10) {
  82. sub = 200;
  83. }
  84. } else if (rate == 2) {
  85. if (consumed > 20) {
  86. sub = 500 + ((consumed - 20) * 50);
  87. }
  88. if (consumed <= 20) {
  89. sub = 500;
  90. }
  91. }
  92. int yearser;
  93. System.out.println("how many years in using the service?");
  94. yearser = scr.nextInt(); //getting input from the user
  95. if (yearser >= 15 ) {
  96. due = sub - (sub * .15);
  97. discount.add("15% promo discount applied!");
  98. } else {
  99. due = sub;
  100. discount.add("No promo discount appllied!");
  101. }
  102. Dues.add(due); //insert the element to the arraylist
  103. }
  104.  
  105. static void update() {
  106. String namess, newName, newAddress;
  107. System.out.println("------Update a current account------!");
  108. System.out.println("Enter the name of the current Account: ");
  109. namess = scr.nextLine();
  110. int position = 0; //initialize postion to 0 value
  111. boolean matched = false; //initialize matched to false value
  112. for (String profile : Defense_WaterBilling.name) { // uses for each loop to get the element,The loop goes through each element of array name
  113. if (profile.contains(namess)) { //check if the element is present in the array
  114. matched = true;
  115. position = Defense_WaterBilling.name.indexOf(profile); // return the position of the element
  116. break;
  117. } else {
  118. matched = false;
  119. System.out.println("No data found !!!!!");
  120. }
  121. }
  122. if (matched == true) {
  123. System.out.println("New name: ");
  124. newName = scr.nextLine(); //getting the new name input from the user
  125. Defense_WaterBilling.name.set(position, newName);
  126. System.out.println("New address: ");
  127. newAddress = scr.nextLine(); //getting the new address input from the user
  128. address.set(position, newAddress);
  129.  
  130.  
  131. }
  132. }
  133.  
  134. static void display() {
  135. System.out.println("------Show accounts for all users!------");
  136. for (int i = 0; i < name.size(); i++) { //uses for loop
  137. System.out.println("Name: " + name.get(i)); // i is the index of the array name
  138. System.out.println("Address: " + address.get(i)); //get, is a method in arraylist
  139. System.out.println("Previous reading: " + prevMreading.get(i));
  140. System.out.println("Current reading: " + currMreading.get(i));
  141. System.out.println("Current consumption: " + consumption.get(i));
  142. System.out.println("Total payment due: " + Dues.get(i));
  143. System.out.println("Promo: " + discount.get(i));
  144. System.out.println("");
  145.  
  146.  
  147. }
  148. }
  149.  
  150. static void search() {
  151. int position = 0; //initialize postion to 0 value
  152. String names;
  153. boolean matched = false; //initialize matched to false value
  154. System.out.println("------Try looking for an account here!------");
  155. System.out.println("Name: ");
  156. names = scr.nextLine();
  157. for (String profile : Defense_WaterBilling.name) { //The loop goes through each element of array name
  158. if (profile.contains(names)) { //check if the element is present in the array
  159. matched = true;
  160. position = Defense_WaterBilling.name.indexOf(profile); //position = index where the profile is
  161. break;
  162. } else {
  163. matched = false;
  164. System.out.println("Not found!!!!!!!");
  165. }
  166. }
  167.  
  168. if (matched == true) {
  169. System.out.println("");
  170. System.out.println("Name: " + Defense_WaterBilling.name.get(position)); //print where the index of the searched element
  171. System.out.println("Address: " + Defense_WaterBilling.address.get(position));
  172. System.out.println("Previous reading: " + prevMreading.get(position));
  173. System.out.println("Current reading: " + currMreading.get(position));
  174. System.out.println("Current consumption: " + consumption.get(position));
  175. System.out.println("Total payment due: " + Dues.get(position));
  176. System.out.println("Promo: " + discount.get(position));
  177. System.out.println("");
  178.  
  179. }
  180. }
  181.  
  182. static int exit() { //exit method
  183. return 0;
  184. }
  185.  
  186. }
  187.  
Add Comment
Please, Sign In to add comment