473,666 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Help w/ Enum Safe Type

4 New Member
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");

}
Sep 13 '07 #1
3 1402
madhoriya22
252 Contributor
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 ?
Sep 13 '07 #2
noellieb
4 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2. public enum Coin
  3. {
  4.      penny(1, "penny"), nickel(5,"nickel"), dime(10,"dime"), quarter(25,"quarter"); 
  5.  
  6.      //Constructor 
  7.      Coin(int value, String name) 
  8.      { 
  9.          this.value = value; 
  10.         this.name = name;
  11.      }
  12.  
  13.      public int value() 
  14.      { 
  15.          return value; 
  16.      }
  17.  
  18.      public String CoinName() 
  19.      { 
  20.  
  21.          return name; 
  22.      }
  23.  
  24.      private final int value;
  25.      private final String name;
  26. }
  27.  
  28.  
  29.  
Sep 13 '07 #3
noellieb
4 New Member
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]
Sep 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

20
4833
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
4
7842
by: Mattias Brändström | last post by:
Hi! Suppose I want to do some thing like this: enum X { A, B, C, D }; int main() { X a1 = { A, B, C, D }; int size_a = sizeof(a1) / sizeof(X);
12
3414
by: Steven T. Hatton | last post by:
Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using static const * int class members as opposed to using enumerations to accomplish a * similar goal. The use of static constants seems more explicit and * obvious to me. Unless I assign values to the enumerators, the * compiler will do it for me....
18
2368
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if there was a bit more typechecking ?
2
11590
by: Gregg Teehan | last post by:
Suggestions on best practive for array declare / subscript using an enum - this is easy in Delphi / Object Pascal. I want private MyObjectClass myObjects; myObjects = new MyObjectClass ; myObjects = new MyObjectClass(); myObjects = new MyObjectClass();
3
3000
by: Matt | last post by:
Hi, Recently we had some code like this cause a failure: MyEnum myEnum = (MyEnum) (int) dt; i.e. reading an int out of the database and casting it into a type-safe enum. The thought behind this construct was to enforce type safety and
10
2884
by: Tomás | last post by:
When you simply want to store a number, what integral type do you use? For instance, let's say we have the following in a Poker game: struct Card { enum Suit { Hearts, Diamonds, Spades, Clubs } suit; int value; };
34
11177
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
3
1284
by: Ralfeus | last post by:
Hi all Oftenly I need to use enums but standard C# enums provide little bit poor possibilities. For example I'd like to restrict variables to get only values specified in enums. Also sometimes I want to implement some type conversion from enum to some other type. Is it possible to create a new type, which would extend an enum? Thanks
0
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.