Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8898039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:30:28+00:00 2026-06-15T00:30:28+00:00

this is my first post here so excuse me if I am not following

  • 0

this is my first post here so excuse me if I am not following the rules. So my project is writing code to simulate a snack machine. There are cookies and mints in the snack machine. We interact with the snack machine using a GUI to buy cookies/mints and choosing the amount of dimes,nickels, and quarters to put in. Here is the code for the following Snack Machine file and Money file(I have not included the GUI file given to us because we are not to change it and I do not believe anything is wrong as it was given to us).

    package proj3;

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

public class SnackMachine {

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

    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){
        if(money.getTotal() != .65){
            return null;
        }
        while(cookies.size() != 0){
            amount = money;
            amount.addMoney(money);
            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 = 5;
    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 Money addMoney(Money money){
        this.numnickels = numnickels + money.numnickels;
        numdimes = money.numdimes;
        numquarters = money.numquarters;
        return money;
    }
    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)) / 100;
        return total;
    }
    public String toString(){
        String str = "Nickels: " + getNickels() + "\n" + "Dimes: " + getDimes() + "\n" + "Quarters: " + getQuarters() + "\n" + "Total: $" + getTotal();
        return str;
    }
}

This is where I am having trouble. I have no idea how to get the machine to add on additional money the user inputs in. Amount is initially set to null(right?) so without having initialized it, I cannot call the addMoney method cause it keeps giving me nullpointer error. So I have to initialize it to the money variable of Money. But then this will keep resetting it to whatever I input in next without adding it on. Same as with when I put it in the buyMints. The amount in buyMints will override the amount in buyCookies and vice versa. I am not expecting an answer just a sort of a pointer as in ‘I have to create a new method’ or that sort of stuff. I have been stuck on this for quite some time and would really appreciate any help! Thank you all.

P.S: User must always input in exact change. For cookies it is $.65 and mints it is $.35. If not there will be a failed purchase error(it is in the GUI file given to us).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:30:29+00:00Added an answer on June 15, 2026 at 12:30 am

    You are correct in stating that amount is initially null. To get rid of the NullPointerException you could initially give the machine Money but with a zero-value, i.e,

    private Money amount  = new Money(0, 0, 0);
    

    I’d also look closely at the loop in buyCookies. It seems like you’re overwriting how much money is in the machine.

    The only other thoughts I have are that, by the looks of it, the only difference between mints and cookies are the price. Can you combine the functionality of these two?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post here in StackOverflow. I am not a newbie to
This is my first post here. I am writing a program in C++ using
This is my first post here so go easy. I am trying to build
This is my first post here. I have a problem. I need to take
this is my first post here on stackoverflow and am very impressed by the
My first post here, so i hope this is the right area. I am
um, first post here, this place seems to be all over google and i
I'm new to Java and this is my first post on here so hopefully
This is my first time here so I hope I post this question at
This is my first post on stackoverflow, so please excuse me if my question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.