Skip to content

Instantly share code, notes, and snippets.

@delucas
Created October 10, 2012 03:46
Show Gist options
  • Save delucas/3863043 to your computer and use it in GitHub Desktop.
Save delucas/3863043 to your computer and use it in GitHub Desktop.
UNLaM - TW1 - Billetera
package ar.edu.unlam.tallerweb.billetera;
public class BilleteException extends Exception {
private static final long serialVersionUID = 8656459465486574L;
public BilleteException(String message){
super(message);
}
}
package ar.edu.unlam.tallerweb.billetera;
import java.math.BigDecimal;
public class Billete {
private BigDecimal monto;
private String moneda;
public Billete(BigDecimal monto, String moneda) {
this.monto = monto;
this.moneda = moneda;
}
public Billete sumar (Billete otro) throws BilleteException {
if (this.moneda.equals(otro.moneda)){
return new Billete (this.monto.add(otro.monto), this.moneda);
} else{
throw new BilleteException("No se pueden sumar Billetes de diferente Moneda");
}
}
public BigDecimal getMonto() {
return this.monto;
}
public String getMoneda() {
return this.moneda;
}
}
package ar.edu.unlam.tallerweb.billetera;
import java.math.BigDecimal;
import org.junit.Assert;
import org.junit.Test;
public class BilleteraTest {
@Test
public void testQueSePuedaSumar() throws BilleteException{
Billete b1 = new Billete(new BigDecimal("1"),"pesos");
Billete b2 = new Billete(new BigDecimal("1"),"pesos");
Billete b3 = b1.sumar(b2);
Assert.assertEquals(new BigDecimal("2"), b3.getMonto());
Assert.assertEquals("pesos", b3.getMoneda());
}
@Test(expected=BilleteException.class)
public void testQueNoSePuedaSumar() throws BilleteException{
Billete b1 = new Billete(new BigDecimal("1"),"pesos");
Billete b2 = new Billete(new BigDecimal("1"),"dolares");
b1.sumar(b2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment