• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

enum's injava  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the java file CreditCardtest.java is type safe sol
class CreditCardTest
{
public static void main(String[] args)
{
String creditCard=args[0].toUpperCase();
if(creditCard.equals(AllowedCreditCard.VISA.getname()))
{
System.out.println("yourcreditcard"+creditCard+"is accpeted.");
}else
if(creditCard.equals(AllowedCreditCard.MASTERCARD.getname()))
{
System.out.println("yourcreditcard"+creditCard+"is accpeted.");
}else
if(creditCard.equals(AllowedCreditCard.AMERICANEXPRESS.getname()))
{
System.out.println("yourcreditcard"+creditCard+"is accpeted.");
}else
{
System.out.println("cc not valid for acceptance"+args[0]+"at this point of time");
}
}
}
class AllowedCreditCard
{
protected final String card;
public final static AllowedCreditCard VISA=new AllowedCreditCard("VISA");
public final static AllowedCreditCard MASTERCARD=new AllowedCreditCard("MASTERCARD");
public final static AllowedCreditCard AMERICANEXPRESS=new AllowedCreditCard("AMERICANEXPRESS");
private AllowedCreditCard(String str)
{
card=str;
}
String getname()
{
return card;
}
}

//here the solution is considered type safe and is called type safe enumerationdesign pattern;
//sol to enumeration problem is left to the user
//in j2se5.0 presents arobust sol by introducing enum.
the outputs of the problem are:
javac creditcardtest.java
java CreditCardTest visa
>yourcreditcard VISA is accepted
java CreditCardTest ABNAMRO
>cc not valid for acceptance ABNAMRO at this point of time

now, ihave redone the prog using enum's

class CreditCardTest{
public static void main(String[]args){
int nargs=args.length;
if(nargs<1){
System.out.println("there must be an argument in the command:visa,mastercard");
System.out.println("EXAMPLE:java EnumTest visa");
System.exit(0);
}else{
BankCard c=Enum.valueOf(BankCard.class,args[0].toUpperCase());
System.out.println("yourcredit card"+c.name()+"isaccepted for transaction");
}
//System.out.println("yourcredit card is not accepted for transactionat this point of time");
}
}
enum BankCard{
VISA,
MASTERCARD,
AMERICANEXPRESS,
HSBC,
STANDARDBANK,
ABNAMRO,
ICICI,
HDFC;
}

here iam able to write the program for getting
the valid result when valid query is given in the args
ex:.java CreditCardTest
>yourcreditcard VISA is accepted

but unable toget valid sol like that in file CreditCardtest.java
ie. java CreditCardTest ABNAMRO
>cc not valid for acceptance ABNAMRO at this point of time
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is specific to SCJP certification? I do not think so. Moving to Java In General (intermediate).
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure what you are asking for. Are you looking for a way of replacing all those if-else clauses when using your new enum?

Perhaps the Enum tutorial from Sun will help you.
[ January 22, 2007: Message edited by: Barry Gaunt ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!