Need Help w/ Enum Safe Type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • noellieb
    New Member
    • Sep 2007
    • 4

    #1

    Need Help w/ Enum Safe Type

    I have class project and was asked to design 2 classes. 1 : Class Coin designated by coin type (PENNY, NICKEL, DIME, QUARTER OR DOLLAR)
    2: Create Class CoinChanger To allow coin changing : say if one were to buy something to return the correct change. I have the following so far. Please Help!!!
    This is my class COIN :
    public enum Coin
    {
    penny(1, "penny"), nickel(5,"nicke l"), dime(10,"dime") , quarter(25,"qua rter");

    //Constructor
    Coin(int value, String name) { this.value = value;
    this.name = name;}

    public int value() { return value; }

    public String CoinName() { return name; }

    private final int value;
    private final String name;}// end of class


    My Class CoinChanger is as follow:

    I would like to know how to keep track of my instance using type coin in the coinChanger constructor. I am contemplating this:

    public class CoinChanger
    {
    /**
    *
    */
    private double my_balance;// keep track of much money I have
    private int purchase; // the the total amount of user purchase
    private int payment; // the amount payed for the purchase
    private Coin my_dollars; // total amnt of $
    private Coin my_dimes;// total dimes
    private Coin my_quarters;// total quarters
    private Coin my_nickels;
    private Coin my_pennies;
    final static int a =4;// to determine the $ value using quarters


    // Pass object of type coin or primitive?
    CoinChanger(Str ing aName)
    {
    // keeping track of pennies how???
    my_pennies = Coin.penny; // the value of my_pennies is PENNY(1)
    //then add penny to list???
    my_nickels = Coin.valueOf("n ickel");
    my_dimes = Coin.valueOf("d ime");
    my_quarters = Coin.valueOf("q uarter");
    // How to detirmine the value of a $????/
    my_dollars= Coin.valueOf("q uarter");

    }
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    #2
    Originally posted by noellieb
    I have class project and was asked to design 2 classes. 1 : Class Coin designated by coin type (PENNY, NICKEL, DIME, QUARTER OR DOLLAR)
    2: Create Class CoinChanger To allow coin changing : say if one were to buy something to return the correct change. I have the following so far. Please Help!!!
    This is my class COIN :
    public enum Coin
    {
    penny(1, "penny"), nickel(5,"nicke l"), dime(10,"dime") , quarter(25,"qua rter");

    //Constructor
    Coin(int value, String name) { this.value = value;
    this.name = name;}

    public int value() { return value; }

    public String CoinName() { return name; }

    private final int value;
    private final String name;}// end of class


    My Class CoinChanger is as follow:

    I would like to know how to keep track of my instance using type coin in the coinChanger constructor. I am contemplating this:

    public class CoinChanger
    {
    /**
    *
    */
    private double my_balance;// keep track of much money I have
    private int purchase; // the the total amount of user purchase
    private int payment; // the amount payed for the purchase
    private Coin my_dollars; // total amnt of $
    private Coin my_dimes;// total dimes
    private Coin my_quarters;// total quarters
    private Coin my_nickels;
    private Coin my_pennies;
    final static int a =4;// to determine the $ value using quarters


    // Pass object of type coin or primitive?
    CoinChanger(Str ing aName)
    {
    // keeping track of pennies how???
    my_pennies = Coin.penny; // the value of my_pennies is PENNY(1)
    //then add penny to list???
    my_nickels = Coin.valueOf("n ickel");
    my_dimes = Coin.valueOf("d ime");
    my_quarters = Coin.valueOf("q uarter");
    // How to detirmine the value of a $????/
    my_dollars= Coin.valueOf("q uarter");

    }
    Hi,
    Please use code tags while posting your code.
    Where is the Coin class you are talking about ?

    Comment

    • noellieb
      New Member
      • Sep 2007
      • 4

      #3
      Code:
      public enum Coin
      {
      	 penny(1, "penny"), nickel(5,"nickel"), dime(10,"dime"), quarter(25,"quarter"); 
      	 
      	 //Constructor 
      	 Coin(int value, String name) 
      	 { 
      		 this.value = value; 
      		this.name = name;
      	 }
      
      	 public int value() 
      	 { 
      		 return value; 
      	 }
      	 
      	 public String CoinName() 
      	 { 
      		
      		 return name; 
      	 }
      	 
      	 private final int value;
      	 private final String name;
      }

      Comment

      • noellieb
        New Member
        • Sep 2007
        • 4

        #4
        here is the CoinChanger

        [CODE

        public class CoinChanger
        {
        private double my_balance;//money avail in the register
        private int purchase;// amount of goods purchased by client
        private int payment;//amount paid the client private Coin my_dollars;
        private Coin my_dimes;
        private Coin my_quarters;
        private Coin my_nickels;
        private Coin my_pennies;
        final static int a =4;// to determine the $value using qtrs

        /**
        *
        * @param aNum
        * @param aName
        */
        // Pass object of type coin or primitive?
        CoinChanger(Str ing aName)
        {
        // keeping track of pennies how???
        my_pennies = Coin.penny; // the value of my_pennies is PENNY(1)
        //then add penny to list???
        my_nickels = Coin.valueOf("n ickel");
        my_dimes = Coin.valueOf("d ime");
        my_quarters = Coin.valueOf("q uarter");
        // How to detirmine the value of a $????/
        my_dollars= Coin.valueOf("q uarter");

        }


        [/CODE]

        Comment

        Working...