_MuradPro_

BusCompany

Sep 28th, 2020
760
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. public class BusCompany {
  3.     private String name;
  4.     private Bus[] buses;
  5.     private Line[] lines;
  6.     private Trip[] trips;
  7.    
  8.     public BusCompany(String name) {
  9.         this.name = name;
  10.         this.buses = new Bus[100];
  11.         this.lines = new Line[50];
  12.         this.trips = new Trip[1000];
  13.     }
  14.    
  15.     public String toString() {
  16.         int busCount=0;
  17.         for (int i = 0; i < buses.length; i++) {
  18.             if(buses[i] != null)
  19.                 busCount++;
  20.         }
  21.         int tripCount=0;
  22.         for (int i = 0; i < trips.length; i++) {
  23.             if(trips[i] != null)
  24.                 tripCount++;
  25.         }
  26.         return "The bus company " + name + " has " + busCount + " buses and " + tripCount + " trips";
  27.        
  28.     }
  29.    
  30.     public boolean addTrip(Trip trip) {
  31.         boolean added = false;
  32.         for(int i=0; i<trips.length && added == false; i++) {
  33.             if(trips[i] == null) {
  34.                 trips[i] = trip;
  35.                 added = true;
  36.             }
  37.         }
  38.         return added;
  39.     }
  40.    
  41.     public boolean removeTrip(Trip trip) {
  42.         boolean found = false;
  43.         for(int i=0; i<trips.length && found == false; i++) {
  44.             if(trips[i] == trip) {
  45.                 trips[i] = null;
  46.                 found = true;
  47.             }
  48.         }
  49.         return found;
  50.     }
  51.  
  52.     public String getName() {
  53.         return name;
  54.     }
  55.  
  56.     public void setName(String name) {
  57.         this.name = name;
  58.     }
  59.    
  60.     public boolean addBus(Bus bus) {
  61.         boolean added = false;
  62.         for(int i=0; i<buses.length && added == false; i++) {
  63.             if(buses[i] == null) {
  64.                 buses[i] = bus;
  65.                 added = true;
  66.             }
  67.         }
  68.         return added;
  69.     }
  70.    
  71.     public boolean removeBus(int bus_num) {
  72.         boolean found = false;
  73.         for(int i=0; i<buses.length; i++) {
  74.             if(buses[i] != null) {
  75.                 if(buses[i].getBus_name() == bus_num) {
  76.                     found = true;
  77.                     buses[i] = null;
  78.                 }
  79.             }
  80.         }
  81.         return found;
  82.     }
  83. }
RAW Paste Data