grhey

Untitled

Jun 19th, 2022
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.03 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3. import java.util.Random;
  4.  
  5. public class FinalProject {
  6.  
  7. static ArrayList<String> names = new ArrayList<String> ();
  8. static ArrayList<String> addresses = new ArrayList<String> ();
  9. static ArrayList<Double> prevReadings = new ArrayList<Double> ();
  10. static ArrayList<Double> currReadings = new ArrayList<Double> ();
  11. static ArrayList<Double> paymentDues = new ArrayList<Double> ();
  12. static ArrayList<Integer> since = new ArrayList<Integer> ();
  13. static ArrayList<Double> consumptions = new ArrayList<Double> ();
  14. static ArrayList<String> discounts = new ArrayList<String> ();
  15. static Scanner input = new Scanner(System.in);
  16.  
  17. public static void main(String[] args) {
  18. Random months = new Random();
  19. int currentMonth = months.nextInt((12 - 1) + 1)+1;
  20. int mainMenu = -1;
  21. final int month = currentMonth;
  22. System.out.println("Current month: " + currentMonth);
  23. while (mainMenu != 0) {
  24. menu();
  25. mainMenu = input.nextInt();
  26. input.nextLine();
  27. switch (mainMenu) {
  28. case 0:
  29. exit();
  30. break;
  31. case 1:
  32. add(month);
  33. break;
  34. case 2:
  35. update();
  36. break;
  37. case 3:
  38. display();
  39. break;
  40. case 4:
  41. search();
  42. break;
  43. case 8:
  44. System.out.println("Current month: " + month);
  45. break;
  46. default:
  47. System.out.println("Out of range");
  48. main(args);
  49. break;
  50. }
  51. }
  52. }
  53.  
  54. static void menu() {
  55. System.out.println("Water Billing System");
  56. System.out.println("[1] Add new account");
  57. System.out.println("[2] Update existing account");
  58. System.out.println("[3] Display all accounts");
  59. System.out.println("[4] Search for account");
  60. System.out.println("[0] Exit");
  61. System.out.print(">>> ");
  62. }
  63.  
  64. static void add(int quarter) {
  65. Random rng = new Random();
  66. String name, address;
  67. double prev, curr, due = 0.0, sub = 0.0;
  68. int rate;
  69. System.out.println("Add new user!");
  70. System.out.println("Name: ");
  71. name = input.nextLine();
  72. names.add(name);
  73. System.out.println("Address: ");
  74. address = input.nextLine();
  75. addresses.add(address);
  76. System.out.println("Previous reading: ");
  77. prev = input.nextDouble();
  78. prevReadings.add(prev);
  79. System.out.println("Current reading: ");
  80. curr = input.nextDouble();
  81. currReadings.add(curr);
  82. System.out.println("[1] Residential");
  83. System.out.println("[2] Business");
  84. System.out.print(">>> ");
  85. rate = input.nextInt();
  86. double consumed = curr - prev;
  87. consumptions.add(consumed);
  88. if (rate == 1) {
  89. if (consumed > 10) {
  90. sub = 200 + ((consumed - 10) * 30);
  91. }
  92. if (consumed <= 10) {
  93. sub = 200;
  94. }
  95. } else if (rate == 2) {
  96. if (consumed > 20) {
  97. sub = 500 + ((consumed - 20) * 50);
  98. }
  99. if (consumed <= 20) {
  100. sub = 500;
  101. }
  102. }
  103. int yearInService = rng.nextInt(26);
  104. since.add(yearInService);
  105. if (yearInService >= 15 && (quarter == 1 || quarter == 4 || quarter == 7 || quarter == 10)) {
  106. due = sub - (sub * (15/100));
  107. discounts.add("15% promo discount applied!");
  108. } else {
  109. due = sub;
  110. discounts.add("No promo discount appllied!");
  111. }
  112. paymentDues.add(due);
  113. }
  114.  
  115. static void update() {
  116. String name, newName, newAddress;
  117. System.out.println("Update account!");
  118. System.out.println("Name: ");
  119. name = input.nextLine();
  120. int position = 0;
  121. boolean matched = false;
  122. for (String profile : names) {
  123. if (profile.contains(name)) {
  124. matched = true;
  125. position = names.indexOf(profile);
  126. break;
  127. } else {
  128. matched = false;
  129. }
  130. }
  131. if (matched == true) {
  132. int modify;
  133. System.out.println("[1] Update name");
  134. System.out.println("[2] Update address");
  135. modify = input.nextInt();
  136. input.nextLine();
  137. switch (modify) {
  138. case 1:
  139. System.out.println("New name: ");
  140. newName = input.nextLine();
  141. names.set(position, newName);
  142. break;
  143. case 2:
  144. System.out.println("New address: ");
  145. newAddress = input.nextLine();
  146. addresses.set(position, newAddress);
  147. break;
  148. default:
  149. System.out.println("Unknown command!");
  150. break;
  151. }
  152. } else {
  153. System.out.println("No data found");
  154. }
  155. }
  156.  
  157. static void display() {
  158. System.out.println("All accounts!");
  159. for (int i = 0; i < names.size(); i++) {
  160. System.out.println("Name: " + names.get(i));
  161. System.out.println("Address: " + addresses.get(i));
  162. System.out.println("Previous reading: " + prevReadings.get(i));
  163. System.out.println("Current reading: " + currReadings.get(i));
  164. System.out.println("Current consumption: " + consumptions.get(i));
  165. System.out.println("Total payment due: " + paymentDues.get(i));
  166.  
  167. }
  168. }
  169.  
  170. static void search() {
  171. int position = 0, searchBy;
  172. String name, address;
  173. boolean matched = false;
  174. System.out.println("Search an account!");
  175. System.out.println("[1] By name");
  176. System.out.println("[2] By address");
  177. System.out.print(">>> ");
  178. searchBy = input.nextInt();
  179. input.nextLine();
  180. if (searchBy == 1) {
  181. System.out.print("Name: ");
  182. name = input.nextLine();
  183. for (String profile : names) {
  184. if (profile.contains(name)) {
  185. matched = true;
  186. position = names.indexOf(profile);
  187. break;
  188. } else {
  189. matched = false;
  190. }
  191. }
  192. } else if (searchBy == 2) {
  193. System.out.print("Address: ");
  194. address = input.nextLine();
  195. for (String profile : addresses) {
  196. if (profile.contains(address)) {
  197. matched = true;
  198. position = addresses.indexOf(profile);
  199. break;
  200. } else {
  201. matched = false;
  202. }
  203. }
  204. } else {
  205. System.out.println("Unknown command");
  206. }
  207.  
  208. if (matched == true) {
  209. System.out.println(
  210. "Name: " + names.get(position) +
  211. "\nAddress: " + addresses.get(
  212. position) +
  213. "\nCustomer since: " + (2022 -
  214. since.get(position)) +
  215. "\nPrevious reading: " + prevReadings.get(position) +
  216. "\nCurrent reading: " + currReadings.get(position) +
  217. "\nCurrent consumption: " + consumptions.get(position) +
  218. "\nTotal payment due: " + paymentDues.get(position) +
  219. "\nPromo: " + discounts.get(position) +
  220. "\n"
  221. );
  222. }
  223. }
  224.  
  225. static int exit() {
  226. return 0;
  227. }
  228.  
  229. }
  230.  
Advertisement
Add Comment
Please, Sign In to add comment