Java Tutorial #9 Creating a carwash app with Queues!

in #programming7 years ago

Previous tutorial https://steemit.com/programming/@nicetea/java-tutorial-8-creating-a-multithreaded-dice-roll-app

In this tutorial, we will get directly started with the code, as it should be pretty self-explanatory.

Car.java

public class Car {
    private String typ;
    private String number;

    public Car(String typ, String number) {
        this.typ = typ;
        this.number = number;
    }

    public String toString() {
        return "Car: " + typ + " with Number: " + number;
    }
}

As usual, first the attributes and the standart constructor. This time, we will add the toString() method, which will get called if an object of this class is called directly(we will see the usage later).

SuperCarWash.java


package Uebung8_2;

import java.util.LinkedList;
import java.util.Queue;

public class SuperCarWash {

    private Queue<Car> a;

    public SuperCarWash() {
        a = new LinkedList<Car>();

    }

    public void driveIn(Car a) {
        this.a.offer(a);
    }

    public Car driveOut() {
        return this.a.poll();
    }

    public static void main(String[] args) {
        Car audi = new Car("Audi", "X765A3");
        Car mercedes = new Car("Mercedes", "A309DA");
        Car opel = new Car("Opel", "B3RT90");
        SuperCarWash SuperWash = new SuperCarWash();
        SuperWash.driveIn(audi);
        SuperWash.driveIn(mercedes);
        SuperWash.driveIn(opel);
        for (int i = 1; i <= 3; i++)
            System.out.println(i + ". " + SuperWash.driveOut() + " ist sauber");
    }

}

Firstly, we define a new car queue with the unbeliveable creative name a. In the public constructor, we are declaring, that the queue is used as LinkedList. Lookup polymorphism in java if you want to know more about that!
Next, we are creating 2 methods. driveIn(Car a) and driveOut(). With this.a.offer(a); We are "offering" the a LinkedList a new object for the list.(Object a will get added to the list). With this.a.poll();, we will remove the first added object from the queue.

In the main of the app, we are now creating a few cars by using:

        Car audi = new Car("Audi", "X765A3");
        Car mercedes = new Car("Mercedes", "A309DA");
        Car opel = new Car("Opel", "B3RT90");

Next, we are creating the actual carwash object and adding the previously created cars to it.

        SuperCarWash SuperWash = new SuperCarWash();
        SuperWash.driveIn(audi);
        SuperWash.driveIn(mercedes);
        SuperWash.driveIn(opel);

Lastly, in the for loop, we will let the cleaned cars drive out of the steet and print the output.

Note: Notice that SuperWash.driveOut() is called and returns the output of the toString() method which we defined earlier? That's exactly what toString() does!
When we call driveOut() a Car object is returned. The toString() method is going to print a string instead of an actual object address. So that's why we see an output like this:

Selection_036.png

Stay tuned!

Coin Marketplace

STEEM 0.17
TRX 0.16
JST 0.029
BTC 75005.44
ETH 2818.62
USDT 1.00
SBD 2.54