This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Player(val name: String, var score: Int, | |
var blackGames: Int, var whiteGames: Int, var bye: Boolean, var forfeitWin: Boolean) | |
fun swissPairing(players: List<Player>, rounds: Int) { | |
// Sort players by score | |
players.sortBy { it.score } | |
for (round in 1..rounds) { | |
// Pair players with the same score | |
val pairs = players.filter { !it.bye && !it.forfeitWin }.groupBy { it.score }.values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val resultArray = IntArray(100) { | |
0 | |
} | |
for (percent in 1..100) { | |
for (x in 1..1000000) { | |
if (simulateFindingBestSecretary(percent)) { | |
resultArray[percent - 1]++ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PaymentService { | |
EventBus eventBus = EventBusFactory.getEventBus(); | |
public void onPaymentSuccessful (Payment payment) { | |
PaymentEvent paymentEvent = new PaymentEvent(); | |
paymentEvent.setPayment(payment); | |
//post the payment event, after this point, | |
//all methods that subscribed to payment event are executed Asynchronously | |
eventBus.post(paymentEvent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.eventbus.Subscribe; | |
public class RecieptSender { | |
@Subscribe | |
public void sendRecieptToCustomer(PaymentSuccessfulEvent paymentSuccessfulEvent){ | |
// Simulate sending reciept | |
System.out.println("Reciept sent to Customer"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PaymentEvent { | |
private String name = "PaymentSuccesful"; | |
public String getName() { | |
return name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PaymentSuccessfulEvent { | |
private String name = "PaymentSuccesful"; | |
private Payment payment; | |
public PaymentSuccessfulEvent (Payment payment) { | |
this.payment = payment; | |
} | |
public String getName() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.eventbus.AsyncEventBus; | |
import com.google.common.eventbus.EventBus; | |
import java.util.concurrent.Executors; | |
public class EventBusFactory { | |
//hold the instance of the event bus here | |
private static EventBus eventBus = new AsyncEventBus(Executors.newCachedThreadPool()); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void onPaymentSuccessful(Payment payment) { | |
sendRecieptToSeller (Payment payment); | |
sendRecieptToBuyer (Payment payment); | |
} | |
private void sendRecieptToSeller (Payment payment) { | |
System.out.print("Reciept sent to seller"); | |
} | |
private void sendRecieptToBuyer (Payment payment) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
This class has been simplified for this article | |
**/ | |
public enum TokenManager implements Constants { | |
INSTANCE; | |
private ConcurrentHashMap<String, LoggedInUser> tokenUserMap = new ConcurrentHashMap<>(); | |
public void addToken(String token, User user) { | |
tokenUserMap.put(token, new LoggedInUser(user)); | |
} | |
public void updateLastAccessedTime(String token) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"event":"charge.success", | |
"data":{ | |
"id":302961, | |
"domain":"live", | |
"status":"success", | |
"reference":"qTPrJoy9Bx", | |
"amount":10000, | |
"message":null, | |
"gateway_response":"Approved by Financial Institution", |