Simple java solution

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mizzy
    New Member
    • Apr 2017
    • 2

    Simple java solution

    In this simple java program the user is asked for
    account no , current meter reading and previous meter reading and then calculates the total amount using the electricity usage for that account. When the user enter
    0 as the account no the program stop by showing the total
    amount for all accounts.

    The program is already doing it but there is a slight mistake which I could not figure out.

    1- When the program starts if user enter 0 as account no
    The program should show the message. No account has been processed. But while entering if user enters 0 as account no it should add up all the previous account amounts and show.

    attached is the code snippet and the program sample
    out put exactly how it should do..

    A help is highly appreciated.

    Thanks

    Code:
    package e.power.bhd;
    
    import java.util.Scanner;
    
    public class EPowerBhd {
    
        public static void main(String[] args) {
            int accountNum;
            double cMeter;
            double pMeter;
            double eUsage;
            double totalDueAmount = 0.0;
            double Tot = 0.0;
            Scanner input = new Scanner(System.in);
            do {
    
                System.out.print("Enter Account number (0 to stop ) : ");
                accountNum = input.nextInt();
                if (accountNum != 0) {
    
                    System.out.print("Current meter reading : ");
                    cMeter = input.nextDouble();
                    System.out.print("Previous meter reading : ");
                    pMeter = input.nextDouble();
                    eUsage = cMeter - pMeter;
                    System.out.print("Electricity Usage(in kWh) : " + eUsage);
                    System.out.println();
    
                    if (eUsage >= 1 && eUsage <= 200) {
                        totalDueAmount = 0.218 * eUsage;
                    } else if (eUsage > 200 && eUsage <= 300) {
                        totalDueAmount = 0.334 * eUsage;
                    } else if (eUsage > 300 && eUsage <= 600) {
                        totalDueAmount = 0.516 * eUsage;
                    } else if (eUsage > 600 && eUsage <= 900) {
                        totalDueAmount = 0.546 * eUsage;
                    } else {
                        totalDueAmount = 0.571 * eUsage;
                    }
                    System.out.println();
                    System.out.println("Amount number : " + accountNum + "," + " your charge is " + "RM" + totalDueAmount);
    
                    System.out.println();
    
                } else {
                    System.out.println("No account has been processed.");
                }
    
            } while (accountNum != 0);
    
            Tot += totalDueAmount;
            System.out.println("Total Amount collected  = " + "RM" + Tot);
            System.out.println("________________________________");
            System.out.println("--- DONE ----");
            System.out.println("________________________________");
        }
    
    }


  • mizzy
    New Member
    • Apr 2017
    • 2

    #2




    attached sample out put and program detail

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #3
      You could just do it by duplicating the read-statement (first solution) or better by using a boolean flag to remember if the user has entered an account number or not (second solution). This would ease readability and has no repeated code.

      First solution:
      Code:
      Scanner input = new Scanner(System.in);
      System.out.print("Enter Account number (0 to stop ) : ");
      int accountNum = input.nextInt();
      if (accountNum == 0)
      {
          System.out.println("No account has been processed.");
      }
      else
      {
          do {
             ... // do all your calculation stuff here: line 21 to 43
             System.out.print("Enter Account number (0 to stop ) : ");
             accountNum = input.nextInt();
          } while (accountNum != 0);
      
          ... // do all your statistic stuff here: line 51 to 55
      }
      Second solution:
      Code:
      Scanner input = new Scanner(System.in);
      boolean isAccountNumberEntered = false;
      while (true) {
        System.out.print("Enter Account number (0 to stop ) : ");
        int accountNum = input.nextInt();
        if (acountNum == 0)
        {
          if (! isAccountNumberEntered )
          {
            System.out.println("No account has been processed.");
          }
          break;
        }
        else
        {
          isAccountNumberEntered = true;
        }
      
        ... // do all your calculation stuff here: line 21 to 43
      
        ... // do all your statistic stuff here: line 51 to 55
      }

      Comment

      Working...