grhey

Water Billing SystemV3

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