Skip to content

Instantly share code, notes, and snippets.

@ckob
Created March 17, 2016 16:25
Show Gist options
  • Save ckob/b2d42768babb91450646 to your computer and use it in GitHub Desktop.
Save ckob/b2d42768babb91450646 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
public class Ferry {
private String regNumber; // Matricula del ferry
private String name; // Nom del ferry
private String harbourName; // Nom del port destí del ferry
private double pricePerAxle;// Preu en € que cobra el ferry al camió per cada eix
private double pricePerTon; // Preu en € que cobra el ferry al camió per cada Tm de pes
private double maxWeight; // Pes màxim en Tm dels camions transportables pel ferry.
private ArrayList<Lorry> boardedLorries; // Els camions carregats al ferry
public Ferry() {
}
public Ferry(String regNumber) {
this(regNumber, "Barcaza", "Puerto Pobre", 25, 15, 200);
}
public Ferry(String regNumber, String name, String harbourName, double pricePerAxle, double pricePerTon, double maxWeight) {
this.regNumber = regNumber;
this.name = name;
this.harbourName = harbourName;
this.pricePerAxle = pricePerAxle;
this.pricePerTon = pricePerTon;
this.maxWeight = maxWeight;
boardedLorries = new ArrayList<>();
}
public double totalWeight() {
int tmp=0;
for (int i = 0; i < boardedLorries.size(); i++) {
tmp+=boardedLorries.get(i).getWeight();
}
return tmp;
}
public boolean isBoarded(Lorry lorry) {
return boardedLorries.contains(lorry);
}
public boolean canBoard(Lorry lorry) {
if (lorry.getWeight() > maxWeight || isBoarded(lorry)) return false;
return true;
}
public boolean board(Lorry lorry) {
if (!canBoard(lorry))
return false;
boardedLorries.add(lorry);
return true;
}
public Lorry lorryInPosition(int n){
return boardedLorries.get(n);
}
public double tollPrice(Lorry lorry) {
return (pricePerAxle*lorry.getNAxles()+pricePerTon*lorry.getWeight());
}
public double collectedTolls() {
double tmp=0;
for (int i = 0; i < boardedLorries.size(); i++) {
tmp+=tollPrice(boardedLorries.get(i));
}
return tmp;
}
@Override
public boolean equals (Object o) {
if (this == o) return true;
if (o == null ) return false;
Ferry ferry = (Ferry) o;
return regNumber.equals(ferry.regNumber);
}
@Override
public int hashCode() {
return regNumber.hashCode();
}
public static void main(String[] args) {
Ferry f = new Ferry("CKB-9875645", "Titanic", "Port de Barcelona", 25, 6, 20);
Lorry[] l = new Lorry[5];
l[0]=new Lorry("1234ABC", 11, 2);
l[1]=new Lorry("4578CPS", 15, 2);
l[2]=new Lorry("7894JKI", 20, 3);
l[3]=new Lorry("6824BOE", 25, 3);
l[4]=new Lorry("5769AAZ", 30, 5);
for (Lorry i: l) {
if (f.board(i)) System.out.println("EMBARCAT: "+i.toString());
else System.out.println("NO EMBARCAT: "+i.toString());
}
System.out.println();
for (Lorry i: l) {
if (f.isBoarded(i)) System.out.println("ESTA EMBARCAT: " + i.toString());
else System.out.println("NO esta embarcat: "+i.toString());
}
System.out.println("Total recaptat: "+f.collectedTolls()+"€");
}
}
/**
* Created by Charly on 3/10/16.
*/
public class Lorry {
private String regNumber; // La matrícula del camió.
private double weight; // El pes del camió en Tm.
private int nAxles; // El nombre d'eixos del camió.
public Lorry() {
}
public Lorry(String regNumber) {
this(regNumber, 25, 6);
}
public Lorry(String regNumber, double weight, int nAxles) {
this.regNumber=regNumber;
this.weight=weight;
this.nAxles=nAxles;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null ) return false;
Lorry lorry = (Lorry) o;
return regNumber.equals(lorry.regNumber);
}
@Override
public int hashCode() {
return regNumber.hashCode();
}
public int getNAxles() {
return nAxles;
}
public double getWeight() {
return weight;
}
public String toString() {
return "regNumber:" +regNumber+", weight:"+weight+", nAxles:"+nAxles;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment