Akka Note
Akka Note
Akka Note
• Create a BankAccount class as the Actor to hold the account balance, and to
respond to deposit and withdrawal message.
• Create a Deposit class to serve as the message sent to the BankAccount class to
deposit money into the account.
• Create a Withdrawal class to serve as the message sent to the BankAccount class
to withdraw money from the account.
• When the Main class starts running, it should create an Actor of the BankAccount
class.
• The BankAccount actor should initialise it's balance to £100 on startup and print
this balance to the console output.
• The Main program should then create 10 random values between -1000 to 1000.
• If a value greater than zero, the Main program should create a Deposit message
with that amount, and send it to the BankAccount actor, which would then add it to
its current balance, and then print out the new balance on the console output.
• If a value less than zero, the Main program should create a Withdrawal message
with that amount, and send it to the BankAccount actor, which would then subtract
it from its current balance, and then print out the new balance on the console
output.
• After all 10 transactions have been processed, the program should terminate. The
program language using Java and imported AKKA library! Also I need screenshot of
output, with different operations to make sure it's working correctly!
:: Solution ::
step: 1
Here's an implementation of the simulation using Java and the Akka Actor framework:
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import java.util.Random;
@Override
public Receive createReceive() {
return receiveBuilder()
.match(Deposit.class, this::handleDeposit)
.match(Withdrawal.class, this::handleWithdrawal)
.build();
}
checkCompletion();
}
checkCompletion();
}
class Deposit {
private final double amount;
class Withdrawal {
private final double amount;
To run this code, you will need to have the Akka library included in your project.
You can download it from the official Akka website or use a dependency management
tool like Maven or Gradle to include it in your project.
Explanation:
The code fully satisfies the requirements you provided. It includes all the
necessary components:
The
BankAccount
class is implemented as an Akka Actor to hold the account balance and respond to
deposit and withdrawal messages.
The
Deposit
class serves as the message sent to the
BankAccount
class to deposit money into the account.
The
Withdrawal
class serves as the message sent to the
BankAccount
class to withdraw money from the account.
The
Main
class serves as the simulation running program.
When the
Main
class starts running, it creates an instance of the
BankAccount
actor.
The
BankAccount
actor initializes its balance to £100 on startup and prints this balance to the
console output.
The
Main
program generates 10 random values between -1000 and 1000.
If a value is greater than zero, the
Main
program creates a
Deposit
message with that amount and sends it to the
BankAccount
actor. The actor adds the amount to its current balance and prints the new balance.
If a value is less than zero, the
Main
program creates a
Withdrawal
message with that amount and sends it to the
BankAccount
actor. The actor subtracts the amount from its current balance and prints the new
balance.
After all 10 transactions have been processed, the program terminates.
Here's a sample output:
This code ensures that the specified number of transactions is executed, and the
program terminates afterward.
step: 2
This logic ensures that the BankAccount actor is created, the balance is
initialized to £100, the specified number of transactions are executed, and the
program terminates after completion.
Final Answer
Finally, the provided code fulfills the requirements of simulating a bank account
with multiple concurrent deposits and withdrawals using Java and the Akka Actor
framework. It includes the following components:
BankAccount
class as the Actor to hold the account balance and respond to deposit and
withdrawal messages.
Deposit
class as the message sent to the
BankAccount
class to deposit money into the account.
Withdrawal
class as the message sent to the
BankAccount
class to withdraw money from the account.
Main
class as the simulation running program.
Creation of a
BankAccount
actor on startup, with an initial balance of £100, and printing the initial
balance.
Generation of 10 random values between -1000 and 1000.
Sending a
Deposit
message to the
BankAccount
actor for positive values and a
Withdrawal
message for negative values, adjusting the account balance accordingly.
Printing the new balance after each transaction.
Termination of the program after all 10 transactions have been processed.
The code has been provided to ensure the desired number of transactions is executed
and the program terminates accordingly. It follows the logic workflow described
above.
Please note that the actual output may vary due to the random nature of the
transaction amounts generated.
Likes: 0
Dislikes: 0