Created
February 5, 2017 14:10
-
-
Save 13andrew13/79b69124b01cca3ac40354fe24fd5bf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FamilyTariff implements TaxiTarrif { | |
@Override | |
public long calculatePrice(TaxiRide ride){ | |
return 50+20*ride.getDistance()/ride.getPassengers(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StandartTariff implements TaxiTarrif { | |
@Override | |
public long calculatePrice(TaxiRide ride) { | |
return 30+5*ride.getDistance()+2*ride.getDuration(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TaxiRide { | |
private int passengers; | |
private long distance; | |
private long duration; | |
private TaxiTarrif tarrif; | |
public TaxiRide(){ | |
} | |
public TaxiRide(long duration,long distance,TaxiTarrif taxiTarrif){ | |
passengers = 1; | |
check(distance); | |
this.duration=duration; | |
check(distance); | |
this.distance = distance; | |
tarrif = taxiTarrif; | |
} | |
public TaxiRide(int passengers,long distance, long duration,TaxiTarrif taxiTarrif){ | |
check(passengers); | |
this.passengers=passengers; | |
check(distance); | |
this.distance=distance; | |
check(duration); | |
this.duration=duration; | |
tarrif = taxiTarrif; | |
} | |
public int getPassengers() { | |
return passengers; | |
} | |
public long getDistance() { | |
return distance; | |
} | |
public long getDuration() { | |
return duration; | |
} | |
public long getPrice(){ | |
return tarrif.calculatePrice(this); | |
} | |
public String toString(){ | |
return "Ride \nduration:" +this.duration + " min\ndistance: " + this.distance + " km\n" + "price: " + getPrice()+"$ \n"; | |
} | |
private void check(int passengers){ | |
if(passengers<0){ | |
throw new IllegalArgumentException("This value can't be less or equal 0!"); | |
} | |
} | |
private void check(long value){ | |
if(value<0){ | |
throw new IllegalArgumentException("This value can't be less or equal 0!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TaxiRide { | |
private int passengers; | |
private long distance; | |
private long duration; | |
private TaxiTarrif tarrif; | |
public TaxiRide(){ | |
} | |
public TaxiRide(long duration,long distance,TaxiTarrif taxiTarrif){ | |
passengers = 1; | |
check(distance); | |
this.duration=duration; | |
check(distance); | |
this.distance = distance; | |
tarrif = taxiTarrif; | |
} | |
public TaxiRide(int passengers,long distance, long duration,TaxiTarrif taxiTarrif){ | |
check(passengers); | |
this.passengers=passengers; | |
check(distance); | |
this.distance=distance; | |
check(duration); | |
this.duration=duration; | |
tarrif = taxiTarrif; | |
} | |
public int getPassengers() { | |
return passengers; | |
} | |
public long getDistance() { | |
return distance; | |
} | |
public long getDuration() { | |
return duration; | |
} | |
public long getPrice(){ | |
return tarrif.calculatePrice(this); | |
} | |
public String toString(){ | |
return "Ride \nduration:" +this.duration + " min\ndistance: " + this.distance + " km\n" + "price: " + getPrice()+"$ \n"; | |
} | |
private void check(int passengers){ | |
if(passengers<0){ | |
throw new IllegalArgumentException("This value can't be less or equal 0!"); | |
} | |
} | |
private void check(long value){ | |
if(value<0){ | |
throw new IllegalArgumentException("This value can't be less or equal 0!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TaxiRidesRunner { | |
public static void main(String[] args) { | |
TaxiTarrif tarrif = new StandartTariff(); | |
TaxiTarrif tarrif1 = new FamilyTariff(); | |
TaxiRide taxiRide1 = new TaxiRide(5,3,4,tarrif); | |
TaxiRide taxiRide2 = new TaxiRide(8,5,1,tarrif1); | |
TaxiRide taxiRide3 = new TaxiRide(1,8,40,tarrif1); | |
TaxiRides rides = new TaxiRides(); | |
rides.add(taxiRide1); | |
rides.add(taxiRide2); | |
rides.add(taxiRide3); | |
rides.printRides(); | |
System.out.println("During the day, I earned " + rides.getPrice() + "$"); | |
//System.out.println(taxiRide1.toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface TaxiTarrif { | |
public long calculatePrice(TaxiRide ride); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment