fixed version of DeliveryCar: no problems with index
This commit is contained in:
parent
16291e0e11
commit
d7684ef387
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -6,7 +6,6 @@ import akka.actor.typed.javadsl.AbstractBehavior;
|
|||
import akka.actor.typed.javadsl.ActorContext;
|
||||
import akka.actor.typed.javadsl.Behaviors;
|
||||
import akka.actor.typed.javadsl.Receive;
|
||||
import akka.actor.typed.javadsl.TimerScheduler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -40,9 +39,7 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
return newReceiveBuilder()
|
||||
.onMessage(PickupResponse.class, this::onPickupResponse)
|
||||
.onMessage(LoadMessage.class, this::onLoadMessage)
|
||||
.onMessage(PickupResponse.class, this::onPickupResponse)
|
||||
.onMessage(StartRoute.class, msg->{
|
||||
onStartRoute();return this;})
|
||||
.onMessage(StartRoute.class, this::onStartRoute)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@ -53,88 +50,58 @@ public class DeliveryCar extends AbstractBehavior<DeliveryCar.Message> {
|
|||
}
|
||||
|
||||
private Behavior<Message> onPickupResponse(PickupResponse rsp) {
|
||||
if(rsp.paket.inhalt == null){
|
||||
if (rsp.paket == null || rsp.paket.inhalt == null) {
|
||||
globalIndex++;
|
||||
checkNextStop();
|
||||
}else {
|
||||
if(pakete.size()==3){
|
||||
checkNextStop();
|
||||
}
|
||||
} else {
|
||||
if (pakete.size() < maxCapacity) {
|
||||
pakete.add(rsp.paket);
|
||||
getContext().getLog().info("Aktuelle Anzahl der Pakete im Truck: {}", pakete.size());
|
||||
}
|
||||
globalIndex++;
|
||||
checkNextStop();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
//Die rekursive Methode,die über alle Addressen iteriert
|
||||
|
||||
private void checkNextStop() {
|
||||
//Wenn Wagen voll ist, dann wird erstmal geguckt ob man ein Paket liefern kann
|
||||
if (pakete.size() >= maxCapacity) {
|
||||
deliverPackages();
|
||||
checkNextStop();
|
||||
}
|
||||
//Wenn fertig iteriert so geht der Wagen an den Verteilerzentrum
|
||||
if(globalIndex==4){
|
||||
DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete,getContext().getSelf());
|
||||
//ActorRef<DistributionCenter.Message> antwort1;
|
||||
// antwort1.tell(antwort);
|
||||
|
||||
//Fehlende Antwort an den Verteilerzentrum
|
||||
|
||||
|
||||
//
|
||||
|
||||
}
|
||||
else {
|
||||
//Guckt ob Paket für Kunden da ist und fragt an ob etwas mitgenommen werden muss
|
||||
deliverPackages();
|
||||
} else if (globalIndex >= route.size()) {
|
||||
getContext().getLog().info("Wagen kehrt zum Verteilzentrum zurück.");
|
||||
DistributionCenter.ArriveMessage antwort = new DistributionCenter.ArriveMessage(pakete, getContext().getSelf());
|
||||
// Antwort an das Verteilzentrum senden (die genaue Implementierung hängt von Ihrer Systemarchitektur ab)
|
||||
} else {
|
||||
sendPickupMessage();
|
||||
}
|
||||
}
|
||||
public Behavior<Message> onStartRoute(){
|
||||
/*if(globalIndex==0){
|
||||
checkNextStop();
|
||||
return this;
|
||||
|
||||
}else{
|
||||
if(globalIndex==4) {
|
||||
globalIndex = 0;
|
||||
onStartRoute();
|
||||
}
|
||||
}
|
||||
return this;*/
|
||||
private Behavior<Message> onStartRoute(StartRoute msg) {
|
||||
globalIndex = 0;
|
||||
checkNextStop();
|
||||
return this;
|
||||
}
|
||||
|
||||
//Sendet PickUp Nachricht an den jetztigen Kunden wo der gerade ist.
|
||||
private void sendPickupMessage() {
|
||||
if (globalIndex < route.size()) {
|
||||
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
|
||||
nextCustomer.tell(new Customer.PickUpMessage(getContext().getSelf(), "Wagen kann Paket aufnehmen"));
|
||||
globalIndex++;
|
||||
} else {
|
||||
getContext().getLog().info("Keine weiteren Kunden in der Route.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Prüft ob Paket im Wagen für den Empfänger ist oder nicht, falls ja wird dieser direkt entfernt
|
||||
private void deliverPackages() {
|
||||
if (globalIndex < route.size()) {
|
||||
ActorRef<Customer.Message> nextCustomer = route.get(globalIndex);
|
||||
//Iteriert über alle Pakete
|
||||
for (Paket paket : pakete) {
|
||||
//Falls Empfänger vorhanden so wird dieser aus dem Wagen genommen und geht dann weiter
|
||||
if(pakete.get(globalIndex).empfaenger == nextCustomer ) {
|
||||
for (int i = 0; i < pakete.size(); i++) {
|
||||
Paket paket = pakete.get(i);
|
||||
if (paket.empfaenger == nextCustomer) {
|
||||
nextCustomer.tell(new Customer.DeliveryMessage(paket));
|
||||
pakete.remove(globalIndex);
|
||||
// globalIndex++;
|
||||
pakete.remove(i);
|
||||
i--; // Index anpassen, da ein Element entfernt wurde
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void removeMessage(Paket paket){
|
||||
pakete.remove(paket);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -22,4 +22,3 @@ public class Paket {
|
|||
this.empfaenger = empfaenger;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue