<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Daiiszuki</title>
    <description>The latest articles on DEV Community by Daiiszuki (@daiiszuki).</description>
    <link>https://dev.to/daiiszuki</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F880200%2F2d3d6a94-c247-44c0-a445-2069ec74090f.png</url>
      <title>DEV Community: Daiiszuki</title>
      <link>https://dev.to/daiiszuki</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/daiiszuki"/>
    <language>en</language>
    <item>
      <title>How can I make the following parking-lot simulation asynchronous using RabbitMQ?</title>
      <dc:creator>Daiiszuki</dc:creator>
      <pubDate>Tue, 21 Jun 2022 04:13:37 +0000</pubDate>
      <link>https://dev.to/daiiszuki/how-can-i-make-the-following-parking-lot-simulation-asynchronous-using-rabbitmq-56ji</link>
      <guid>https://dev.to/daiiszuki/how-can-i-make-the-following-parking-lot-simulation-asynchronous-using-rabbitmq-56ji</guid>
      <description>&lt;p&gt;0&lt;/p&gt;

&lt;p&gt;I'm trying to learn and undertand the different approaches to concurrency in java. I have with me a serialised implementation of a parking lot simulation. I want to make it so that the program uses Rabbitmq instead, how would I go about doing that?&lt;/p&gt;

&lt;p&gt;Below is my parking-lot class, which, implementation wise could also be improved.&lt;/p&gt;

&lt;p&gt;EDIT: I've realised that my main question is actually, how can I do the Pub/sub in a loop.2. Is pub/sub even necessary?&lt;/p&gt;

&lt;p&gt;`&lt;br&gt;
public class Parking {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args) throws InterruptedException {

    Parking parkingObj = new Parking();

    parkingObj.runSimulation();

}

public void runSimulation() throws InterruptedException{
    int numOfRuns = 101;//100 runs
    int currentRuns = 1;

    securityGuard myGuard = new securityGuard();

    //spot mySpot = new spot();

    ArrayList&amp;lt;ticketClass&amp;gt; ticketArray = new ArrayList&amp;lt;&amp;gt;();

    int bouncedCustomers = 1;
    spot availableSpot = new spot();


    //Random randomExit  = new Random();

    while (currentRuns &amp;lt; numOfRuns){
        Random randomSleep  = new Random();

        //Car object instantiation  
        carClass vehicle = new carClass();
        //ticketClass ticketObj = new ticketClass();

        //Random time generator 

        Random randomTime = new Random();


        //instantiation of the info geneatator class

        infoGenerator info = new infoGenerator();


        //Generaring random Car info 

        String plateNumber = info.plateGenerator();
        String carModel = info.modelGenerator();
        String color = info.colorGenerator();

        if (availableSpot.getSpotNum() == 15 ){
                   System.out.println("Carpark full, No cars allowed unitl a space is free");

                   //Customers waiting for free space
                   Thread.sleep(9000);

                   System.out.println("Total Waiting customers: " + bouncedCustomers);

                   bouncedCustomers += 1;

        }
        else{

            //System.out.println("Customer Exiting");
            Thread.sleep(randomTime.nextInt(5000));

            meterClass myMeter = new meterClass();

            ticketClass myTicket = myGuard.ticketGenerator(vehicle, myMeter);
            //ticketClass myTicket = new ticketClass();
            myTicket.setmeter(myMeter);
            myTicket.setCar(vehicle);
            myTicket.getCar().plateSetter(plateNumber);
            myTicket.getCar().colorSetter(color);
            myTicket.getCar().modelSeter(carModel);
            myTicket.getCar().minSetter(randomTime.nextInt(100));

            //Details are only set if there is space available
            //The assumption is that users cannot stay longer than 2 days. The reality-to-simulation time ratio is 1 unit:10min
            myMeter.setPurchasedMinutes(randomTime.nextInt(72));


            System.out.println("\n\nCar " + currentRuns + " has entered the car park");
            System.out.println("\nCAR DETAILS:");
            System.out.println(carModel);
            System.out.println(plateNumber);
            System.out.println(color);


            int spotAvail = availableSpot.assignSpot();

            myTicket.setSlotNum(spotAvail);


            //Set the time the car entered 
            String timeIn = info.timeMonitor();

            //myTicket.


            ticketArray.add(myTicket);
            System.out.println("\n\n===Total customers: " + ticketArray.size());


            System.out.println(timeIn+"\n");

            availableSpot.spotLog();


        }
        //Cars leaving at random times

        for (int i= 0; i &amp;lt; ticketArray.size();i++ ){

                meterClass meterOut =  ticketArray.get(i).getMeter();
                carClass ExitCar = ticketArray.get(i).getCar();


                if(myGuard.checkParking(ExitCar,meterOut)){
                    System.out.println("\nCustomer " + ExitCar.plateGetter()+ " is exiting the carpark...");
                    double penaltyVal = ticketArray.get(i).getPenalty();
                    System.out.println("FINE: " + penaltyVal);
                    System.out.println("==================================================================");
                    Thread.sleep(randomTime.nextInt(4000));
                    ticketArray.remove(ticketArray.remove(i));
                    availableSpot.spotFree(i);

                }




        }




    currentRuns += 1;

    }





}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
`&lt;br&gt;
TLDR: I need to optimise the following code, both structure-wise and in terms of speed (Specifically using multithreading with RabbitMQ)&lt;/p&gt;

&lt;p&gt;As it currently is, it runs in an infinite loop, and the fine value is 0. The security guard class which is responsible for this calculation is as such;&lt;/p&gt;

&lt;p&gt;public class securityGuard{&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public String badgeNumber;
    public String guardName;

    securityGuard(){}

    securityGuard(String badgeNumber, String guardName){

        this.badgeNumber = badgeNumber;
        this.guardName = guardName;           
    }

    public void setBadgeNumber(String badgeNumber){
        this.badgeNumber = badgeNumber;
    }

    public String getBadgeNumber(){

        return badgeNumber;

    }

    public void setguardName(String guardName){
        this.guardName = guardName;
    }

    public String getGuardName(){

        return guardName;

    }

    public boolean checkParking(carClass car,meterClass meter){

        return car.minGetter() &amp;gt; meter.getPurchasedMinutes();
    }  

    public ticketClass ticketGenerator(carClass car, meterClass meterVar){

        ticketClass myTicket = new ticketClass(car,this);

        int timeRemaining = car.minGetter() - meterVar.getPurchasedMinutes();

        if(checkParking(car,meterVar)){
            if (timeRemaining &amp;lt; 60){
                myTicket.penalty = 50;
            }

            else {
                myTicket.penalty = 50 + (10 * (timeRemaining/60));
            }





    }
return myTicket;


}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Please let me know if you require any additional information regarding the other classes or if I left anything out .Thank you in advance&lt;/p&gt;

&lt;p&gt;EDIT:&lt;/p&gt;

&lt;p&gt;Below is my attempt at a producer implementation. I tried using a for loop in the run method with the goal of publishing 100 random messages&lt;/p&gt;

&lt;p&gt;public class entrySimulation implements Runnable {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int numOfRuns = 100;//1000 runs
int currentRuns = 1;

securityGuard_exp_1 myGuard = new securityGuard_exp_1();


//System.out.println("Customer Exiting");

//Thread.sleep(randomTime.nextInt(5000));

meterClass_exp_1 myMeter = new meterClass_exp_1();
//Details are only set if there is space available
//The assumption is that users cannot stay longer than 2 days. The reality-to-simulation time ratio is 1 unit:10min


//instantiation of the info generator class

infoGenerator_exp_1 info = new infoGenerator_exp_1();

//Generating random Car info


//spot_exp_1 mySpot = new spot_exp_1();
//Use an iterator
 List&amp;lt;ticketClass_exp_1&amp;gt; ticketArray = new ArrayList&amp;lt;&amp;gt;();

Iterator&amp;lt;ticketClass_exp_1&amp;gt; iter = ticketArray.iterator();

public final spot_exp_1 availableSpot = new spot_exp_1();

//Random time generator
Random randomTime = new Random();


public static void main(String[] args) {

    String exchangeName = "entryExchange";
    String routingKey = "exitKey";
    String message = "";

    //Creating a connection factory

    ConnectionFactory factory = new ConnectionFactory();

    //Creating new connection

    try(Connection conVar = factory.newConnection();){

        Channel channelCon = conVar.createChannel();

        //Exchange declaration

        channelCon.exchangeDeclare(exchangeName,"customerExit");



        channelCon.basicPublish(exchangeName,routingKey,null,message.getBytes(StandardCharsets.UTF_8));

        System.out.println("Customer Exited");





    }catch(Exception e){}


}




@Override
public void run() {

    for (int i = 0; i &amp;lt; numOfRuns; i++) {
        //System.out.println(i);


        String plateNumber = info.plateGenerator();
        String carModel = info.modelGenerator();
        String color = info.colorGenerator();

        myMeter.setPurchasedMinutes(randomTime.nextInt(30));


        carClass_exp1_1 vehicle = new carClass_exp1_1(carModel, color, plateNumber, randomTime.nextInt(2880));
        ticketClass_exp_1 myTicket = myGuard.ticketGenerator(vehicle, myMeter);



        //Generating details
        myTicket.setmeter(myMeter);
        myTicket.setCar(vehicle);
        myTicket.getCar().plateSetter(plateNumber);
        myTicket.getCar().colorSetter(color);
        myTicket.getCar().modelSeter(carModel);
        myTicket.getGuard().setguardName("Phill");
        myTicket.getGuard().setBadgeNumber("AF170");
        int spotAvail = availableSpot.assignSpot();
        myTicket.setSlotNum(spotAvail);


        //Set the time the car entered
        String timeIn = info.timeMonitor();


        //message
        System.out.println("\n\nCar " + currentRuns + " has entered the car park");
        System.out.println("\nCAR DETAILS:");
        System.out.println(carModel);
        System.out.println(plateNumber);
        System.out.println(color);
        System.out.println("Penalty" + myTicket.getPenalty());
        ticketArray.add(myTicket);
        System.out.println("============================================|");
        System.out.println("TIME IN: " + timeIn);
        //System.out.println("\n\n===Total customers: " + myTicket.slotNum);



        //message
        availableSpot.spotLog();





    }


}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;However, I'm not sure how to. Please if anyone can help in any way. Sorry, this is a very new concept to me&lt;/p&gt;

</description>
      <category>rabbitmq</category>
      <category>amqp</category>
      <category>java</category>
      <category>mqp</category>
    </item>
  </channel>
</rss>
