Advertisement
grhey

WaterBillingSystem V5

Jun 28th, 2022 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.05 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. 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("[0] Exit");
  56. System.out.print("Enter Your Choice: ");
  57.  
  58. }
  59.  
  60. static void add() { //method to add data to the arraylist
  61. String Name, Address;
  62. double prev, curr,consumed, due, sub = 0.0;
  63. int rate;
  64. char overdue,quarter;
  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. System.out.println("-------Is the payment overdue?-------");
  99. System.out.println("[press [y] if yes and any letter if no]");
  100. System.out.println("[Note: Lower Case Only!]");
  101. overdue=scr.next().charAt(0);
  102. if (overdue == 'y'){
  103. due = sub + 200;
  104. }else {
  105. due=sub * 1;
  106. }
  107. System.out.println("-------Is it quarter of the year?--------");
  108. System.out.println("[press [y] if yes and any letter if no]");
  109. System.out.println("[Note: Lower Case Only!]");
  110. quarter = scr.next().charAt(0);
  111. if(quarter == 'y'){
  112. int yearser;
  113. System.out.println("how many years in using the service?");
  114. yearser = scr.nextInt(); //getting input from the user
  115. if (yearser >= 15 ) {
  116. due = sub - (sub * .15);
  117.  
  118. } else {
  119. due = sub;
  120. }
  121.  
  122. }
  123. Dues.add(due); //insert the element to the arraylist
  124. }
  125.  
  126. static void update() { //function will update name and address only
  127. String namess, newName, newAddress;
  128. System.out.println("------Update a current account------!");
  129. System.out.println("Enter the name of the current Account: ");
  130. namess = scr.nextLine();
  131. int position = 0; //initialize postion to 0 value
  132. boolean matched = false; //initialize matched to false value
  133. for (String profile : Defense_WaterBilling.name) { // uses for each loop to get the element,The loop goes through each index of array name
  134. if (profile.contains(namess)) { //check if the element is present in the array and exactly the same as the current index in names list
  135. matched = true;
  136. position = Defense_WaterBilling.name.indexOf(profile); // return the position of the element
  137. break;
  138. } else
  139. {
  140. matched = false;
  141.  
  142. }
  143. }if (matched ==false){
  144. System.out.println("No data found !!!!!");
  145. }
  146. if (matched == true)
  147. {
  148. System.out.println("New name: ");
  149. newName = scr.nextLine(); //getting the new name input from the user
  150. Defense_WaterBilling.name.set(position, newName); // Set newName as new data for name and the index is equal to the value of position
  151. System.out.println("New address: ");
  152. newAddress = scr.nextLine(); //getting the new address input from the user
  153. address.set(position, newAddress);
  154.  
  155. }
  156. }
  157.  
  158. static void display()
  159. {
  160. System.out.println("------Show accounts for all users!------");
  161. for (int i = 0; i < name.size(); i++) //uses for loop to scan the index in the arraylist
  162. {
  163. System.out.println("Name: " + name.get(i)); // i is the index of the array name
  164. System.out.println("Address: " + address.get(i)); //get, is a method in arraylist
  165. System.out.println("Previous reading: " + prevMreading.get(i));
  166. System.out.println("Current reading: " + currMreading.get(i));
  167. System.out.println("Current consumption: " + consumption.get(i));
  168. System.out.println("Total payment due: " + Dues.get(i));
  169. System.out.println("");
  170.  
  171.  
  172. }
  173. }
  174.  
  175. static void search()
  176. {
  177. int position = 0; //initialize postion to 0 value
  178. String names;
  179. boolean matched = false; //initialize matched to false value
  180. System.out.println("------Try looking for an account here!------");
  181. System.out.println("Name: ");
  182. names = scr.nextLine();
  183. for (String profile : Defense_WaterBilling.name) { // uses for each loop to run from index 0 to the last until matched == true
  184. if (profile.contains(names)) //check if the element is present in the array and exactly the same as the current index in names list
  185. {
  186. matched = true; //change value matched to true
  187. position = Defense_WaterBilling.name.indexOf(profile); //position = index where the profile is
  188. break;
  189. } else
  190. {
  191. matched = false; // if not matched keep to false
  192.  
  193. }
  194. }if (matched == false){
  195. System.out.println("Not Found!!!!");
  196. }
  197.  
  198. if (matched == true) {
  199. System.out.println("");
  200. System.out.println("Name: " + Defense_WaterBilling.name.get(position)); //print where the index of the searched element
  201. System.out.println("Address: " + Defense_WaterBilling.address.get(position));
  202. System.out.println("Previous reading: " + prevMreading.get(position));
  203. System.out.println("Current reading: " + currMreading.get(position));
  204. System.out.println("Current consumption: " + consumption.get(position));
  205. System.out.println("Total payment due: " + Dues.get(position));
  206. System.out.println("");
  207.  
  208. }
  209. }
  210.  
  211.  
  212. static int exit() { //exit method
  213. return 0;
  214. }
  215.  
  216. }
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement