PDA

View Full Version : HELP:Java & Design Patterns


DBASE_WIZ
07-13-2005, 07:17 AM
I got this project that i am working on.I have developed some codes but i am stuck. I need help on the areas in red.

What’s expected? :
Your task is to create an application that uses the Command, Strategy and Chain of Responsibility design patterns to convert user-specified dollar amounts input from the command line to individual coin values.


Command pattern:
The Command pattern lets an application framework make requests of application-specific objects, without the framework knowing the objects' exact type or the application-specific behavior they implement. (Geary)


Strategy pattern:
The Strategy pattern decouples algorithms so that programs can be more flexible in their execution and logic


Chain of Responsibility pattern:
The Chain of Responsibility pattern reduces coupling by allowing several objects the opportunity to handle a request.


Notice if the user enters a ‘$’ in the specified dollar amount, then the Strategy pattern check will just output the line ‘$ FOUND’ and skip over the Chain of Responsibility logic.


Specification:

•You are to write a class called CommandStrategyChainOfRespPattern, & the following interface:

public interface Command {
public void execute();
}

public class CommandStrategyChainOfRespPattern
{
private static Logger logger =...
private String stringAmount;

public CommandStrategyChainOfRespPattern(String stringAmount)
{
this.stringAmount = stringAmount;
}

public void applyChainOfResponsibility()
{
if (!testStrategy( new startsWithDollarSign(), stringAmount))
{
// generate random # between 1 and 2
switch(number){
case 1:
// invoke USCoins method using Wrapper class to convert
// string amount to double value
break;
case 2:
// invoke CanadianCoins method using Wrapper class to
// convert
// string amount to double value
break;}
}
else { logger.info(""); }
}

public interface TestStrategy
{
// insert code here }

public boolean testStrategy(TestStrategy strategyApproach, String s)
{
// insert code here
}

public class startsWithDollarSign implements TestStrategy
{
public boolean test(String s)
{
// check input string value for null value, return false if found
// check input string value to see if it starts with ‘$’; return boolean
// value of condition found (true/false)
}}

public static void main(String args[])
{
if (args.length != 1)
{
logger.info("USAGE: java CommandStrategyChainOfRespPattern <amount>");
System.exit(0);
}
new CommandStrategyChainOfRespPattern(args[0]).applyChainOfResponsibility();
}

}

•]You are to write a class called USCoins and CanadianCoins, which implement the following code. NOTE: The CanadianCoins class will implement logic to convert the dollar amount input by the user to .75 of the US Coin amount to account for the difference in currency rates. public class USCoins implements Command
{
private double amount = 0;
private QuarterHandler quarterHandler;
private DimeHandler dimeHandler;
private NickelHandler nickelHandler;
private PennyHandler pennyHandler;

public USCoins(double amount)
{
// setup chain of responsibility pattern implementation
this.amount = amount;
try
{
quarterHandler = new QuarterHandler();
dimeHandler = new DimeHandler();
nickelHandler = new NickelHandler();
pennyHandler = new PennyHandler();

quarterHandler.setSuccessor( dimeHandler );
dimeHandler.setSuccessor( nickelHandler );
nickelHandler.setSuccessor( pennyHandler );
}
catch( Exception e ) { // exception code here }
}

public void execute()
{
int coinAmount = <convert coin amount to integer value>

quarterHandler.handleRequest(coinAmount);

logger.info(“get count values from coin handler references…”);
}
}

•Your applications will use the Logger class to render program status text to the Java console.
•You will provide Javadoc artifacts with your submission.

This file must contain your executable JAR file with all your source code and its compiled classes, as well as Javadoc artifacts. If your work requires any special compilation instructions or other documentation, please add a file called README.txt that contains any additional information needed to run your code.

Submission Checklist:

• CommandStrategyChainOfRespPattern.java

• CanadianCoins.java
• USCoins.java
• Command.java

• QuarterHandler.java
• DimeHandler.java
• NickelHandler.java
• PennyHandler.java
• TestHandler.java

• Executable JAR file called CommandStrategyChainOfRespPattern.jar
• Javadoc artifacts

Roelf
07-13-2005, 09:37 AM
Smells like a homework assignment (http://www.codingforums.com/showthread.php?t=53446)

suryad
07-13-2005, 10:23 AM
Heh it does and quite a complicated one at that...is it for a masters program or what?

DBASE_WIZ
07-14-2005, 05:33 AM
i need help guys.it is a masters program yes and i have done some work and i just need some help please