473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Simple java solution

2
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

Expand|Select|Wrap|Line Numbers
  1. package e.power.bhd;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class EPowerBhd {
  6.  
  7.     public static void main(String[] args) {
  8.         int accountNum;
  9.         double cMeter;
  10.         double pMeter;
  11.         double eUsage;
  12.         double totalDueAmount = 0.0;
  13.         double Tot = 0.0;
  14.         Scanner input = new Scanner(System.in);
  15.         do {
  16.  
  17.             System.out.print("Enter Account number (0 to stop ) : ");
  18.             accountNum = input.nextInt();
  19.             if (accountNum != 0) {
  20.  
  21.                 System.out.print("Current meter reading : ");
  22.                 cMeter = input.nextDouble();
  23.                 System.out.print("Previous meter reading : ");
  24.                 pMeter = input.nextDouble();
  25.                 eUsage = cMeter - pMeter;
  26.                 System.out.print("Electricity Usage(in kWh) : " + eUsage);
  27.                 System.out.println();
  28.  
  29.                 if (eUsage >= 1 && eUsage <= 200) {
  30.                     totalDueAmount = 0.218 * eUsage;
  31.                 } else if (eUsage > 200 && eUsage <= 300) {
  32.                     totalDueAmount = 0.334 * eUsage;
  33.                 } else if (eUsage > 300 && eUsage <= 600) {
  34.                     totalDueAmount = 0.516 * eUsage;
  35.                 } else if (eUsage > 600 && eUsage <= 900) {
  36.                     totalDueAmount = 0.546 * eUsage;
  37.                 } else {
  38.                     totalDueAmount = 0.571 * eUsage;
  39.                 }
  40.                 System.out.println();
  41.                 System.out.println("Amount number : " + accountNum + "," + " your charge is " + "RM" + totalDueAmount);
  42.  
  43.                 System.out.println();
  44.  
  45.             } else {
  46.                 System.out.println("No account has been processed.");
  47.             }
  48.  
  49.         } while (accountNum != 0);
  50.  
  51.         Tot += totalDueAmount;
  52.         System.out.println("Total Amount collected  = " + "RM" + Tot);
  53.         System.out.println("________________________________");
  54.         System.out.println("--- DONE ----");
  55.         System.out.println("________________________________");
  56.     }
  57.  
  58. }
  59.  
  60.  



Apr 13 '17 #1
2 1121
chaarmann
785 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. Scanner input = new Scanner(System.in);
  2. System.out.print("Enter Account number (0 to stop ) : ");
  3. int accountNum = input.nextInt();
  4. if (accountNum == 0)
  5. {
  6.     System.out.println("No account has been processed.");
  7. }
  8. else
  9. {
  10.     do {
  11.        ... // do all your calculation stuff here: line 21 to 43
  12.        System.out.print("Enter Account number (0 to stop ) : ");
  13.        accountNum = input.nextInt();
  14.     } while (accountNum != 0);
  15.  
  16.     ... // do all your statistic stuff here: line 51 to 55
  17. }
Second solution:
Expand|Select|Wrap|Line Numbers
  1. Scanner input = new Scanner(System.in);
  2. boolean isAccountNumberEntered = false;
  3. while (true) {
  4.   System.out.print("Enter Account number (0 to stop ) : ");
  5.   int accountNum = input.nextInt();
  6.   if (acountNum == 0)
  7.   {
  8.     if (! isAccountNumberEntered )
  9.     {
  10.       System.out.println("No account has been processed.");
  11.     }
  12.     break;
  13.   }
  14.   else
  15.   {
  16.     isAccountNumberEntered = true;
  17.   }
  18.  
  19.   ... // do all your calculation stuff here: line 21 to 43
  20.  
  21.   ... // do all your statistic stuff here: line 51 to 55
  22. }
  23.  
Apr 24 '17 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Nuno Silva | last post by:
Hello, I'm starting to learn about java networking. What I want to do is for now, just make a server that listens to connections through a port, and accepts it's connections from the client. ...
1
by: disha manikumar | last post by:
Simple java code to read an excel sheet and extract only few fields by treating the excel sheet as a flat file. Can anyone suggest a simple java code for this?
1
by: nela | last post by:
Hello everyone, from Croatia ! I am designing a site for a Serbian client, and the english page is ok, but the serbian page needs to be cyrillic. It was no problem for Photoshop design, but I...
2
JavaStudent07
by: JavaStudent07 | last post by:
H:\>c:\j2sdk1.4.1\bin\javac.exe BubbleSort.java H:\>c:\j2sdk1.4.1\bin\java.exe BubbleSort Please input the name of the file from which the data will be acquired. Be sure to use the entire path....
3
by: altafur | last post by:
hi, i am using java with hibernate . i want to run a hibernate order by query thru java. i have written the query in hibernate.hbm.xml file. the query is as follows: query: SELECT distinct...
16
by: feda | last post by:
Hi everybody, I am new to java I installed netbeans 6.0.1 I tried running the following simple program but it didnor work??? import java.util.*; public class Hello { public...
5
by: najmi | last post by:
hai.. i hava use FullCalendar in my application.i try to reload event into the calendar but it doesn`t work.here is my code <%@ page import="java.util.HashMap;" %> <%@ page...
8
by: Youness Alma | last post by:
This is example of my page: <html> <head> <JAVA> </JAVA> <title> my title </title> </head> <body> </body> </html>
2
by: jacobr933 | last post by:
im am new to programing and currently looking to learn how to program with java but i need a simple complier that uses java and that i can learn on to program and run apps i have no experance so i...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.