Results 1 to 11 of 11

Thread: How to

  1. #1
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default How to

    I am kind of confused about creating new objects in while/for-loops. In my main(), I have a while-loop that sometimes triggers the creation of a new object from a class Quotation. I want each quotation to have its own ID, and also its own Reference variable (reference variable and ID could be the same I think).

    How do I make sure that each object is referred to with another reference variable?
    Piece of code in which I want to use it:
    Java Code:
        	    ...
        	    ...
        	    else if (listOfEvents.get(0).eventType=="quotationSubmitted"){
        	    	//If the event that occurs now is a submission of a quotation, then create a new quotation
        	    	//Give this quotation a unique number
        	    	Quotation someReference = new Quotation;
        	    	someReference.quotationId = 
        	    	listOfEvents.remove(0);
        	    	}
        	    ...
        	    ...
    I would like to know whether it is possible to assign a unique ID to each newly made quotation object.
    If not, if I assign IDs on quotations before they are submitted (my model will work on historical data, so I am basically able to do that), how do I make sure that the right ID is coupled to the right quotation?

    edit: I wanted to change the title. Not possible though
    Last edited by casta; 05-09-2016 at 03:56 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to

    First, your code snippet does not seem to relate to your question (no while loop). Second, you have other problems. Line 6 is syntactically incorrect as
    you need trailing () at the end of Quotation. In line 3 you are using == to compare strings. You should use equals().

    Please provide a Short, Self Contained, Correct Example that demonstrates the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: How to

    Thanks for your reply. I see that I was not complete in my problem description. Second try here.

    The problem is as follows:
    I have a while loop that loops through an ArrayList of events. The class Event is a superclass for several types of events. In my example, you could think about the submission of a quotation, the confirmation of an order, etc. Each event has a certain type (a string telling what type it is), start time, priority, etc.

    What I would like the while-loop to do is: To run through the whole set of events that are currently there, plus the events that are created by previous events. Let me clarify what I mean here:
    "A quotation is made for a specific customer. Then, when both customer and supplier agree on the assignment,product, etc., this same quotation is transformed to a real order. In my example, after a submission of quotation, there must be a trigger to create a new event, which would be named 'orderConfirmed' or something like that. This eventually will lead to new events, e.g. startProductionEvent, endProductionEvent, etc. I want all these events that are linked to one specific order/customer to have the same ID."

    I could give this ID manually to each quotation that is submitted. In that case I wonder how I would be able to transfer this ID to all other events
    Maybe, I would also be able to give each order a unique ID without manually doing it before running my program.

    Either way, I would like to know how I am able to give each set of events (quotation submitted, order confirmed, order in production, etc) the same ID. Then, I would like to know how I can refer to this specific order.


    Java Code:
        	while(!listOfEvents.isEmpty()) {
        		sortListOfEvents();
        	    if (listOfEvents.get(0).eventType=="Type A") {
        	    	
        	    	//Do something
        	    } //end if
        	    
        	    else if (listOfEvents.get(0).eventType=="Type B") {
            	//do something else  	    	
        	    }
        	     	    
        	    else if (listOfEvents.get(0).eventType=="quotationSubmit"){
        	    	QuotationSubmitEvent rn = (QuoationSubmitEvent) listOfEvents.get(0);
        	    	listOfEvents.add(rn.createOrderConfirmedEvent(rn.eventTime));//Assuming that each quotation turns into a order confirmation
        	    	// I would like to give the order a certain ID here!
        	    	listOfEvents.remove(0);
        	    	}
        	       	      	    
        	    else if (listOfEvents.get(0).eventType=="orderConfirmed") {
        	    	OrderConfirmedEvent ro = (OrderConfirmedEvent) listOfEvents.get(0);
        	    	listOfEvents.add(ro.createOrderStartManufacturing(ro.eventTime));
        	    	listOfEvents.remove(0);
        	    }
      	    
        	    
        	    
        	}//end while

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to

    Well, based on my limited understanding of what you want to do, you could use a Map. The key could be the ID and the value could be
    a list of related events. Whatever it is that triggers the successive events will probably need to send the appropriate ID.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: How to

    That indeed seems useful. Thanks. I do however have some difficulty trying to import a HashMap into my current Order creation method. I make the program create orders based on a text-file. Each line is an order. The invocation of this method happens in a different class.

    [code]



    How can I make the OrderID become the HashKey for my HashMap?

    Java Code:
    //import java.time.Duration;
    //import java.time.LocalTime;
    import java.util.ArrayList;
    import java.util.HashMap;
    
    
    public class Order {
    	HashMap <Integer, ArrayList<Order>> map = new HashMap <Integer, ArrayList<Order>>();
    	int orderID;
    	String orderStatus;
    	...
    	...
    	...
    	...
    	//constructor below
    	public Order(String line) {
    
    		String[] arrayLine = line.split("\\s+");
    		this.orderID = Integer.parseInt(arrayLine[0]);
    		this.orderTime = Integer.parseInt(arrayLine[3]);
    		this.orderStatus = "Confirmed";
    	}
    I hoped I could easily put the 'put'-method of the Map in the constructor, but that does not work, since the arguments are Integer, Arraylist<Order>
    How am I able to make this Mapping work?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to

    Looking at that it seems to imply that each order consists of a Map of Orders.
    That doesn't sound correct to me.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: How to

    I get your point, although I am not sure how to make a HashMap in my example. My thoughts: I think that it indeed does not make sense to make the HashMap in my constructor (as I did in my previous code). I did this in an attempt to connect all information of each order with the orderID (the key). I do not know how to do this.
    If I want to map each order, do I need a loop? What should be the V in "Class HashMap<K,V>" to connect all information with the key?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to

    Can you explain your model?

    You have an Order. It has an id.
    So how does this fit in with everything else?

    Why do you think an Order should have a Map<List<Order>> in it?
    What is that modelling?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: How to

    I think I make a mistake in my thinking, and that it is actually easier than I make it to be. The class order has several instance variables, of which one is the ID of the order. I want methods to perform actions on each order. The methods and sequence of methods may differ based on the orders characteristics. For instance, some orders are marked Emergency-orders, that need to be dispatched earlier. This data is stored in the Order-class.

    Hence, methods will need to know what ID the order they process has, and what characteristics it has. Each order is required to have its own unique reference-code. For that reason, I wanted to use a HashMap. My perception on the initialization of HashMaps is, that the K in Class HashMap<K,V> is the key. This would be the orderID. The V would then be all other variable values for this specific order.

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: How to

    Remember that the HashMap does not retain order. So the order in which you put the items will most likely NOT be the order you get when you iterate over them. If order is important, don't use a HashMap, but a LinkedHashMap.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to

    That still doesn't explain that code above, and the existence of that Map in there...hey ho.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •