Figure 2 Algorithm flowchart of Menu Source code: Performed Student: Pham Phu Loc... View Latest Message"; Performed Student: Pham Phu Loc 4... Education Send Message Algorithm Step
IMPLEMENT COMPLEX DATA STRUCTURES AND ALGORITHMS (LO3)
Implement a complex ADT and algorithm in an executable programming language to solve
A queue is a line of items waiting to be handled, starting at the front of the line and processing it sequentially
In essence, a message is a byte array with some headers at the topff it is the data that is carried between the sender and the receiving program
A message queue employs an asynchronous communication protocol that allows messages to be added to a designated queue without the need for immediate replies or synchronization between sender and receiver In this model, the sender can continue processing other tasks without waiting for a response Email serves as a notable illustration of a message queue, where messages are enqueued and delivered to recipients without demanding an instant reply or acknowledgment.
Figure 1 Message Queue 1.2 Scenario overview
The conveyance and processing of messages between layers is one aspect of the provider interface for the middleware that is currently under development For transport, a buffer of queued messages is deployed and processed, the system requires a stack of messages
It is necessary to develop these types of collections for the system It is recommended to design an ADT/algorithm for these 2 structures and implement a demo version with the message as a string of up to 2fi0 characters Handle the problems and errors encountered in the demo carefully with exceptions and some tests should be done to prove the correctness of the algorithms/operations
From the requests that have been made, | have selected two suitable ADT types to deal with, namely Stacks and Queues
- The stack will be used to store process messages
- Queues are used to store the messages being transported
Performed Student: Pham Phu Loc 2
‘BTEC s nine win IG ean ù ‘BT EC
The main menu offers four primary functions: input message, send message, view message, and exit Each function corresponds to a number from 1 to 4 To execute a specific action, users select the function's corresponding number and enter it to initiate the desired process.
Figure 2 Algorithm flowchart of Menu Source code:
Performed Student: Pham Phu Loc
9 static Queue messageStore = new LinkedList();
10 static Stack messageConsumer = new Stack();
11 128 public static void main(String args[]) { int choice = 0;
14 Scanner sc = new Scanner(System.in);
17 System.out.println("Message System!");
18 System.out.println("1 Input Message");
19 System.out.println("2 Send Message");
20 System.out.println("3 View Latest Message");
26 System.out.print("Choose a number from 1 to 4: ");
27 choice = Integer.parseInt(sc.nextLine());
30 System.out.println("Please input number format!");
36 System.out.println("Please input number from 1 to 4!");
Figure 3 Source code of Menu Input Message
Step 1: Input a message below 2fi0 characters Step 2: Store a message to queue
Performed Student: Pham Phu Loc 4 eee SB Step 3: Break ‘BTEC
Display the message: Display the message
“Input Message Failed! -—yes> oe Gaerne aS
Messages can only contain characters ge SE Bo Kr"
Figure 4 Algorithm flowchart of Input Message Source code: a) SOSIBDARANHPSHCHUARHEWN
//Input Message e public static void addMessage() {
Scanner sc = new Scanner(System.in);
System.out.print("Input Message: "); ms = sc.nextLine(); try { if (ms.isEmpty()) { throw new Exception("Message cannot be blank!");
} else { try { if (ms.length() > 250) { throw new Exception("Input Message Failed! Messages can only contain 25@ characters"); else { } messageStore.add(ms) ; System.out.println("messageStore: nan Dan
} catch (Exception e) { System.out.printIn(e.getMessage());
Figure 5 Source code of Input Message
Performed Student: Pham Phu Loc 5
Step 1: Check if there are already messages in the queue
Step 2: If yes, retrieve the message in the first line of the queue
Step 3: Push messages to consumers
Please input messageStorel" if there are no message a in the queue yet
Vv Send the first message in the queue to the consumer
Figure 6 Algorithm flowchart of Send Message Source code:
Performed Student: Pham Phu Loc 6
“BTEC _ ơ aaa bon ù ‘BT E Cc
92 System.out.println("MessageStore is empty! Please input messageStore!");
97 System.out.println("messageStore: " +messageStore);
98 System.out.println("messageConsumer: " +messageConsumer) ;
Figure 7 Source code of Send Message View Latest Message
Step 1: Check if there are any messages in the stack
Step 2: If yes, retrieve the message on top of the stack
Input Nessage_