Skip to content

Instantly share code, notes, and snippets.

@vestel
Created August 6, 2009 14:48
Show Gist options
  • Save vestel/163350 to your computer and use it in GitHub Desktop.
Save vestel/163350 to your computer and use it in GitHub Desktop.
package sef.module7.activity;
import java.util.Calendar;
/**
* Represents an implementation of the Currency interface. The
* equality test for this currency implementation requires that the
* value and the name of the denomination must be true in order to
* be considered equal.
*
* @author John Doe
*
*/
public class CurrencyImpl implements Currency {
private float value;
private Denomination denomination;
private final Calendar createDate = Calendar.getInstance();
/**
* Creates an instance of the of the Currency class with the
* specified value and denomination
*
* @param value
* the value of the currency
* @param denomination
* the denomination of the currency
*/
public CurrencyImpl(float value, Denomination denomination) {
this.value = value;
this.denomination = new DenominationImpl(
denomination.getName(),
denomination.getDescription(),
denomination.getSymbol());
}
/*
* (non-Javadoc)
*
* @see sef.module6.activity.Currency#getValue()
*/
public float getValue() {
return this.value;
}
/*
* (non-Javadoc)
*
* @see sef.module6.activity.Currency#getDenomination()
*/
public Denomination getDenomination() {
Denomination d = new DenominationImpl(
this.denomination.getName(),
this.denomination.getDescription(),
this.denomination.getSymbol());
return d;
}
/*
* (non-Javadoc)
*
* @see sef.module6.activity.Currency#getCreateDate()
*/
public Calendar getCreateDate() {
Calendar cd=Calendar.getInstance();
cd.setTimeInMillis(createDate.getTimeInMillis());
return cd;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
return Float.valueOf(this.value) + " "
+ this.denomination.toString();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o == null) {
return false;
}
Currency c;
try {
c = (Currency) o;
} catch (Exception e) {
return false;
}
if (c.getValue() == this.getValue()
&& c.getDenomination().getName().equals(
denomination.getName())) {
return true;
} else {
return false;
}
}
}
package sef.module7.activity;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
/**
* An implementation of the Denomination interface. The equality
* test for between instances of this class considers the name and
* the symbol
*
* @author John Doe
*
*/
public class DenominationImpl implements Denomination {
private String name;
private String description;
private String symbol;
/**
* Creates a new instance with the specified parameters
*
* @param name
* the name of the denomination
* @param description
* a description
* @param symbol
*/
public DenominationImpl(String name, String description,
String symbol) {
if ((name == null) || (description == null)
|| (symbol == null)) {
throw new IllegalArgumentException("Cannot be null!");
}
this.name = name;
this.description = description;
this.symbol = symbol;
}
/*
* (non-Javadoc)
*
* @see sef.module6.activity.Denomination#getDescription()
*/
public String getDescription() {
return this.description;
}
/*
* (non-Javadoc)
*
* @see sef.module6.activity.Denomination#getName()
*/
public String getName() {
return this.name;
}
/*
* (non-Javadoc)
*
* @see sef.module6.activity.Denomination#getSymbol()
*/
public String getSymbol() {
return this.symbol;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.name + " (" + this.symbol + ") "
+ this.description;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o == null) {
return false;
}
Denomination d;
try {
d = (Denomination) o;
} catch (Exception e) {
return false;
}
if (d.getName().equals(name)
&& d.getSymbol().equals(symbol)) {
return true;
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment