/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
public class Main
{
public static void main(String[] args) {
Company c1 = new Company("Company");
Bus b1 = new Bus(1,50);
c1.addBus(b1);
c1.toString();
}
}
public class Bus{
private int id, seats;
public Bus(int id, int seats){
setId(id);
setSeats(seats);
}
public Bus(Bus other){
this(other.id, other.seats);
}
public int getId(){
return this.id;
}
public void setId(int id){
this.id = id;
}
public int getSeats(){
return this.seats;
}
public void setSeats(int seats){
this.seats = seats;
}
public String toString(){
return "id: " + id + "; num of seats: " + seats;
}
}
public class Company{
private String name;
private Bus[] buses = new Bus[100];
public Company(String name){
setName(name);
for (int i = 0; i < buses.length; i++){
buses[i] = new Bus(0,0);
}
}
public int searchForId(int id){
for (int i = 0; i < buses.length; i++){
if (buses[i].getId() == id) return i;
}
return -1;
}
public void addBus(Bus newBus){
if(searchForId(newBus.getId()) != -1) System.out.println("the bus exist already in the company");
else{
int s = searchForId(0);
if (s == -1) System.out.println("the company can't hold more buses!");
else buses[s] = newBus;
}
}
public void removeBus(Bus dumped){
int s = searchForId(dumped.getId());
if (s != -1){
buses[s].setId(0);
buses[s].setSeats(0);
}
else System.out.println("this bus doesn't exist");
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public String toString(){
String toString = "name: " + name + "\n buses:\n";
for (int i = 0; i < buses.length; i++){
toString = toString + buses[i].toString() + "\n";
}
return toString;
}
}