grhey

Water Billing System V6

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