Skip to content

Instantly share code, notes, and snippets.

@ad-m
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save ad-m/bfe08159b75237886747 to your computer and use it in GitHub Desktop.

Select an option

Save ad-m/bfe08159b75237886747 to your computer and use it in GitHub Desktop.
package v3;
public class Bus extends Vehicle {
private int fuelConsumption;
public Bus(int id, int maxSpeed, int fuelConsumption) {
super(id, maxSpeed);
this.fuelConsumption = fuelConsumption;
}
public int getFuelConsumption() {
return fuelConsumption;
}
public void setFuelConsumption(int fuelConsumption) {
this.fuelConsumption = fuelConsumption;
}
@Override
public String toString() {
return "Bus [fuelConsumption=" + fuelConsumption + ", getMaxSpeed()="
+ getMaxSpeed() + ", getId()=" + getId() + "]";
}
}
package v3;
public class BusDepot extends Depot<Bus>{
public BusDepot(String name) {
super(name);
}
public int totalFuelConsumption(){
int sum=0;
for(int i=0; i<this.size(); i++){
sum+=this.get(i).getFuelConsumption();
}
return sum;
}
@Override
public String toString() {
return "BusDepot [totalFuelConsumption()=" + totalFuelConsumption()
+ ", getName()=" + getName() + ", asList()=" + asList() + "]";
}
}
package v3;
import java.util.LinkedList;
public class Depot<T> {
private LinkedList<T> vehicles = new LinkedList<T>();
private String name;
public Depot(String name) {
this.name = name;
}
public int size() {
return vehicles.size();
}
public boolean add(T e) {
return vehicles.add(e);
}
public void clear() {
vehicles.clear();
}
public T get(int index) {
return vehicles.get(index);
}
public T remove(int index) {
return vehicles.remove(index);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LinkedList<T> asList(){
return this.vehicles;
}
@Override
public String toString() {
return "Depot [vehicles=" + vehicles + ", name=" + name + "]";
}
}
package v3;
public class Test {
public Test() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
BusDepot set = new BusDepot("Anna");
set.add(new Bus(1, 50, 10));
set.add(new Bus(2, 10, 15));
set.add(new Bus(3, 30, 20));
System.out.print(set);
TramwayDepot set2 = new TramwayDepot("Anna");
set2.add(new Tramway(1, 50, 10));
set2.add(new Tramway(2, 10, 15));
set2.add(new Tramway(3, 30, 20));
System.out.print(set2);
}
}
package v3;
public class Tramway extends Vehicle {
private int trolleyCount;
public Tramway(int id, int maxSpeed, int trolleyCount) {
super(id, maxSpeed);
this.trolleyCount = trolleyCount;
}
public int getTrolleyCount() {
return trolleyCount;
}
public void setTrolleyCount(int trolleyCount) {
this.trolleyCount = trolleyCount;
}
@Override
public String toString() {
return "Tramway [trolleyCount=" + trolleyCount + ", getMaxSpeed()="
+ getMaxSpeed() + ", getId()=" + getId() + "]";
}
}
package v3;
public class TramwayDepot extends Depot<Tramway>{
public TramwayDepot(String name) {
super(name);
}
public int totalTrolleyCount(){
int sum=0;
for(int i=0; i<this.size(); i++){
sum+=this.get(i).getTrolleyCount();
}
return sum;
}
@Override
public String toString() {
return "TramwayDepot [totalTrolleyCount()=" + totalTrolleyCount()
+ ", getName()=" + getName() + ", asList()=" + asList() + "]";
}
}
package v3;
public class Vehicle {
private int Id;
private int maxSpeed;
public Vehicle(int id, int maxSpeed) {
Id = id;
this.maxSpeed = maxSpeed;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
@Override
public String toString() {
return "Vehicle [Id=" + Id + ", maxSpeed=" + maxSpeed + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment