27

For my assignment I have to create a Gas Meter System for a Gas company to allow employees to create new customer accounts and amend data such as name and unit costs along with taking (depositing) money from their account.

I have created my constructor and even added in a method of overloading although I'm currently running into a problem when initiating one of my methods I named deposit(), this is supposed to take money from the users account while other methods such as recordUnits() allows the employee to import a gas meter reading of how many units the customer has used and updates the balance of that customers account which is essentially what the customer owes the company.

When testing the program with just preset information when trying to initiate the deposit method I get this

Account.deposit(Double.MAX_VALUE);

I am not too sure what this means and cannot seem to find anyway of getting past it!

test data and code seen below:

public class TestGasAccount 

{
    public static void main (String [] args)
    {
        GasAccount Account = new GasAccount (223,"Havana","TQ",1000);
        
        Account.getAccNo();
        Account.getName();
        Account.getAddress();
        Account.getUnits();
        Account.getBalance();
        Account.recordUnits(1000);
        Account.getUnits();
        Account.getBalance();
        Account.deposit(Double.MAX_VALUE);
    }
}

public class GasAccount 
{
    private int intAccNo;
    private String strName;
    private String strAddress; 
    private double dblBalance;
    private double dblUnits;
    protected double dblUnitCost = 0.02; 
    
     public GasAccount(int intNewAccNo,String strNewName,String strNewAddress,double dblNewUnits)
     {
         intAccNo = intNewAccNo;
         strName = strNewName;
         strAddress = strNewAddress;
         dblUnits = dblNewUnits;
         dblBalance = dblNewUnits * dblUnitCost;
     }
     
     public GasAccount (int intNewAccNo, String strNewName, String strNewAddress)
     {
         intAccNo = intNewAccNo;
         strName = strNewName;
         strAddress = strNewAddress;
     }
     
     public double deposit (Double dblDepositAmount)
     {
        dblBalance = dblBalance - dblDepositAmount; 
        return dblBalance;
     }
     
     public String recordUnits (double dblUnitsUsed)
     {
         double dblTempBalance;
         
         dblTempBalance = dblUnitsUsed * dblUnitCost;
         dblBalance = dblBalance + dblTempBalance;
         dblUnits = dblUnits + dblUnitsUsed;
         
         return "Transaction Successful"; 
     }
     
     public int getAccNo ()
     {
         System.out.println(intAccNo);
         return intAccNo;
     }
     
     public String getName()
     {
         System.out.println(strName);
         return strName; 
     }
     
      public String getAddress()
     {
         System.out.println(strAddress);
         return strName; 
     }
     
     public double getBalance()
     {
         System.out.println("£"+dblBalance);
         return dblBalance; 
     }
     
     public double getUnitCost()
     {
        
         return dblUnitCost;
     }
     
     public double getUnits ()
     {
         System.out.println(dblUnits);
         return dblUnits;
     }
     
     public void updateUnitCost (double dblNewUnitCost)
     {
         dblUnitCost = dblNewUnitCost;
     }
}
Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
Kriss Brown
  • 303
  • 2
  • 4
  • 6
  • 1
    It's not clear what the actual problem is. Which bit do you not understand? Have you looked at the documentation for Double.MAX_VALUE? – Jon Skeet Apr 22 '13 at 11:40
  • I'm trying to give the employee a choice of how much they want to take out but it does not allow me to put any specific value in it just wont recognize it. I'm not too sure how I would work around this. – Kriss Brown Apr 22 '13 at 12:18
  • 6
    I would've liked to have my account be deposited Double.MAX_Value :( – Totumus Maximus Feb 01 '17 at 16:16

3 Answers3

41

Double.MAX_VALUE is the maximum value a double can represent (somewhere around 1.7*10^308).

This should end in some calculation problems, if you try to subtract the maximum possible value of a data type.

Even though when you are dealing with money you should never use floating point values especially while rounding this can cause problems (you will either have to much or less money in your system then).

Kuchi
  • 4,204
  • 3
  • 29
  • 37
  • Thank you for the heads up I was not aware of the calculation problems! I have looked at the document trying to make sense of it I'm unfortunately not mathematically gifted like some so it's harder for me to understand! I'm trying to find a work through as it won't accept any value unless I put a 0.100 instead of 100. I have been looking at other data types to use such as big decimal but have no experience with this type. – Kriss Brown Apr 22 '13 at 12:21
  • Ever tried to use 100.0 instead of 100 (this shouldn't be a problem in Java). What IDE or compiler are you using? Btw you should use Comments in your code ;) – Kuchi Apr 22 '13 at 12:23
  • Thanks again Kuchi I appreciate the help I didn't even think of it that way around how silly of me. This will certainly have to do for now anyway! Kind regards PS: Thanks for the reminder I cheated a little and decided to add comments later ;) I'm currently using Net Beans the latest version – Kriss Brown Apr 22 '13 at 12:35
  • If it is true that Double.MAX_VALUE is the largest value, why do I think that Double.MAX_VALUE < Double.POSITIVE_INFINITY? – Tatarize Jun 22 '16 at 05:45
  • 2
    True. To be correct from the docs. MAX_VALUE: A constant holding the largest positive finite value of type. See http://math.stackexchange.com/questions/36289/is-infinity-a-number – Kuchi Jun 22 '16 at 07:55
  • Then what data type should you use for money? – Halfacht Oct 02 '17 at 07:39
  • 1
    @Halfacht `BigDecimal`. See: https://stackoverflow.com/questions/8148684/what-is-the-best-data-type-to-use-for-money-in-java-app – Kuchi Oct 02 '17 at 16:55
16

Resurrecting the dead here, but just in case someone stumbles against this like myself. I know where to get the maximum value of a double, the (more) interesting part was to how did they get to that number.

double has 64 bits. The first one is reserved for the sign.

Next 11 represent the exponent (that is 1023 biased). It's just another way to represent the positive/negative values. If there are 11 bits then the max value is 1023.

Then there are 52 bits that hold the mantissa.

This is easily computed like this for example:

public static void main(String[] args) {

    String test = Strings.repeat("1", 52);

    double first = 0.5;
    double result = 0.0;
    for (char c : test.toCharArray()) {
        result += first;
        first = first / 2;
    }

    System.out.println(result); // close approximation of 1
    System.out.println(Math.pow(2, 1023) * (1 + result));
    System.out.println(Double.MAX_VALUE);

} 

You can also prove this in reverse order :

    String max = "0" + Long.toBinaryString(Double.doubleToLongBits(Double.MAX_VALUE));

    String sign = max.substring(0, 1);
    String exponent = max.substring(1, 12); // 11111111110
    String mantissa = max.substring(12, 64);

    System.out.println(sign); // 0 - positive
    System.out.println(exponent); // 2046 - 1023 = 1023
    System.out.println(mantissa); // 0.99999...8
Eugene
  • 117,005
  • 15
  • 201
  • 306
1

this states that Account.deposit(Double.MAX_VALUE); it is setting deposit value to MAX value of Double dataType.to procced for running tests.

NPKR
  • 5,368
  • 4
  • 31
  • 48
  • Thanks for the swift reply NPKR could you possibly go into more detail I'm not too familiar with all of this! The thing is I'm trying to give the employee a choice of how much they want to take out but lets say for example I try to take out £10 it does not allow me to put that in or any other value! I considered using a different variable type such as integer although that wouldn't be viable as it's working with money and decimals! – Kriss Brown Apr 22 '13 at 11:46