tingwong 0 Newbie Poster

Hi everyone, I have a little problem with my code right now. For a project, we are to create a snack machine with cookies and mints using a GUI. We are given nickels, dimes, and quarters to enter in. The problem is when I enter in 1 nickel and 6 dimes, it doesnt work. It also doesnt work when I input 7 nickels and 3 dimes. These are the 2 combinations out of all that I tried that doesnt work(I believe I tried all possible combinations). Below are snippets of my code.

package proj3;

import java.awt.Color;
import java.util.ArrayList;

public class SnackMachine {

    private ArrayList<Mints> mints;
    private ArrayList<Cookies> cookies;
    private Money amount; 
    private Cookies c;
    private Mints m;

    public SnackMachine(){
        mints = new ArrayList<Mints>();
        cookies = new ArrayList<Cookies>();
    }
    public void addCookies(CookieFlavors flavor, int nrcookies){
        for(int i = 0; i < nrcookies; i++){
            c = new Cookies(flavor);
            cookies.add(c);
        }
    }
    public void addMints(Color color, int nrmints){
        for(int i = 0; i < nrmints; i++){
            m = new Mints(color);
            mints.add(m);
        }
    }
    public Cookies buyCookies(Money money){
        amount = money;
        if(money.getTotal() != .65){
            return null;
        }
        while(cookies.size() != 0){
            return cookies.remove(0);
        }if(cookies.size() == 0){
            return null;
        }
        return c;
    }
    public Mints buyMints(Money money) {
        if(money.getTotal() != .35){
            return null;
        }
        if(mints.size() != 0){
            return mints.remove(0);
        }if(mints.size() == 0){
            return null;
        }
        return m;
    }   
    public int getNrMints() {
        return mints.size();
    }
    public int getNrCookies() {
        return cookies.size();
    }
    public Money getCashOnHand() {
        return amount;
    }
}



package proj3;

public class Money {

    private int numnickels;
    private int numdimes;
    private int numquarters;
    private final double nickel = .05;
    private final double dime = .10;
    private final double quarter = .25;

    public Money(int nickel, int dime, int quarter){
        this.numnickels = nickel;
        this.numdimes = dime;
        this.numquarters = quarter;
    }
    public void addMoney(Money money){
        this.numnickels = numnickels + money.numnickels;
        this.numdimes = numdimes + money.numdimes;
        this.numquarters = numquarters + money.numquarters;
    }
    public int getNickels(){
        return numnickels;
    }
    public int getDimes(){
        return numdimes;
    }
    public int getQuarters(){
        return numquarters;
    }
    public double getTotal(){
        double total = ((numnickels * nickel) + (numdimes * dime) + (numquarters * quarter));
        return total;
    }
    public String toString(){
        String str = "Nickels: " + getNickels() + "\n" + "Dimes: " + getDimes() + "\n" + "Quarters: " + getQuarters() + "\n" + "Total: $" + getTotal();
        return str;
    }
}

I would really appreciate if anyone could help me. I honestly have no clue what is wrong! Unless I am seriously bad at basic arithmetic....
EDIT: I found out that it turns out to be .65000000001. How can I fix it so that it returns a double with just 2 decimal places?

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.