CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2016
    Location
    JRE 1.8
    Posts
    2

    need some tips first java project

    Hey there everyone, im new here, and new to coding. ive been learning java on udemy, and decided to try my first project.

    Basically, im just looking for tips and ideas of how i could have done this more effeciently. please dont laugh at me as its really my first attempt at doing something with what ive learned so far, and im still at the beggining of the course

    The goal of the code is to help user calculate electric bill, based on the reading of the numbers on the electric clock of Kilo Watts used.
    thats done by taking reading number of current KW used, and subtracting from it the reading of previous month.
    then multiplying the new number by the rate of cents per KW.
    Theres also an option for a deduction of 1/2 price on the first 400kw used each month. (and subtracting 18% of tax from the deduction value)
    P.S. the actual currency is in NIS, (New Israeli Shekel).

    HERE GOES:

    Heres the Calculation Class
    Code:
    public class ElectricReading {
        private double previousNum;
        private double newNum;
        private String month;
        private boolean discountEligible;
    
    
        public ElectricReading(double previousNum, double newNum, String month, boolean discountEligible) {
            this.previousNum = previousNum;
            this.newNum = newNum;
            this.month = month;
            this.discountEligible = discountEligible;
    
        }
    
        public void CalculateKw(double kwRate) {
            double totalKwUsed;
            totalKwUsed = (this.newNum - this.previousNum);
    
            System.out.println("After subtracting previous reading of " + previousNum + "kw from new reading of " + newNum + "kw. " +
                    "The total Kw used in the month(s) of " + month + " came out to the amount of " + totalKwUsed + " kw");
    
    
            double kwCost;
            kwCost = totalKwUsed * kwRate;
            System.out.println("Cost of electric bills for month of " + month + " = " + kwCost + " NIS");
    
            //user who is eligible for discount receives 50% price per KW on first 800KW used per two month time period
    
            double discountCost = kwCost/2;
            double discountCostBeforeTax = discountCost;
            double taxedDiscountCost = discountCost - (discountCostBeforeTax * .18);
            double kwCostDeductedTaxed = kwCost - taxedDiscountCost;
    
             if (discountEligible = true){
                 System.out.println("User is eligible for discount of 50% on first 800kw used per 2 month period. ");
    
                 if(totalKwUsed <= 400) {
                     System.out.println("Discount of " + taxedDiscountCost + "NIS applied." +"\n" + "Total before discount: " + kwCost + " nis"+"\n" + "Total after discount: " + kwCostDeductedTaxed + " nis");
                 }else{
                      discountCost = 400 * kwRate;
                      discountCostBeforeTax = discountCost;
                      taxedDiscountCost = discountCost - (discountCostBeforeTax * .18);
                     double kwCostDeductedTaxed2 = kwCost - taxedDiscountCost;
    
                     System.out.println("Discount of " + taxedDiscountCost + "NIS applied."+ "\n" + "Total before discount: " + kwCost + " nis." + "\n" + "Total after discount: " + kwCostDeductedTaxed2 + " nis.");
    
                 }
             }
    
    
    
            {
    
    
            }
    
    
        }
    }



    Heres the MAIN page
    Code:
    public class Main {
    
        public static void main(String[] args) {
    //must put in 5 digit number for each reading
    	ElectricReading mayJune = new ElectricReading(10000,10800,"May,June", true);
           mayJune.CalculateKw(0.53);
    
    
    
    
        }
    
    }


    Heres a printout when KW used is equal to or under 800kw
    Code:
    After subtracting previous reading of 10000.0kw from new reading of 10800.0kw. The total Kw used in the month(s) of May,June came out to the amount of 800.0 kw
    Cost of electric bills for month of May,June = 424.0 NIS
    User is eligible for discount of 50% on first 800kw used per 2 month period. 
    Discount of 173.84NIS applied.
    Total before discount: 424.0 nis.
    Total after discount: 250.16 nis.
    
    Process finished with exit code 0



    And Heres printout when KW used is over 800kw
    Code:
    After subtracting previous reading of 10200.0kw from new reading of 13800.0kw. The total Kw used in the month(s) of May,June came out to the amount of 3600.0 kw
    Cost of electric bills for month of May,June = 1908.0 NIS
    User is eligible for discount of 50% on first 800kw used per 2 month period. 
    Discount of 173.84NIS applied.
    Total before discount: 1908.0 nis.
    Total after discount: 1734.16 nis.
    
    Process finished with exit code 0

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: need some tips first java project

    Here's a problem I see:
    Code:
     if (discountEligible = true){
    The expression is an assignment not a comparison. Use the == operator to compare two operands.
    Also note that the value of a boolean variable has either a true or false value and does not need to be compared:
    Code:
     if (discountEligible){
    Should there be an else block of code that does something if there isn't a discount?
    Last edited by Norm; June 20th, 2016 at 10:59 AM.
    Norm

  3. #3
    Join Date
    Jun 2016
    Location
    JRE 1.8
    Posts
    2

    Re: need some tips first java project

    thnks for commenting!
    good point, i forgot about the == operator for boolean.

    as far as an else block if there isnt a discount. i had meant for the main part of the CalculateKw method to be used as a default. and only if theres a discount, for the code to jump to the ifDiscountEligible code.

    Code:
     public void CalculateKw(double kwRate) {
            double totalKwUsed;
            totalKwUsed = (this.newNum - this.previousNum);
    
            System.out.println("After subtracting previous reading of " + previousNum + "kw from new reading of " + newNum + "kw. " +
                    "The total Kw used in the month(s) of " + month + " came out to the amount of " + totalKwUsed + " kw");
    
    
            double kwCost;
            kwCost = totalKwUsed * kwRate;
            System.out.println("Cost of electric bills for month of " + month + " = " + kwCost + " NIS");

  4. #4
    Join Date
    May 2016
    Location
    Navi Mumbai
    Posts
    10

    Re: need some tips first java project

    In order to build a project in Java, take the reference guide from the course tutorials. These tutorials can help out.

  5. #5
    Join Date
    Jul 2016
    Posts
    6

    Re: need some tips first java project

    larger java code examples archive will help you about your projects

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured