nus-cs2030 / 2021-s1

28 stars 48 forks source link

project 2 toString() method #308

Open chongweil907 opened 3 years ago

chongweil907 commented 3 years ago

Hi all, Does anyone has any idea on how to get the string representation of the event? Since everything is wrapped in a lambda, I am stuck on how to get the String representation out. I am assuming that we are not system.out.println it.

Thanks is advance !

georgepwhuang commented 3 years ago

You can always call from the super class Event's getters or toString().

chongweil907 commented 3 years ago

@georgepwhuang There is only one attribute for my Event class which is the private final Function<Shop, Pair<Shop, Event>> func;

georgepwhuang commented 3 years ago

I thought that was for the subclasses of Event though? I submitted with multiple methods onto CodeCrunch and passed fine

chongweil907 commented 3 years ago

@georgepwhuang yea how do i do it for subclass of Event? I wrote the function in my super constructor and was wondering how can i get the toString method(). Thanks

georgepwhuang commented 3 years ago

Um I stored the necessary parameters I needed for the string in the toString() method as attributes of the Event class, and some as the attributes of the subclass, so I didn't have to call from the lambda function.

georgepwhuang commented 3 years ago

So essentially I still kept the attributes in the Event class from Project 1, but I added the function as an extra attribute.

ghost commented 3 years ago

@georgepwhuang By adding the function as an extra attribute, does your event class have a function attribute too? Because I thought the question states that your event class must have a method called execute, which is this.func.apply(shop). If you do have the func attribute, is it private final or public? Thank you in advance!

georgepwhuang commented 3 years ago

Yeah my function attribute is private final and I just assign a lambda function to the function attribute through the constructors of the subclasses using super().

ghost commented 3 years ago

So your super takes in (customer, shop, lambda)?

chongweil907 commented 3 years ago

how can i get the information of the server that is allocated to that customer since it is wrapped inside a lambda

georgepwhuang commented 3 years ago

I save the server ID in Project 1 also lol

joelwongjy commented 3 years ago

Your lambda in ArriveEvent should return either a Serve Event, Wait Event or a Leave Event. In the lambda function, you could use the find function from Shop to find an available server and then map it to the respective Event, while passing in the server ID to the new event. orElseGet will help here too.

ghost commented 3 years ago

I see. I guess my question is where to define the lambda function when instantiating each event. For example, if the lambda is inside super(), then doesn't that mean ArriveEvent needs to provide the lambda for ServeEvent, and ServeEvent needs to provide the lambda for DoneEvent? Sorry a bit confused haha

SkyBlaise99 commented 3 years ago

I had the toString method just to pass level 2. I used sop for the execute() part

joelwongjy commented 3 years ago

All of Arrive, Serve, Wait, Leave and Done extend Event. ArriveEvent and each of the other events are not related so you can just declare in the lambdas in each Event class's super(). E.g. Serve event's lambda will always return a Done event.

SkyBlaise99 commented 3 years ago

^ My ServeEvent can potentially return a ServeEvent lol, it depends on your implementation