It is a part of code of project in Anylogic, where i need to process requests and every time, when i try it – the same 1 request unprocessed.
for (Request curRequest : request) {
System.out.print(curRequest + " ");
}
//where we can find all requests
Collections.sort(request, new Comparator<Request>() {
@Override
public int compare(Request r1, Request r2) {
//System.out.println(r1.toString() +" "+ r2.toString());
return Double.compare(r2.pricePriority, r1.pricePriority);
}
});
/*for (Request curRequest : request) {
System.out.print(curRequest + " ");
}*/
//the same, but there is some problem with java.lang.IndexOutOfBoundsException, which not influance on all work of code
Iterator<Request> iterator = request.iterator();
while (iterator.hasNext()) {
Request currentRequest = iterator.next();
System.out.println(currentRequest);
//where you can find only 8 items
Clients currentClient = null;
for (Clients client : clients) {
if (client.name.equals(currentRequest.to)) {
currentClient = client;
break;
}
}
Warehouse currentWarehouse = null;
for (Warehouse warehouse : warehouse) {
if (warehouse.codeOfWarehouse == currentRequest.from) {
currentWarehouse = warehouse;
break;
}
}
int distance = 1;
List<BatchWithDestination> matchingBatches = new ArrayList<>();
for (BatchWithDestination batch : currentWarehouse.batchIn) {
if (batch.codeOfProduct == currentRequest.codeOfProduct &&
(batch.remainedValidity-distance) >= currentRequest.requiredValidity) {
matchingBatches.add(batch);
}
}
String status = "не определён";
if (matchingBatches.isEmpty()) {
status = "полностью не реализован";
fatalRequest.add(new Request(currentRequest.pricePriority, currentRequest.to, currentRequest.from, currentRequest.quantity, currentRequest.requiredValidity, currentRequest.codeOfProduct, currentRequest.data, status));
break;}
Collections.sort(matchingBatches, new Comparator<BatchWithDestination>() {
@Override
public int compare(BatchWithDestination b1, BatchWithDestination b2) {
return Double.compare(b1.remainedValidity, b2.remainedValidity);
}
});
BatchWithDestination currentBatch = matchingBatches.get(0);
if (currentClient == null) {
System.out.println("Client not found: " + currentRequest.to);
continue;
}
if (currentWarehouse == null) {
System.out.println("Warehouse not found: " + currentRequest.from);
continue;
}
if (currentBatch == null) {
System.out.println("Batch not found in warehouse for the product: " + currentRequest.codeOfProduct);
continue;
}
double realisedQuantity = currentRequest.quantity;
if (currentRequest.quantity >= currentBatch.quantity) {
currentRequest.set_quantity(currentRequest.quantity - currentBatch.quantity);
currentClient.batchIn.add(new Batch(currentBatch.codeOfProduct, currentBatch.remainedValidity, currentBatch.quantity, currentBatch.data));
currentWarehouse.batchIn.remove(currentBatch);
matchingBatches.remove(currentBatch);
int i = 0;
while(currentRequest.quantity>0){
if(matchingBatches.isEmpty() || i >= matchingBatches.size()){
currentBatch.set_quantity(currentBatch.quantity - currentRequest.quantity);
status = "частично выполнен";
fatalRequest.add(new Request(currentRequest.pricePriority, currentRequest.to, currentRequest.from, currentRequest.quantity, currentRequest.requiredValidity, currentRequest.codeOfProduct, currentRequest.data, status));
realisedQuantity -= currentRequest.quantity;
satisfiedRequest.add(new Request(currentRequest.pricePriority, currentRequest.to, currentRequest.from, realisedQuantity, currentRequest.requiredValidity, currentRequest.codeOfProduct, currentRequest.data, status));
break;
}
currentBatch = matchingBatches.get(i);
if(currentRequest.quantity > currentBatch.quantity){
currentRequest.set_quantity(currentRequest.quantity - currentBatch.quantity);
currentClient.batchIn.add(new Batch(currentBatch.codeOfProduct, currentBatch.remainedValidity, currentBatch.quantity, currentBatch.data));
currentWarehouse.batchIn.remove(currentBatch);
i++;}
else if(currentRequest.quantity < currentBatch.quantity){
currentClient.batchIn.add(new Batch(currentBatch.codeOfProduct, currentBatch.remainedValidity, currentRequest.quantity, currentBatch.data));
currentBatch.set_quantity(currentBatch.quantity - currentRequest.quantity);
currentRequest.set_quantity(0);}
else {
currentClient.batchIn.add(new Batch(currentBatch.codeOfProduct, currentBatch.remainedValidity, currentBatch.quantity, currentBatch.data));
currentRequest.set_quantity(0);
currentWarehouse.batchIn.remove(currentBatch);}
}
}
else if(currentRequest.quantity < currentBatch.quantity && currentRequest.quantity!=0){
currentClient.batchIn.add(new Batch(currentBatch.codeOfProduct, currentBatch.remainedValidity, currentRequest.quantity, currentBatch.data));
currentBatch.set_quantity(currentBatch.quantity - currentRequest.quantity);
currentRequest.set_quantity(0);
}
if (currentRequest.quantity == 0) {
status = "выполнен";
satisfiedRequest.add(new Request(currentRequest.pricePriority, currentRequest.to, currentRequest.from, realisedQuantity, currentRequest.requiredValidity, currentRequest.codeOfProduct, currentRequest.data, status));
}
}
details details details details details details details details details details details details details details details details details details details details details details details
New contributor
Мария Сливко is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4