Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Trailer {
- private int boxesNr;
- private String matriculationNr;
- private static int last = 10;
- public Trailer(final String matriculationNr) {
- this.matriculationNr = matriculationNr;
- this.boxesNr = ++last;
- }
- public Trailer(final String matriculationNr, int boxesNr) {
- this.matriculationNr = matriculationNr;
- last = this.boxesNr = boxesNr;
- }
- public int getBoxesNr() {
- return boxesNr;
- }
- public String getMatriculationNr() {
- return matriculationNr;
- }
- @Override
- public String toString() {
- return "R(" + matriculationNr + ", " + boxesNr + ")";
- }
- }
- class Truck {
- private static final int MAX_LEN = 5;
- private Trailer[] trailers;
- private int len;
- public Truck() {
- trailers = new Trailer[MAX_LEN];
- len = 0;
- }
- public boolean addTrailer(final String matriculationNr, int boxesNr) {
- if (len == MAX_LEN) {
- return false;
- }
- trailers[len++] = new Trailer(matriculationNr, boxesNr);
- return true;
- }
- public boolean addTrailer(final Trailer trailer) {
- if (len == MAX_LEN) {
- return false;
- }
- trailers[len++] = trailer;
- return true;
- }
- public Trailer deleteTrailer(final String matriculationNr) {
- Trailer tmp = null;
- int id = -1;
- for (int i = 0; i < len; i++) {
- if (trailers[i].getMatriculationNr().equals(matriculationNr)) {
- tmp = trailers[i];
- id = i;
- break;
- }
- }
- if (id == -1) {
- return null;
- }
- for (int i = id; i < len - 1; i++) {
- trailers[i] = trailers[i + 1];
- }
- len--;
- return tmp;
- }
- private int getTrailersSum() {
- int sum = 0;
- for (int i = 0; i < len; i++) {
- sum += trailers[i].getBoxesNr();
- }
- return sum;
- }
- @Override
- public boolean equals(Object o) {
- if (o instanceof Truck) {
- Truck other = (Truck)o;
- return getTrailersSum() == other.getTrailersSum();
- }
- return false;
- }
- @Override
- public String toString() {
- String string = "T";
- for (int i = 0; i < len; i++) {
- string += " -> " + trailers[i];
- }
- return string;
- }
- }
- public class Main {
- public static void main(String[] args) {
- Truck truck1 = new Truck(), truck2 = new Truck();
- truck1.addTrailer("1abc", 2);
- truck1.addTrailer("2xyz", 3);
- System.out.println(truck1);
- Trailer trailer = new Trailer("3abc");
- truck2.addTrailer(trailer);
- System.out.println(truck1.equals(truck2));
- truck2.addTrailer("4bfj", 1);
- System.out.println(truck2);
- System.out.println(truck1.equals(truck2));
- truck2.deleteTrailer("3abc");
- System.out.println(truck2);
- System.out.println(truck1.equals(truck2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement