Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *This program, Water Billing System, will figure out how much a customer pays based on a particular reading.
- * As well as creating or updating an account, they can also perform a search using it's feature.
- */
- import java.util.Scanner; // for getting user input
- import java.util.ArrayList; //arraylist stores data and uses non primitive data type
- public class Defense_WaterBilling { //class
- static ArrayList<String> name = new ArrayList<String> (); // creating object for arraylist
- static ArrayList<String> address = new ArrayList<String> (); //setting arraylist as a global variable to access in the whole parts of the program
- static ArrayList<Double> prevMreading = new ArrayList<Double> (); //Uses wrapper classes
- static ArrayList<Double> currMreading = new ArrayList<Double> ();
- static ArrayList<Double> Dues = new ArrayList<Double> ();
- static ArrayList<Double> consumption = new ArrayList<Double> ();
- static Scanner scr = new Scanner(System.in); // initializing the scanner as a global variable also
- public static void main(String[] args) { //the main method includes the while loop for looping the main menu and uses switch statement
- int mainMenu = -1; //initialize mainMenu = -1
- while (mainMenu != 0) { //while loop for looping the menu until the user press exit
- menu(); //calling the menu user defined method
- mainMenu = scr.nextInt(); //getting user choice
- scr.nextLine();
- switch (mainMenu) {
- case 0:
- exit(); //calling the userdefined methods
- break;
- case 1:
- add();//calling the userdefined methods
- break;
- case 2:
- update(); //calling the userdefined methods
- break;
- case 3:
- display(); //calling the userdefined methods
- break;
- case 4:
- search(); //calling the userdefined methods
- break;
- default: System.out.println("Out of range");
- }
- }
- }
- static void menu() { //the menu output
- System.out.println("-------[Water Billing System]-------");
- System.out.println("[1] Create a new user account.");
- System.out.println("[2] Update a current account");
- System.out.println("[3] Show accounts for all users.");
- System.out.println("[4] Try looking for an account here.");
- System.out.println("[0] Exit");
- System.out.print("Enter Your Choice: ");
- }
- static void add() { //method to add data to the arraylist
- String Name, Address;
- double prev, curr,consumed, due, sub = 0.0;
- int rate;
- char overdue,quarter;
- System.out.println("------Create a new user account!------");
- System.out.println("Name: ");
- Name = scr.nextLine();
- Defense_WaterBilling.name.add(Name); //insert the element to the arraylist
- System.out.println("Address: ");
- Address = scr.nextLine(); //getting String input from the user
- Defense_WaterBilling.address.add(Address); //insert the element to the arraylist
- System.out.println("Previous reading: ");
- prev = scr.nextDouble(); //getting input from the user
- prevMreading.add(prev); //insert the element to the arraylist
- System.out.println("Current reading: ");
- curr = scr.nextDouble(); //getting input from the user
- currMreading.add(curr); //insert the element to the arraylist
- System.out.println("[1] Residential");
- System.out.println("[2] Business");
- rate = scr.nextInt(); //getting input from the user
- consumed = curr - prev;
- consumption.add(consumed); //insert the element to the arraylist
- if (rate == 1) { //nested if to get the sub value
- if (consumed > 10) {
- sub = 200 + ((consumed - 10) * 30);
- }
- if (consumed <= 10) {
- sub = 200;
- }
- } else if (rate == 2) {
- if (consumed > 20) {
- sub = 500 + ((consumed - 20) * 50);
- }
- if (consumed <= 20) {
- sub = 500;
- }
- }
- System.out.println("-------Is the payment overdue?-------");
- System.out.println("[press [y] if yes and any letter if no]");
- System.out.println("[Note: Lower Case Only!]");
- overdue=scr.next().charAt(0);
- if (overdue == 'y'){
- due = sub + 200;
- }else {
- due=sub * 1;
- }
- System.out.println("-------Is it quarter of the year?--------");
- System.out.println("[press [y] if yes and any letter if no]");
- System.out.println("[Note: Lower Case Only!]");
- quarter = scr.next().charAt(0);
- if(quarter == 'y'){
- int yearser;
- System.out.println("how many years in using the service?");
- yearser = scr.nextInt(); //getting input from the user
- if (yearser >= 15 ) {
- due = sub - (sub * .15);
- } else {
- due = sub;
- }
- }
- Dues.add(due); //insert the element to the arraylist
- }
- static void update() { //function will update name and address only
- String namess, newName, newAddress;
- System.out.println("------Update a current account------!");
- System.out.println("Enter the name of the current Account: ");
- namess = scr.nextLine();
- int position = 0; //initialize postion to 0 value
- boolean matched = false; //initialize matched to false value
- for (String profile : Defense_WaterBilling.name) { // uses for each loop to get the element,The loop goes through each index of array name
- if (profile.contains(namess)) { //check if the element is present in the array and exactly the same as the current index in names list
- matched = true;
- position = Defense_WaterBilling.name.indexOf(profile); // return the position of the element
- break;
- } else
- {
- matched = false;
- }
- }if (matched ==false){
- System.out.println("No data found !!!!!");
- }
- if (matched == true)
- {
- System.out.println("New name: ");
- newName = scr.nextLine(); //getting the new name input from the user
- Defense_WaterBilling.name.set(position, newName); // Set newName as new data for name and the index is equal to the value of position
- System.out.println("New address: ");
- newAddress = scr.nextLine(); //getting the new address input from the user
- address.set(position, newAddress);
- }
- }
- static void display()
- {
- System.out.println("------Show accounts for all users!------");
- for (int i = 0; i < name.size(); i++) //uses for loop to scan the index in the arraylist
- {
- System.out.println("Name: " + name.get(i)); // i is the index of the array name
- System.out.println("Address: " + address.get(i)); //get, is a method in arraylist
- System.out.println("Previous reading: " + prevMreading.get(i));
- System.out.println("Current reading: " + currMreading.get(i));
- System.out.println("Current consumption: " + consumption.get(i));
- System.out.println("Total payment due: " + Dues.get(i));
- System.out.println("");
- }
- }
- static void search()
- {
- int position = 0; //initialize postion to 0 value
- String names;
- boolean matched = false; //initialize matched to false value
- System.out.println("------Try looking for an account here!------");
- System.out.println("Name: ");
- names = scr.nextLine();
- for (String profile : Defense_WaterBilling.name) { // uses for each loop to run from index 0 to the last until matched == true
- if (profile.contains(names)) //check if the element is present in the array and exactly the same as the current index in names list
- {
- matched = true; //change value matched to true
- position = Defense_WaterBilling.name.indexOf(profile); //position = index where the profile is
- break;
- } else
- {
- matched = false; // if not matched keep to false
- }
- }if (matched == false){
- System.out.println("Not Found!!!!");
- }
- if (matched == true) {
- System.out.println("");
- System.out.println("Name: " + Defense_WaterBilling.name.get(position)); //print where the index of the searched element
- System.out.println("Address: " + Defense_WaterBilling.address.get(position));
- System.out.println("Previous reading: " + prevMreading.get(position));
- System.out.println("Current reading: " + currMreading.get(position));
- System.out.println("Current consumption: " + consumption.get(position));
- System.out.println("Total payment due: " + Dues.get(position));
- System.out.println("");
- }
- }
- static int exit() { //exit method
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement