Skip to content

Instantly share code, notes, and snippets.

@nicopaez
Last active May 11, 2016 03:26
Show Gist options
  • Save nicopaez/76d7fcc2915fb3ee06a47d619ec169c3 to your computer and use it in GitHub Desktop.
Save nicopaez/76d7fcc2915fb3ee06a47d619ec169c3 to your computer and use it in GitHub Desktop.
/*
El siguiente grupo de clases modela un dominio de alquiler de bicicletas.
1. Analice las clases
2. Realice un diagrama de clases
3. Realice un diagrama de secuencia del método Customer.amountToPay()
4. Identifique potenciales problemas con el diseño/código presentado e indique porque considera que son problemas
5. Proponga una solución para cada uno de los puntos identificados
*/
public class Bike {
private int dailyPrice;
private int price;
public void setDailyPrice(int dailyPrice) {
this.dailyPrice = dailyPrice;
}
public int getDailyPrice() {
return dailyPrice;
}
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return price;
}
}
public class Rental {
private final Bike bike;
private int period;
private boolean isDaily;
public Rental(Bike aBike) {
this.bike = aBike;
}
public boolean isDaily() {
return isDaily;
}
public Bike getBike() {
return bike;
}
public void setPeriod(int period) {
this.period = period;
}
public int getPeriod() {
return period;
}
public void setIsDaily(boolean isDaily) {
this.isDaily = isDaily;
}
}
public class Customer {
private List<Rental> rentals;
public Customer() {
this.rentals = new ArrayList<Rental>();
}
public void addRental(Rental rental) {
this.rentals.add(rental);
}
public int amountToPay() {
int result = 0;
for (Rental r: this.rentals) {
if (r.isDaily()) {
result += r.getBike().getDailyPrice();
}
else {
result += r.getBike().getPrice() * r.getPeriod();
}
}
return result;
}
}
// ejemplo de uso 1
Bike aBike = new Bike();
aBike.setPrice(3);
Rental rental = new Rental(aBike);
rental.setPeriod(5);
Customer aCustomer = new Customer();
aCustomer.addRental(rental);
int amount = aCustomer.amountToPay();
// ejemplo de uso 2
Bike aBike = new Bike();
aBike.setDailyPrice(10);
Rental rental = new Rental(aBike);
rental.setIsDaily(true);
Customer aCustomer = new Customer();
aCustomer.addRental(rental);
int amount = aCustomer.amountToPay();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment