So I have a button which adds a "Miner" then removes the cost of the Miner.
The problem is that it is taking away too much, double the cost, and also will not buy if the current gold is equal to the cost. I have no idea why it is like this because I know I didn't program it like this.

Here is an Example Scenario:
hasEnoughGold, returns boolean (On Exact Amount)
hasEnoughGold, returns boolean (One gold more then Cost)
miner.getCost(), returns int
Calculation (game.getCurGold() - miner.getCost() = game.getCurGold())
game.getCurGold(), returns double

false
true
25
Calc: 26.0 - 25.0 = 1.0
Gold: -24.0

Some Code:
Miner Class

package com.github.geodox.goldminer.game.upgrades;

import com.github.geodox.goldminer.game.Game;

public class Miner
{
    public Game game;
    protected double perSecGold;
    public int initCost = 25;
    private int totalMiners;
    public int maxMiners = 250;

    public Miner()
    {
        totalMiners = 0;
        perSecGold = 0;
        initCost = 25;
    }

    public void addMiner()
    {
        totalMiners++;
        if(getTotalMiners() < 10)
        {
            setCost(25);
            setPerSecGold(0.1f);
            game.addPerSecGold(perSecGold);
        }
        else if(...)
        {

        }
    }

    public int getCost()
    {
        return initCost;
    }

    public void setCost(int cost)
    {
        this.initCost = cost;
    }

    public double getPerSecGold()
    {
        return perSecGold;
    }

    public void setPerSecGold(double perSecGold)
    {
        this.perSecGold = perSecGold;
    }

    public int getMaxMiners()
    {
        return maxMiners;
    }

    public int getTotalMiners()
    {
        return totalMiners;
    }
}

Game Class

package com.github.geodox.goldminer.game;

public class Game
{
    protected double totalGold;
    protected double curGold;
    protected float perSecGold;
    protected float perClickGold;
    protected int clickCount;

    public Game()
    {
        totalGold = 0;
        curGold = 0;
        perSecGold = 1;
        perClickGold = 1;
        clickCount = 0;
    }

    public void addGold(float goldValue)
    {
        this.curGold += goldValue;
        this.totalGold += goldValue;
    }

    public void removeGold(float goldValue)
    {
        System.out.println("Calc: " + this.curGold + " - " + goldValue + " = " + (this.curGold -= goldValue));
        this.curGold -= goldValue;
        System.out.println("Gold: " + getCurGold());
    }

    public boolean hasEnoughGold(float cost)
    {
        if(getCurGold() > cost)
            return true;
        return false;
    }

    public double getTotalGold()
    {
        return totalGold;
    }

    public double getCurGold()
    {
        return curGold;
    }

    public float getPerSecGold()
    {
        return perSecGold;
    }

    public void setPerSecGold(float perSecGold)
    {
        this.perSecGold = perSecGold;
    }

    public void addPerSecGold(double perSecGold2)
    {
        this.perSecGold += perSecGold2;
    }

    public float getPerClickGold()
    {
        return perClickGold;
    }

    public void setPerClickGold(float perClickGold)
    {
        this.perClickGold = perClickGold;
    }

    public void addPerClickGold(float perClickGold)
    {
        this.perClickGold += perClickGold;
    }

    public void addClickCount()
    {
        this.clickCount += 1;
    }

    public int getClickCount()
    {
        return this.clickCount;
    }

}

Controller containing Button's setOnAction

buyMiner.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {
                if(game.hasEnoughGold(miner.getCost()))
                {
                    System.out.println("true");
                    miner.addMiner();
                    System.out.println(miner.getCost());
                    game.removeGold(miner.getCost());
                }
                else
                    System.out.println("false");
            }
        });

Any help is appreciated.

The problem is that it is taking away too much, double the cost

System.out.println("Calc: " + this.curGold + " - " + goldValue + " = " + (this.curGold -= goldValue));
this.curGold -= goldValue;

In the above code you execute this.curGold -= goldValue twice (once in the print statement and once in the line afterwards), so the gold value will indeed be subtracted twice.

and also will not buy if the current gold is equal to the cost.

public boolean hasEnoughGold(float cost)
{
    if(getCurGold() > cost)
        return true;
    return false;
}

Right because getCurGold() > cost is not true if getCurGold() is equal to cost.

PS: if(foo) return true; else return false; can just be written as return foo; - no need for the if.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.