Can you debug my code? It is supposed to be a candy machine using three objects but there is an error I cannot find. Thank you Main: public class Main { public static void main(String[] args){ System.out.println("Press any number to start"); CandyMachine candyMachine = new CandyMachine();//asks for a number candyMachine.On(); } } Candy Machine: import java.util.*; public class CandyMachine{ CashRegister cashRegister; Dispenser candy; Dispenser chips; Dispenser gum; Dispenser cookies; CashRegister register; public CandyMachine(){ CashRegister register = new CashRegister(); Dispenser candy = new Dispenser(0.50, 20); Dispenser chips = new Dispenser(0.60, 20); Dispenser gum = new Dispenser(0.25, 20); Dispenser cookies = new Dispenser(0.85, 20); Scanner console = new Scanner(System.in); cashRegister = new CashRegister(console.nextInt()); } public void On(){ showSelection(); int choice; do{ Scanner input = new Scanner(System.in); choice = input.nextInt(); switch (choice) { case 1: System.out.println("That is not an option, please try again."); sellProduct(candy, register); break; case 2: sellProduct(chips, register); break; case 3: sellProduct(gum, register); break; case 4: sellProduct(cookies, register); break; case 9: break; default: System.out.println("That is not an option, please try again."); } } while (choice != 9); } private void showSelection(){ System.out.println("*** Welcome ***"); System.out.println("To select an item, enter "); System.out.println("0 for Adding more cash"); System.out.println("1 for Candy"); System.out.println("2 for Chips"); System.out.println("3 for Gum"); System.out.println("4 for Cookies"); System.out.println("9 to exit"); } private void sellProduct(Dispenser product, CashRegister cashRegister){ Scanner console = new Scanner(System.in); if(product.getCount() > 0){ if(product.getProductCost() <= cashRegister.currentBalance()) { cashRegister.deductAmount(product.getProductCost()); product.makeSale(); System.out.println("Collect item"); }else{ System.out.println("Plese try again! Insufficent funds"); } }else{ System.out.println("Your choosen item is sold out, choose another one"); } } } Register: public class CashRegister{ private int cashOnHand=0; public CashRegister(){ cashOnHand = 10; } public CashRegister(int cashIn){ cashOnHand = cashIn; //return cashIn; } public int currentBalance(){ return cashOnHand; } public void acceptAmount(int amountIn){ if(amountIn <= 0){ System.out.println("Sorry, but you have to add money."); }else{ cashOnHand = cashOnHand + amountIn; } } public void deductAmount(int amountCost) { cashOnHand= amountCost--; } } Dispenser: public class Dispenser{ private int numberOfItems; private int cost; private int prodQty; private double prodCost; Dispenser(double cost, int numberOfItems) { //prodCost = cost; prodQty = numberOfItems; } double getProdCost() { return prodCost; } int getProdQty() { return prodQty; } void setProdQty(int numberOfItems) { prodQty -= numberOfItems; } public Dispenser(int setNoOfItems, int setCost){ //prodCost = cost; prodQty = numberOfItems; } public int getCount(){ return numberOfItems; } public int getProductCost(){ return cost; // -- Your Code Here } public void makeSale(){ numberOfItems--; } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Can you debug my code? It is supposed to be a candy machine using three objects but there is an error I cannot find. Thank you

Main:

public class Main {
public static void main(String[] args){
System.out.println("Press any number to start");
CandyMachine candyMachine = new CandyMachine();//asks for a number
candyMachine.On();
}
}

Candy Machine:

import java.util.*;
public class CandyMachine{
CashRegister cashRegister;
Dispenser candy;
Dispenser chips;
Dispenser gum;
Dispenser cookies;
CashRegister register;
public CandyMachine(){
CashRegister register = new CashRegister();
Dispenser candy = new Dispenser(0.50, 20);
Dispenser chips = new Dispenser(0.60, 20);
Dispenser gum = new Dispenser(0.25, 20);
Dispenser cookies = new Dispenser(0.85, 20);
Scanner console = new Scanner(System.in);
cashRegister = new CashRegister(console.nextInt());
}
public void On(){
showSelection();
int choice;
do{
Scanner input = new Scanner(System.in);

choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("That is not an option, please try again.");
sellProduct(candy, register);
break;
case 2:
sellProduct(chips, register);
break;
case 3:
sellProduct(gum, register);
break;
case 4:
sellProduct(cookies, register);
break;
case 9:
break;
default:
System.out.println("That is not an option, please try again.");
}
}
while (choice != 9);
}
private void showSelection(){
System.out.println("*** Welcome ***");
System.out.println("To select an item, enter ");
System.out.println("0 for Adding more cash");
System.out.println("1 for Candy");
System.out.println("2 for Chips");
System.out.println("3 for Gum");
System.out.println("4 for Cookies");
System.out.println("9 to exit");
}
private void sellProduct(Dispenser product, CashRegister cashRegister){
Scanner console = new Scanner(System.in);
if(product.getCount() > 0){
if(product.getProductCost() <= cashRegister.currentBalance()) {
cashRegister.deductAmount(product.getProductCost());
product.makeSale();
System.out.println("Collect item");
}else{
System.out.println("Plese try again! Insufficent funds");
}
}else{
System.out.println("Your choosen item is sold out, choose another one");
}
}
}

Register:

public class CashRegister{
private int cashOnHand=0;
public CashRegister(){
cashOnHand = 10;
}
public CashRegister(int cashIn){
cashOnHand = cashIn;
//return cashIn;
}
public int currentBalance(){
return cashOnHand;
}
public void acceptAmount(int amountIn){
if(amountIn <= 0){
System.out.println("Sorry, but you have to add money.");
}else{
cashOnHand = cashOnHand + amountIn;
}
}
public void deductAmount(int amountCost)
{
cashOnHand= amountCost--;
}
}

Dispenser:

public class Dispenser{
private int numberOfItems;
private int cost;
private int prodQty;
private double prodCost;
Dispenser(double cost, int numberOfItems) {
//prodCost = cost;
prodQty = numberOfItems;
}
double getProdCost() {
return prodCost;
}
int getProdQty() {
return prodQty;
}
void setProdQty(int numberOfItems) {
prodQty -= numberOfItems;
}
public Dispenser(int setNoOfItems, int setCost){
//prodCost = cost;
prodQty = numberOfItems;
}
public int getCount(){
return numberOfItems;
}
public int getProductCost(){
return cost;
// -- Your Code Here
}
public void makeSale(){
numberOfItems--;
}
}

Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY