grhey

WaterBillingSystemV7

Jul 3rd, 2022
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.80 KB | None | 0 0
  1. /**
  2. *This program, 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. import java.util.Scanner; // for getting user input
  7. import java.util.ArrayList; //arraylist stores data and uses non primitive data type
  8. public class Defense_WaterBilling { //class
  9.  
  10. static ArrayList<String> name = new ArrayList<String> (); // creating object for arraylist
  11. static ArrayList<String> address = new ArrayList<String> (); //setting arraylist as a global variable to access in the whole parts of the program
  12. static ArrayList<Double> prevMreading = new ArrayList<Double> (); //Uses wrapper classes
  13. static ArrayList<Double> currMreading = new ArrayList<Double> ();
  14. static ArrayList<Double> Dues = new ArrayList<Double> ();
  15. static ArrayList<Double> consumption = new ArrayList<Double> ();
  16. static Scanner scr = new Scanner(System.in); // initializing the scanner as a global variable also
  17.  
  18. public static void main(String[] args) { //the main method includes the while loop for looping the main menu and uses switch statement
  19.  
  20. int mainMenu = -1; //initialize mainMenu = -1
  21. while (mainMenu != 0) { //while loop for looping the menu until the user press exit
  22. menu(); //calling the menu user defined method
  23. mainMenu = scr.nextInt(); //getting user choice
  24. scr.nextLine();
  25. switch (mainMenu) {
  26. case 0:
  27. exit(); //calling the userdefined methods
  28. break;
  29. case 1:
  30. add();//calling the userdefined methods
  31. break;
  32. case 2:
  33. update(); //calling the userdefined methods
  34. break;
  35. case 3:
  36. display(); //calling the userdefined methods
  37. break;
  38. case 4:
  39. search(); //calling the userdefined methods
  40. break;
  41. case 5:
  42. delete(); //calling the userdefined methods
  43. break;
  44. default: System.out.println("Out of range");
  45. }
  46. }
  47. }
  48.  
  49. static void menu() { //the menu output
  50. System.out.println("-------[Water Billing System]-------");
  51. System.out.println("[1] Create a new user account.");
  52. System.out.println("[2] Update a current account");
  53. System.out.println("[3] Show accounts for all users.");
  54. System.out.println("[4] Try looking for an account here.");
  55. System.out.println("[5] Delete an Account");
  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. char overdue,quarter;
  66. System.out.println("------Create a new user account!------");
  67. System.out.println("Name with corresponding meter code: ");
  68. Name = scr.nextLine();
  69. name.add(Name); //insert the element to the arraylist
  70. System.out.println("Address: ");
  71. Address = scr.nextLine(); //getting String input from the user
  72. address.add(Address); //insert the element to the arraylist
  73. System.out.println("Previous reading: ");
  74. prev = scr.nextDouble(); //getting input from the user
  75. prevMreading.add(prev); //insert the element to the arraylist
  76. System.out.println("Current reading: ");
  77. curr = scr.nextDouble(); //getting input from the user
  78. currMreading.add(curr); //insert the element to the arraylist
  79. System.out.println("[1] Residential");
  80. System.out.println("[2] Business");
  81. rate = scr.nextInt(); //getting input from the user
  82. consumed = curr - prev;
  83. consumption.add(consumed); //insert the element to the arraylist
  84. if (rate == 1) { //nested if to get the sub value
  85. if (consumed > 10) {
  86. sub = 200 + ((consumed - 10) * 30);
  87. }
  88. if (consumed <= 10) {
  89. sub = 200;
  90. }
  91. } else if (rate == 2) {
  92. if (consumed > 20) {
  93. sub = 500 + ((consumed - 20) * 50);
  94. }
  95. if (consumed <= 20) {
  96. sub = 500;
  97. }
  98. }
  99. System.out.println("-------Is the payment overdue?-------");
  100. System.out.println("[press [y] if yes and any letter if no]");
  101. System.out.println("[Note: Lower Case Only!]");
  102. overdue=scr.next().charAt(0);
  103. if (overdue == 'y'){
  104. due = sub + 200;
  105. }else {
  106. due = sub;
  107. }
  108. if (rate == 1){
  109. System.out.println("-------Is it quarter of the year?--------");
  110. System.out.println("[press [y] if yes and any letter if no]");
  111. System.out.println("[Note: Lower Case Only!]");
  112. quarter = scr.next().charAt(0);
  113. if(quarter == 'y'){
  114. int yearser;
  115. System.out.println("how many years in using the service?");
  116. yearser = scr.nextInt(); //getting input from the user
  117. if (yearser >= 15 ) {
  118. due = sub - (sub * .15);
  119.  
  120. } else {
  121. due = sub;
  122. }
  123. }
  124. }
  125. Dues.add(due); //insert the element to the arraylist
  126. }
  127.  
  128. static void update() { //function will update name and address only
  129. String namess, newName, newAddress;
  130. System.out.println("------Update a current account------!");
  131. System.out.println("Enter the name of the current Account: ");
  132. namess = scr.nextLine();
  133. int position = 0; //initialize postion to 0 value
  134. boolean matched = false; //initialize matched to false value
  135. for (String profile : name) { // uses for each loop to get the element,The loop goes through each index of array name
  136. if (profile.contains(namess)) { //check if the element is present in the array and exactly the same as the current index in names list
  137. matched = true;
  138. position = name.indexOf(profile); // return the position of the element
  139. break;
  140. } else
  141. {
  142. matched = false;
  143.  
  144. }
  145. }if (matched ==false){
  146. System.out.println(">>>>>>>User Not Found!<<<<<<<");
  147. }
  148. if (matched == true)
  149. {
  150. System.out.println("New name: ");
  151. newName = scr.nextLine(); //getting the new name input from the user
  152. name.set(position, newName); // Set newName as new data for name and the index is equal to the value of position
  153. System.out.println("New address: ");
  154. newAddress = scr.nextLine(); //getting the new address input from the user
  155. address.set(position, newAddress);
  156.  
  157. }
  158. }
  159.  
  160. static void display()
  161. {
  162. if (name.isEmpty()){
  163. System.out.println(">>>>>>>Sorry No Data to Display<<<<<<<");
  164. }else{
  165. System.out.println("------Show accounts for all users!------");
  166. for (int i = 0; i < name.size(); i++) //uses for loop to scan the index in the arraylist
  167. {
  168. System.out.println("Name: " + name.get(i)); // i is the index of the array name
  169. System.out.println("Address: " + address.get(i)); //get, is a method in arraylist
  170. System.out.println("Previous reading: " + prevMreading.get(i));
  171. System.out.println("Current reading: " + currMreading.get(i));
  172. System.out.println("Current consumption: " + consumption.get(i));
  173. System.out.println("Total payment due: Php " + Dues.get(i));
  174. System.out.println("");
  175. }
  176. }
  177. }
  178.  
  179. static void search()
  180. {
  181. int position = 0; //initialize postion to 0 value
  182. String names;
  183. boolean matched = false; //initialize matched to false value
  184. System.out.println("------Try looking for an account here!------");
  185. System.out.println("Name: ");
  186. names = scr.nextLine();
  187. for (String profile : name) { // uses for each loop to run from index 0 to the last until matched == true
  188. if (profile.contains(names)) //check if the element is present in the array and exactly the same as the current index in names list
  189. {
  190. matched = true; //change value matched to true
  191. position = name.indexOf(profile); //position = index where the profile is
  192. break;
  193. } else
  194. {
  195. matched = false; // if not matched keep to false
  196.  
  197. }
  198. }if (matched == false){
  199. System.out.println(">>>>>>>User Not Found!<<<<<<<");
  200. }
  201.  
  202. if (matched == true) {
  203. System.out.println("");
  204. System.out.println("Name: " + name.get(position)); //print where the index of the searched element
  205. System.out.println("Address: " + address.get(position));
  206. System.out.println("Previous reading: " + prevMreading.get(position));
  207. System.out.println("Current reading: " + currMreading.get(position));
  208. System.out.println("Current consumption: " + consumption.get(position));
  209. System.out.println("Total payment due: Php " + Dues.get(position));
  210. System.out.println("");
  211.  
  212. }
  213. }
  214. static void delete()
  215. {
  216. int position = 0; //initialize postion to 0 value
  217. String names;
  218. boolean matched = false; //initialize matched to false value
  219. System.out.println(">>>>>>>Remove an Account!<<<<<<<");
  220. System.out.println("Name: ");
  221. names = scr.nextLine();
  222. for (String profile : name) { // uses for each loop to run from index 0 to the last until matched == true
  223. if (profile.contains(names)) //check if the element is present in the array and exactly the same as the current index in names list
  224. {
  225. matched = true; //change value matched to true
  226. position = name.indexOf(profile); //position = index where the profile is
  227. break;
  228. } else
  229. {
  230. matched = false; // if not matched keep to false
  231.  
  232. }
  233. }if (matched == false){
  234. System.out.println(">>>>>>>User Not Found!<<<<<<<");
  235. }
  236.  
  237. if (matched == true) {
  238. System.out.println(">>>>>>>Account Removed!<<<<<<<");
  239. name.remove(position); //print where the index of the searched element
  240. address.remove(position);
  241. prevMreading.remove(position);
  242. currMreading.remove(position);
  243. consumption.remove(position);
  244. Dues.remove(position);
  245.  
  246.  
  247. }
  248. }
  249.  
  250.  
  251. static int exit() { //exit method
  252. return 0;
  253. }
  254.  
  255. }
  256.  
Advertisement
Add Comment
Please, Sign In to add comment