grhey

FinalsWaterBilling

Jun 23rd, 2022
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 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> ();
  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(); //calling the userdefined methods
  26. case 1:
  27. add();
  28. break;//calling the userdefined methods
  29. case 2:
  30. update();
  31. break;//calling the userdefined methods
  32. case 3:
  33. display();
  34. break;//calling the userdefined methods
  35. case 4:
  36. search();
  37. break;//calling the userdefined methods
  38. default: System.out.println("Out of range");
  39. }
  40. }
  41. }
  42.  
  43. static void menu() { //the menu output
  44. System.out.println("-------[Water Billing System]-------");
  45. System.out.println("[1] Create a new user account.");
  46. System.out.println("[2] Update a current account");
  47. System.out.println("[3] Show accounts for all users.");
  48. System.out.println("[4] Try looking for an account here.");
  49. System.out.println("[0] Exit");
  50. System.out.println("Enter Your Choice");
  51.  
  52. }
  53.  
  54. static void add() {
  55. String name, address;
  56. double prev, curr, due = 0.0, sub = 0.0;
  57. int rate;
  58. System.out.println("------Create a new user account!------");
  59. System.out.println("Name: ");
  60. name = scr.nextLine();
  61. Defense_WaterBilling.name.add(name);
  62. System.out.println("Address: ");
  63. address = scr.nextLine();
  64. Defense_WaterBilling.address.add(address);
  65. System.out.println("Previous reading: ");
  66. prev = scr.nextDouble();
  67. prevMreading.add(prev);
  68. System.out.println("Current reading: ");
  69. curr = scr.nextDouble();
  70. currMreading.add(curr);
  71. System.out.println("[1] Residential");
  72. System.out.println("[2] Business");
  73. rate = scr.nextInt();
  74. double consumed = curr - prev;
  75. consumption.add(consumed);
  76. if (rate == 1) { //nested if to get the sub value
  77. if (consumed > 10) {
  78. sub = 200 + ((consumed - 10) * 30);
  79. }
  80. if (consumed <= 10) {
  81. sub = 200;
  82. }
  83. } else if (rate == 2) {
  84. if (consumed > 20) {
  85. sub = 500 + ((consumed - 20) * 50);
  86. }
  87. if (consumed <= 20) {
  88. sub = 500;
  89. }
  90. }
  91. int yearser;
  92. System.out.println("how many years in using the service?");
  93. yearser = scr.nextInt();
  94. if (yearser >= 15 ) {
  95. due = sub - (sub * (15/100));
  96. discount.add("15% promo discount applied!");
  97. } else {
  98. due = sub;
  99. discount.add("No promo discount appllied!");
  100. }
  101. Dues.add(due);
  102. }
  103.  
  104. static void update() {
  105. String name, newName, newAddress;
  106. System.out.println("------Update a current account------!");
  107. System.out.println("Name: ");
  108. name = scr.nextLine();
  109. int position = 0;
  110. boolean matched = false;
  111. for (String profile : Defense_WaterBilling.name) {
  112. if (profile.contains(name)) {
  113. matched = true;
  114. position = Defense_WaterBilling.name.indexOf(profile);
  115. break;
  116. } else {
  117. matched = false;
  118. }
  119. }
  120. if (matched == true) {
  121. int modify;
  122. System.out.println("[1] Update name");
  123. System.out.println("[2] Update address");
  124. modify = scr.nextInt();
  125. scr.nextLine();
  126. switch (modify) {
  127. case 1:
  128. System.out.println("New name: ");
  129. newName = scr.nextLine();
  130. Defense_WaterBilling.name.set(position, newName);
  131. break;
  132. case 2:
  133. System.out.println("New address: ");
  134. newAddress = scr.nextLine();
  135. address.set(position, newAddress);
  136. break;
  137. default:
  138. System.out.println("Unknown command!");
  139. break;
  140. }
  141. } else {
  142. System.out.println("No data found");
  143. }
  144. }
  145.  
  146. static void display() {
  147. System.out.println("------Show accounts for all users!------");
  148. for (int i = 0; i < name.size(); i++) {
  149. System.out.println("Name: " + name.get(i));
  150. System.out.println("Address: " + address.get(i));
  151. System.out.println("Previous reading: " + prevMreading.get(i));
  152. System.out.println("Current reading: " + currMreading.get(i));
  153. System.out.println("Current consumption: " + consumption.get(i));
  154. System.out.println("Total payment due: " + Dues.get(i));
  155. System.out.println("Promo: " + discount.get(i));
  156. System.out.println("");
  157.  
  158.  
  159. }
  160. }
  161.  
  162. static void search() {
  163. int position = 0, searchBy;
  164. String name, address;
  165. boolean matched = false;
  166. System.out.println("------Try looking for an account here!------");
  167. System.out.println("[1] By name");
  168. System.out.println("[2] By address");
  169. searchBy = scr.nextInt();
  170. scr.nextLine();
  171. switch (searchBy) {
  172. case 1:
  173. System.out.println("Name: ");
  174. name = scr.nextLine();
  175. for (String profile : Defense_WaterBilling.name) {
  176. if (profile.contains(name)) {
  177. matched = true;
  178. position = Defense_WaterBilling.name.indexOf(profile);
  179. break;
  180. } else {
  181. matched = false;
  182. }
  183. } break;
  184. case 2:
  185. System.out.println("Address: ");
  186. address = scr.nextLine();
  187. for (String profile : Defense_WaterBilling.address) {
  188. if (profile.contains(address)) {
  189. matched = true;
  190. position = Defense_WaterBilling.address.indexOf(profile);
  191. break;
  192. } else {
  193. matched = false;
  194. }
  195. } break;
  196. default:
  197. System.out.println("Unknown command");
  198. break;
  199. }
  200.  
  201. if (matched == true) {
  202. System.out.println("");
  203. System.out.println("Name: " + Defense_WaterBilling.name.get(position));
  204. System.out.println("Address: " + Defense_WaterBilling.address.get(position));
  205. System.out.println("Previous reading: " + prevMreading.get(position));
  206. System.out.println("Current reading: " + currMreading.get(position));
  207. System.out.println("Current consumption: " + consumption.get(position));
  208. System.out.println("Total payment due: " + Dues.get(position));
  209. System.out.println("Promo: " + discount.get(position));
  210. System.out.println("");
  211.  
  212. }
  213. }
  214.  
  215. static int exit() {
  216. return 0;
  217. }
  218.  
  219. }
  220.  
Advertisement
Add Comment
Please, Sign In to add comment