Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Car {
- private String matNr;
- private int necFuel;
- public Car(final String matNr, int necFuel) {
- this.matNr = matNr;
- this.necFuel = necFuel;
- }
- public int getNecFuel() {
- return necFuel;
- }
- @Override
- public String toString() {
- return matNr + " - " + necFuel + "\n";
- }
- }
- class GasStation {
- private int fuel = 0;
- private Car[] cars;
- private int len = 0;
- public GasStation() {
- cars = new Car[10];
- }
- boolean feedCar(final Car car) {
- if (fuel >= car.getNecFuel()) {
- fuel -= car.getNecFuel();
- return true;
- }
- if (len != cars.length) {
- cars[len++] = car;
- return true;
- }
- return false;
- }
- @Override
- public String toString() {
- String string = "GS: " + fuel + " Q:\n";
- for (int i = 0; i < len; i++) {
- string += cars[i];
- }
- return string;
- }
- public void feedMe(int fuel) {
- this.fuel += fuel;
- int cnt = 0;
- while (cars[cnt] != null && this.fuel >= cars[cnt].getNecFuel()) {
- this.fuel -= cars[cnt].getNecFuel();
- cnt++;
- }
- for (int i = cnt; i < len; i++) {
- cars[i - cnt] = cars[i];
- cars[i] = null;
- }
- len -= cnt;
- }
- }
- public class Main {
- public static void main(String[] args) {
- Car car1 = new Car("12abc", 5);
- Car car2 = new Car("12vds", 10);
- Car car3 = new Car("12gef", 15);
- GasStation gs = new GasStation();
- gs.feedMe(6);
- gs.feedCar(car2);
- gs.feedCar(car1);
- gs.feedCar(car3);
- gs.feedCar(car1);
- System.out.println(gs);
- gs.feedMe(29);
- System.out.println(gs);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement