POO-ITBA / 2023_02

0 stars 0 forks source link

PARCIAL 2022-1C EJERCICIO3 #9

Open Jalbertonisalini opened 1 year ago

Jalbertonisalini commented 1 year ago

Buenas! Queria preguntar por esta resolucion. Esto imprime lo que debe pero comparando con la solucion veo que puede ser que se repita mucho codigo... Queria saber de esta manera que tan mal está, es decir que tan penalizable es. Muchas gracias!

`package AR.EDU.ITBA.POO._PARCIALES1P._20221C.EJERCICIO3;

import java.util.Objects;

public class Attraction {

private final String name;

public Attraction(String name){
    this.name = name;
}

public String getName() {
    return name;
}

@Override
public boolean equals(Object obj) {
    if(this == obj){
        return true;
    }
    if(!(obj instanceof Attraction aux)){
        return false;
    }
    return name.equals(aux.getName());
}

@Override
public int hashCode() {
    return Objects.hash(name);
}

} `

`package AR.EDU.ITBA.POO._PARCIALES1P._20221C.EJERCICIO3;

import java.time.LocalDate;

public abstract class Pass implements CityPass{

private String clientName;
private LocalDate emissionDate;
private Attraction[] attractions;
private int visitCount;

public Pass(Attraction[] attractions, String clientName, LocalDate emissionDate){
    this.clientName = clientName;
    this.emissionDate = emissionDate;
    this.attractions = attractions;
    visitCount = 0;
}

public LocalDate getEmissionDate() {
    return emissionDate;
}

public void IncrementVisitCount(){
    visitCount++;
}

@Override
public Attraction[] getAttractions() {
    return attractions;
}

public String getClientName() {
    return clientName;
}

public boolean canVisit(Attraction attraction, LocalDate date){
    return belongsIn(attraction) &&  (this.emissionDate.isBefore(date) || this.emissionDate.isEqual(date));
}

@Override
public String toString() {
    return "City Pass for %s used for %d attractions".formatted(clientName,visitCount);
}

} `

`package AR.EDU.ITBA.POO._PARCIALES1P._20221C.EJERCICIO3;

import java.time.LocalDate;

public class UnlimitedPass extends Pass{

public UnlimitedPass(Attraction[] attractions, String name, LocalDate emissionDate){
    super(attractions,name,emissionDate);
}

@Override
public void visit(Attraction attraction, LocalDate date) {
    if (canVisit(attraction, date)) {
        IncrementVisitCount();
        System.out.println("%s visited %s".formatted(getClientName(), attraction.getName()));
        return;
    }
    throw new CannotVisitAttractionException(attraction.getName());
}

} `

`package AR.EDU.ITBA.POO._PARCIALES1P._20221C.EJERCICIO3;

import java.time.LocalDate;

public class LimitedVisitsPass extends Pass{

private int availablePass;

public LimitedVisitsPass(Attraction[] attractions, String name, LocalDate emissionDate, int availablePass){
    super(attractions,name,emissionDate);
    this.availablePass = availablePass;
}

public void spendVisit(){
    availablePass--;
}

@Override
public void visit(Attraction attraction, LocalDate date) {
    if (canVisit(attraction,date)){
        if(availablePass > 0){
            System.out.println("%s visited %s".formatted(getClientName(), attraction.getName()));
            IncrementVisitCount();
            spendVisit();
            return;
        }
    }
    throw new CannotVisitAttractionException(attraction.getName());
}

} `

`package AR.EDU.ITBA.POO._PARCIALES1P._20221C.EJERCICIO3;

import java.time.LocalDate;

public class endDatePass extends Pass{

private LocalDate endDate;

public endDatePass(Attraction[] attractions, String name, LocalDate emissionDate, LocalDate endDate){
    super(attractions,name,emissionDate);
    this.endDate = endDate;
}

@Override
public void visit(Attraction attraction, LocalDate date) {
    if (canVisit(attraction,date)){
        if (endDate.isAfter(date) || endDate.isEqual(date)){
            System.out.println("%s visited %s".formatted(getClientName(), attraction.getName()));
            IncrementVisitCount();
            return;
        }
    }
    throw new CannotVisitAttractionException(attraction.getName());
}

} `

fmeola commented 1 year ago

Hola @Jalbertonisalini Sí, vamos a penalizar que estés repitiendo código del método visit. En las tres haces: