12 Replies - 1077 Views - Last Post: 24 September 2013 - 10:41 PM Rate Topic: -----

#1 Tigers27   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

CashRegister "type" error

Posted 23 September 2013 - 09:53 AM

For some reason my program is saying that CashRegister cannot be resolved as a type and I can't figure out what to do about it.
import java.util.*;

public class CandyMachine {
  
  static Scanner console = new Scanner(System.in);
  
  public static void main(String[] args) {
    CashRegister cashRegister = new CashRegister();
    Dispenser candy = new Dispenser(100, 50);
    Dispenser chips = new Dispenser(100, 65);
    Dispenser gum = new Dispenser(75, 45);
    Dispenser cookies = new Dispenser(100, 85);
    
    int choice;
    
    showSelection();
    choice = console.nextInt();
    
    while(choice != 9)
    {
      switch (choice) {
      case 1: sellProduct(candy, cashRegister);
      
      break;
      
      case 2: sellProduct(chips, cashRegister);
      
      break;
      
      case 3: sellProduct(gum, cashRegister);
      
      break;
      
      case 4: sellProduct(cookies, cashRegister);
      
      break;
      
      default: System.out.println("Invalid Selection");
    }
    
    showSelection();
    choice = console.nextInt();
  }
}


public static void showSelection() {
  System.out.println("***Welcome to Matt's " + "Candy Shop ***");
  System.out.println("To select an item, enter ");
  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 for exit");
}

public static void sellProduct(Dispenser product, CashRegister cRegister) {
  int price;
  int coinsInserted;
  int coinsRequired;
  
  if (product.getCount() > 0)
  {
    price = product.getProductCost();
    coinsRequired = price;
    coinsInserted = 0;
    
    while(coinsRequired > 0)
    {
      System.out.print("Please deposit " + coinsRequired + " cents: ");
      coinsInserted = coinsInserted + console.nextInt();
      coinsRequired = price - coinsInserted;
    }
    System.out.println();
    
    cRegister.acceptAmount(coinsInserted);
    product.makeSale();
    
    System.out.println("Collect your item " + "at the bottom and " + "enjoy. \n");
  }
  else
    System.out.println("Sorry this item " + "is sold out. \n");
}
}
      







Is This A Good Question/Topic? 0
  • +

Replies To: CashRegister "type" error

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15696
  • View blog
  • Posts: 62,853
  • Joined: 12-June 08

Re: CashRegister "type" error

Posted 23 September 2013 - 09:58 AM

Is the class CashRegister defined elsewhere?
Was This Post Helpful? 0
  • +
  • -

#3 Tigers27   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

Re: CashRegister "type" error

Posted 23 September 2013 - 10:05 AM

View Postmodi123_1, on 23 September 2013 - 09:58 AM, said:

Is the class CashRegister defined elsewhere?


yes, it's the class name and I defined it in main.
Was This Post Helpful? 0
  • +
  • -

#4 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: CashRegister "type" error

Posted 23 September 2013 - 10:17 AM

Is it in a different package?
Was This Post Helpful? 0
  • +
  • -

#5 IvGotAPocket   User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 157
  • Joined: 12-August 13

Re: CashRegister "type" error

Posted 23 September 2013 - 12:39 PM

when you say this you are instantiating a new object of your CashRegister Class
    CashRegister cashRegister = new CashRegister();



but i dont see your CashRegister class?
public class CashRegister
{
 
}


Was This Post Helpful? 0
  • +
  • -

#6 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: CashRegister "type" error

Posted 23 September 2013 - 02:16 PM

Your other dependency is dispenser. Does that resolve?
Was This Post Helpful? 0
  • +
  • -

#7 Tigers27   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

Re: CashRegister "type" error

Posted 23 September 2013 - 09:22 PM

View PostIvGotAPocket, on 23 September 2013 - 12:39 PM, said:

when you say this you are instantiating a new object of your CashRegister Class
    CashRegister cashRegister = new CashRegister();



but i dont see your CashRegister class?
public class CashRegister
{
 
}


what do you mean?
Was This Post Helpful? 0
  • +
  • -

#8 Atspulgs   User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 541
  • Joined: 29-July 09

Re: CashRegister "type" error

Posted 23 September 2013 - 10:17 PM

He means, you need to create CashRegister class before using it :) So do you have Cash register class?
Was This Post Helpful? 0
  • +
  • -

#9 Tigers27   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

Re: CashRegister "type" error

Posted 24 September 2013 - 10:54 AM

View PostAtspulgs, on 23 September 2013 - 10:17 PM, said:

He means, you need to create CashRegister class before using it :)/> So do you have Cash register class?

haha I was out of it when I asked what he meant. But I'm not sure how to go about a cash register class
Was This Post Helpful? 0
  • +
  • -

#10 Tigers27   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

Re: CashRegister "type" error

Posted 24 September 2013 - 11:10 AM

I need to figure out how to make a cashregister class and dispenser class. can anyone help with that?
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15696
  • View blog
  • Posts: 62,853
  • Joined: 12-June 08

Re: CashRegister "type" error

Posted 24 September 2013 - 11:14 AM

Hit the tutorials...

http://www.dreaminco...igning-classes/
http://www.dreaminco...ing-classes-ii/
Was This Post Helpful? 0
  • +
  • -

#12 Tigers27   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

Re: CashRegister "type" error

Posted 24 September 2013 - 09:47 PM

Now I'm getting more errors...



import java.util.*;

public class CandyMachine {
  
  static Scanner console = new Scanner(System.in);
  
  private int cashOnHand;
  public CashRegister()
  public CashRegister(int cashIn)
  public int currentBalance()
  public void acceptAmount(int amountIn)
  public int currentBalance() {
  return cashOnHand;
}
public void acceptAmount(int amountIn) {
  cashOnHand = cashOnHand + amountIn;
}
public CashRegister(int cashIn) {
  if (cashIn >= 0)
    cashOnHand = cashIn;
  else 
    cashOnHand = 500;
}
public CashRegister() {
  cashOnHand = 500;
}
public class CashRegister {
  private int cashOnHand;
  
  public CashRegister() {
    cashOnHand = 500;
  }
  public CashRegister(int cashIn) {
    if (cashIn >= 0)
      cashOnHand = cashIn;
    else
      cashOnHand = 500;
  }
  public int currentBalance() {
    return cashOnHand;
  }
  public void acceptAmount(int amountIn) {
    cashOnHand = cashOnHand + amountIn;
  }
}

private int numberOfItems;
private int cost;

public Dispenser()
  public Dispenser(int setNoOfItems, int setCost)
  public int getCount()
  public int getProductCost()
  public void makeSale()
  Dispenser chips = new Dispenser(100, 65);
Dispenser candy = new Dispenser(100, 65);
Dispenser gum = new Dispenser(100, 65);
Dispenser cookies = new Dispenser(100, 65);
public int getCount() {
  return numberOfItems;
}
public int getProductCost() {
  return cost;
}
public void makeSale() {
  numberOfItems--;
}
public Dispenser(int getNoOfItems, int setCost) {
  if (setNoOfItems >= 0)
    numberOfItems = setNoOfItems;
  else
    numberOfItems = 50;
  if (setCost >= 0)
    cost = setCost;
  else
    cost = 50;
}
public Dispenser() {
  numberOfItems = 50;
  cost = 50;
}
public class Dispenser {
  numberOfItems = 50;
  cost = 50;
}
public Dispenser(int setNoOfItems, int setCost) {
  if (setNoOfItems >= 0)
    numberOfItems = setNoOfItems;
  else
    numberOfItems = 50;
  if (setCost >= 0)
    cost = setCost;
  else
    cost = 50;
}
public int getCount() {
  return numberOfItems;
}
public int getProductCost() {
  return cost;
}
public void makeSale() {
  numberOfItems--;
}
}
  
  public static void main(String[] args) {
    CashRegister cashRegister = new CashRegister();
    Dispenser candy = new Dispenser(100, 50);
    Dispenser chips = new Dispenser(100, 65);
    Dispenser gum = new Dispenser(75, 45);
    Dispenser cookies = new Dispenser(100, 85);
    
    int choice;
    
    showSelection();
    choice = console.nextInt();
    
    while(choice != 9)
    {
      switch (choice) {
      case 1: sellProduct(candy, cashRegister);
      
      break;
      
      case 2: sellProduct(chips, cashRegister);
      
      break;
      
      case 3: sellProduct(gum, cashRegister);
      
      break;
      
      case 4: sellProduct(cookies, cashRegister);
      
      break;
      
      default: System.out.println("Invalid Selection");
    }
    
    showSelection();
    choice = console.nextInt();
  }
}


public static void showSelection() {
  System.out.println("***Welcome to Matt's " + "Candy Shop ***");
  System.out.println("To select an item, enter ");
  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 for exit");
}

public static void sellProduct(Dispenser product, CashRegister cRegister) {
  int price;
  int coinsInserted;
  int coinsRequired;
  
  if (product.getCount() > 0)
  {
    price = product.getProductCost();
    coinsRequired = price;
    coinsInserted = 0;
    
    while(coinsRequired > 0)
    {
      System.out.print("Please deposit " + coinsRequired + " cents: ");
      coinsInserted = coinsInserted + console.nextInt();
      coinsRequired = price - coinsInserted;
    }
    System.out.println();
    
    cRegister.acceptAmount(coinsInserted);
    product.makeSale();
    
    System.out.println("Collect your item " + "at the bottom and " + "enjoy. \n");
  }
  else
    System.out.println("Sorry this item " + "is sold out. \n");
}

}}
      













Was This Post Helpful? 0
  • +
  • -

#13 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: CashRegister "type" error

Posted 24 September 2013 - 10:41 PM

Make sure for every open curly brace you have only one corresponding closing curly brace.

Also, if you're using eclipse, you can do ctrl+shift+f to format your code in the editor.

EDIT:
Also, make sure that your statements all end with semicolons.

Also, make sure your called methods are called from within a method and not out in class-space.

Also, try to find out how to fix the error you specifically get by double clicking it so it jumps to that line. Then you can identify the problem and fix it.

You'll get the hang of it.

EDIT:
Also, you should try to create an external file class instead of a nested class because the scope of that nested class makes it easy to introdoce all kinds of hard to notice problems. When it's in another file, private really means private.

EDIT:
Final edit as I am tired of picking things out of this thing.

What you are doing is currently too complex for you. Start smaller and work your way up.

Try creating a class that prints your name in an external class file and calling that method from an instance of the class in your main method. From there, spread your fingers and see where the wild things are.

This post has been edited by MentalFloss: 24 September 2013 - 10:49 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1