Advertisement
STANAANDREY

trucks

Oct 13th, 2023 (edited)
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. class Trailer {
  2.     private int boxesNr;
  3.     private String matriculationNr;
  4.     private static int last = 10;
  5.  
  6.     public Trailer(final String matriculationNr) {
  7.         this.matriculationNr = matriculationNr;
  8.         this.boxesNr = ++last;
  9.     }
  10.     public Trailer(final String matriculationNr, int boxesNr) {
  11.         this.matriculationNr = matriculationNr;
  12.         last = this.boxesNr = boxesNr;
  13.     }
  14.  
  15.     public int getBoxesNr() {
  16.         return boxesNr;
  17.     }
  18.  
  19.     public String getMatriculationNr() {
  20.         return matriculationNr;
  21.     }
  22.  
  23.     @Override
  24.     public String toString() {
  25.         return "R(" + matriculationNr + ", " + boxesNr + ")";
  26.     }
  27. }
  28.  
  29. class Truck {
  30.     private static final int MAX_LEN = 5;
  31.     private Trailer[] trailers;
  32.     private int len;
  33.  
  34.     public Truck() {
  35.         trailers = new Trailer[MAX_LEN];
  36.         len = 0;
  37.     }
  38.  
  39.     public boolean addTrailer(final String matriculationNr, int boxesNr) {
  40.         if (len == MAX_LEN) {
  41.             return false;
  42.         }
  43.         trailers[len++] = new Trailer(matriculationNr, boxesNr);
  44.         return true;
  45.     }
  46.  
  47.     public boolean addTrailer(final Trailer trailer) {
  48.         if (len == MAX_LEN) {
  49.             return false;
  50.         }
  51.         trailers[len++] = trailer;
  52.         return true;
  53.     }
  54.  
  55.     public Trailer deleteTrailer(final String matriculationNr) {
  56.         Trailer tmp = null;
  57.         int id = -1;
  58.         for (int i = 0; i < len; i++) {
  59.             if (trailers[i].getMatriculationNr().equals(matriculationNr)) {
  60.                 tmp = trailers[i];
  61.                 id = i;
  62.                 break;
  63.             }
  64.         }
  65.         if (id == -1) {
  66.             return null;
  67.         }
  68.         for (int i = id; i < len - 1; i++) {
  69.             trailers[i] = trailers[i + 1];
  70.         }
  71.         len--;
  72.         return tmp;
  73.     }
  74.  
  75.     private int getTrailersSum() {
  76.         int sum = 0;
  77.         for (int i = 0; i < len; i++) {
  78.             sum += trailers[i].getBoxesNr();
  79.         }
  80.         return sum;
  81.     }
  82.  
  83.     @Override
  84.     public boolean equals(Object o) {
  85.         if (o instanceof Truck) {
  86.             Truck other = (Truck)o;
  87.             return getTrailersSum() == other.getTrailersSum();
  88.         }
  89.         return false;
  90.     }
  91.  
  92.     @Override
  93.     public String toString() {
  94.         String string = "T";
  95.         for (int i = 0; i < len; i++) {
  96.             string += " -> " + trailers[i];
  97.         }
  98.         return string;
  99.     }
  100. }
  101.  
  102. public class Main {
  103.     public static void main(String[] args) {
  104.         Truck truck1 = new Truck(), truck2 = new Truck();
  105.         truck1.addTrailer("1abc", 2);
  106.         truck1.addTrailer("2xyz", 3);
  107.         System.out.println(truck1);
  108.         Trailer trailer = new Trailer("3abc");
  109.         truck2.addTrailer(trailer);
  110.         System.out.println(truck1.equals(truck2));
  111.         truck2.addTrailer("4bfj", 1);
  112.         System.out.println(truck2);
  113.         System.out.println(truck1.equals(truck2));
  114.         truck2.deleteTrailer("3abc");
  115.         System.out.println(truck2);
  116.         System.out.println(truck1.equals(truck2));
  117.     }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement