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).
You are correct in stating that
amountis initially null. To get rid of the NullPointerException you could initially give the machine Money but with a zero-value, i.e,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?