grhey

waterbilling

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