Teil 1 / Aufgabe 5

This commit is contained in:
Sebastian Brosch 2024-05-21 14:48:00 +02:00
parent acb7881eb6
commit 949ad201d7
4 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,44 @@
package T01.Aufgabe05;
/**
* Class to represent a ArrayQueue (using an array).
*/
public class ArrayQueue {
private Produkt queue[] = new Produkt[100];
/**
* Method to add a product to the Queue.
*
* @param produkt The product to add.
*/
public void add(Produkt produkt) {
for (int i = 0; i < this.queue.length; i++) {
if (queue[i] == null) {
int j = i;
while (j > 0) {
queue[j] = queue[j - 1];
j--;
}
break;
}
}
queue[0] = produkt;
}
/**
* Method to take a product from the Queue.
*
* @return The product taken from the Queue.
*/
public Produkt take() {
Produkt produkt = queue[0];
for (int i = 0; i < queue.length; i++) {
if (queue[i] == null) {
break;
}
queue[i] = queue[i + 1];
}
return produkt;
}
}

View File

@ -0,0 +1,33 @@
package T01.Aufgabe05;
/**
* Teil 1 / Aufgabe 5
*
* @author Sebastian Brosch
*/
public class Aufgabe05 {
public static void main(String[] args) {
System.out.println("\nQueue with Array:");
ArrayQueue queueA = new ArrayQueue();
queueA.add(new Produkt(1, "Salz", 10.0, Regalnummer.Regal01));
queueA.add(new Produkt(2, "Zucker", 15.0, Regalnummer.Regal02));
queueA.add(new Produkt(3, "Mehl", 10.0, Regalnummer.Regal03));
queueA.take().print();
queueA.take().print();
queueA.take().print();
System.out.println("\nQueue with List:");
ListQueue queueB = new ListQueue();
queueB.add(new Produkt(1, "Salz", 10.0, Regalnummer.Regal01));
queueB.add(new Produkt(2, "Zucker", 15.0, Regalnummer.Regal02));
queueB.add(new Produkt(3, "Mehl", 10.0, Regalnummer.Regal03));
queueB.take().print();
queueB.take().print();
queueB.take().print();
}
}

View File

@ -0,0 +1,30 @@
package T01.Aufgabe05;
import java.util.ArrayList;
/**
* Method to represent a Queue (using a List).
*/
public class ListQueue {
ArrayList<Produkt> queue = new ArrayList<>();
/**
* Method to add a product to the Queue.
*
* @param produkt The product to add.
*/
public void add(Produkt produkt) {
queue.add(0, produkt);
}
/**
* Method to take a product from the Queue.
*
* @return The product taken from the Queue.
*/
public Produkt take() {
Produkt produkt = queue.get(0);
queue.remove(0);
return produkt;
}
}

View File

@ -0,0 +1,30 @@
package T01.Aufgabe05;
/**
* List with all available shelf numbers.
*/
enum Regalnummer {
Regal01, Regal02, Regal03, Regal04, Regel05
}
/**
* Class to represent a Produkt.
*/
public class Produkt {
int id;
String name;
double nettopreis;
Regalnummer regal;
Produkt(int id, String name, double nettopreis, Regalnummer regal) {
this.id = id;
this.name = name;
this.nettopreis = nettopreis;
this.regal = regal;
}
void print() {
System.out.printf("%d - %s: %.2f (%s)\n", this.id, this.name, this.nettopreis,
this.regal.toString());
}
}