public PharmacistController(boolean option) { if (option) { Pharmacist c1 = new Pharmacist(1, Trung, 19, 0230423442); Pharmacist c2 = new Pharmacist(2, Khanh, 19, 0346346634); Pharmacist c3 = new Pharmacist(3, Cong, 19, 0935457455); listPhar.add(c1); listPhar.add(c2); listPhar.add(c3); while (true) { System.out.println(++); System.out.println(| Pharmacist MANAGEMENT SYSTEM |); System.out.println(++); System.out.println(|1. Insert new Pharmacist |); System.out.println(|2. View list of Pharmacist |); System.out.println(|3. Delete |); System.out.println(++ ); System.out.print(Your choice: ); int choice = sc.nextInt(); sc.nextLine(); System.out.println(++); switch (choice) { case 1:
Trang 1ASSIGNMENT 1
Unit number and title Unit 19: Data Structures and Algorithms
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism I understand thatmaking a false declaration is a form of malpractice
Student’s signatureGrading grid
Trang 2 Summative Feedback: Resubmission Feedback:
Internal Verifier’s Comments:
IV Signature:
Trang 3Table of Figures
Figure 1: ADTs 5
Figure 2: Example for stack 6
Figure 3: push onto stack 7
Figure 4: push method in stack 7
Figure 5: test push method 7
Figure 6: Stack after push 7
Figure 7: pop() of stack 8
Figure 8: after pop 9
Figure 9: peek() of stack 10
Figure 10: method peek in class stack 10
Figure 11: test peek method 10
Figure 12: After peek 10
Figure 13: example for application of stack in memory 11
Figure 14: : example for application of stack in memory(1) 12
Figure 15: : example for application of stack in memory(2) 12
Figure 16: Pharmacist menu using switch case 14
Figure 17: Pharmacist Controller methods 15
Figure 18: Pharmacist system Create method 16
Figure 19: Pharmacist system Read method 17
Figure 20: Pharmacist system Delete method 18
Trang 4Table of Contents
Table of Contents 3
Table of Figures 4
1 Data structures 5
1.1 Abstract data type (P1) 5
1.1.1 Definition: 5
1.1.2 Examples 6
Overview stack operations 6
a push(E element): 7
c peek() : 10
1.2 ADT usages 11
1.2.1 Application of Stack in memory (P2) 11
Definition: 11
1.2.2 Application of an ADT (P3) 13
Scenario: 13
Requirement: 13
The class Pharmacist Controller 14
Running program 16
References 19
Java ArrayList (2023) 19
Available at: https://www.w3schools.com/java/java_arraylist.asp (Accessed: 14 July 2023) 19
ArrayList in Java - GeeksforGeeks (2016) 19
Available at: https://www.geeksforgeeks.org/arraylist-in-java/ (Accessed: 14 July 2023) 19
Index of comments 20
Trang 51 Data structures
1.1
Abstract data type (P1)
1.1.1 Definition:
A mathematical representation of data types is called an abstract data type (ADT) Its behavior
(semantics), as seen from the perspective of a user, is determined by the data's potential values, applicable operations, and the behavior of those operations
ADTs are used to abstract away the implementation details of data structures and algorithms This makes
it easier to use and understand these data structures and algorithms, and it also makes them more reusable
Figure 1: ADTs
There are many different ways to represent ADTs Some common ways include:
- Interfaces: An interface is a contract that lists the functions that can be performed on an ADT This is themost typical manner in which object-oriented programming languages describe ADTs
- Enumerations: An enumeration is a list of named constants This can be used to represent the possible values of an ADT
- Data structures: Data structures are a method of data organization This can be used to illustrate how an ADT is implemented
Here are some examples of ADTs:
Stack: A stack is a data structure that stores data in a Last In, First Out (LIFO) order
Queue: A queue is a data structure that stores data in a First In, First Out (FIFO) order
Linked list: A linked list is a data structure that stores data in a linked list
Trang 61.1.2 Examples
The notion of Linear structure will be introduced before talking about Stack A collection of data has a linear structure in which the items are ordered in a specific sequence, it has two ends, bottom and top or left and right, and it can only be accessed sequentially Elements are typically linked sequentially and require linear memory space Stack and Queue are two types of linear data structures
Stack is an ADT with top-down data structure, things like adding elements and removing elements are done on top of the Stack structure It is a linear data structure because the data set is ordered in ascending order, the first in first, the last in second, can only access the elements in sequence (it cannot be go directlyfrom bottom to top) Stack's sorting principle is called LIFO (last in, first out) the element added to the end will be accessed first, then the element below it
The example for stack is stacks of thing below if you want to remove the bottom thing, you have to take one by one from the top to the one you want Otherwise if you want to add a new one you must put it on the top
Figure 2: Example for stack
Overview stack operations
- push (Element item): Add an item of data type Element to the top of the stack, return true
(successful), false (failed)
- pop (): Remove an item from the top of the stack and return its value
- peek (): Get the value on the top of the stack
- empty (): Checks if the stack has an item, returns true if true, returns false if false
Trang 7a push(E element):
How it works: The function will accept an item of data type Element as input and then verify if the stack isfull Finally, conduct a push operation, which will add the value to the top of the stack and push the preceding element down, resulting in the new element being at the top of the stack Failure (full stack limit
or full memory, for example) returns false
Figure 3: push onto stack
After push 6 onto the stack, 6 become TOP instead of 1, return true
Trang 8b pop():
How it works: The pop() function checks whether the stack is empty or not, if empty, returns null, ifthere is an element, deletes that element from the stack, the top element after deletion will be the elementbelow Returns the deleted value
Figure 7: pop() of stack
Do pop() with stack S, the top element (5) will be removed, the return value will be 5 The purpose ofreturning the deleted value is so that the user knows what the deleted value is or uses it for somethingelse
Trang 9Source code:
Code test:
Figure 8: after pop
Trang 10c peek() :
How it works: The peek() function checks whether the stack is empty or not, if empty, returns null, if not, returns the top value of the stack This function does not change anything of the stack, it only has the purpose of returning the top value of the stack to the user
Figure 9: peek() of stack
Do peek(), the top element (6) will be return , nothing was changed in stack S
Trang 11Figure 13: example for application of stack in memory
Trang 12Applications in storing memory cells for recursive functions, for example, calculating the results of the recursive factorial-function below, the variable result will be stored on the stack, the factorial-function(4) will also be stored on the stack to wait for the results, after calculate Each recursive call will have an extramemory cell to store the factorial-function, when factorial (1) will have a return value of 1, then calculate the value of factorial (2), factorial (3), factorial (4) and free the stack memory Finally return the value for the variable result at Main.
Figure 14: : example for application of stack in memory(1)
Figure 15: : example for application of stack in memory(2)
Trang 131.2.2 Application of an ADT (P3)
Scenario:
The customer X is the owner of a drug store and they are looking for a software to manage pharmacists They need software that can track the pharmacist's basic information such as name, age, and phone number
Requirement:
- Adding a pharmacist
- Editing a pharmacist's information
- Deleting a pharmacist's information
- Search for a pharmacist
- Show all pharmacist
For this scenario I will use Java language with ArrayList to do it
Before making this program, I need to write an Array List ADT so that I can use it in my program In this program, I only used two main function is add and delete objects, so I write two methods to do that function
ArrayList Source code:
Figure 16: ArrayList source code(1)
Trang 14Figure 17: ArrayList source code(2)
Figure 18: ArrayList source code(3)
Figure 19: ArrayList source code(4)
Trang 15- To make this program, I decided to use ADT which is ArrayList, because Array List is a dynamic array that can grow or shrink as needed That will make this program is easy for managing for users.
- My program will have two class to manage pharmacist One is Pharmacist and another is
PharmacistController
The class Pharmacist
Here is the Pharmacist code with field, constructor, getter setter and method Display()
Figure 20: Class Pharmacist
Trang 16The class Pharmacist Controller
Before using ArrayList, we must import the ArrayList library we already write above:
Figure 21: Import ArrayList
Here is the PharmacistController code with method AddNew(), ViewAll(), Delete()
⎯ Insert new Pharmacist (Add a new information of a Pharmacist to system)
⎯ View list of Pharmacist (View all information of Pharmacistin system)
⎯ Delete Pharmacist (Delete delete a specific Pharmacist with the Id passed in via console of a Pharmacistto system)
Figure 22: Pharmacist menu using switch case
Trang 17Figure 23: Pharmacist Controller methods
Trang 18Running program
Firstly, Let’s see how the Pharmacist system look then I will add something new into the system
Figure 24: Pharmacist system Create method
Trang 19Secondly, check that have the new pharmacist I just add above is appear in system yet?
Figure 25: Pharmacist system Read method
Trang 20Thirdly, Delete method
Figure 26: Pharmacist system Delete method
Trang 21References
Abstract data type - Wikipedia (2015)
Available at: https://en.wikipedia.org/wiki/Abstract_data_type (Accessed: 12 July 2023).
Data Structure and Algorithms - Stack (2023)
Available at: https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm (Accessed:
12 July 2023)
(2023) Storm.cis.fordham.edu
Available at: https://storm.cis.fordham.edu/zhang/cs2200/slides/stack.pdf (Accessed: 13 July 2023)
Stack-based memory allocation - Wikipedia (2022)
Available at: https://en.wikipedia.org/wiki/Stack-based_memory_allocation (Accessed: 12 July 2023)
Java ArrayList (2023)
Available at: https://www.w3schools.com/java/java_arraylist.asp (Accessed: 14 July 2023)
ArrayList in Java - GeeksforGeeks (2016)
Available at: https://www.geeksforgeeks.org/arraylist-in-java/ (Accessed: 14 July 2023)
Trang 22Class Pharmacist:
public class Pharmacist {
private int _id ;
private String _name ;
private int _age ;
private String _phone ;
public Pharmacist () {}
public int get_id () {
return _id ;
}
public void set_id ( int _id) {
this _id = _id;
}
public String get_phone () {
return _phone ;
}
public void set_phone ( String _phone) {
this _phone = _phone;
}
public int get_age () {
return _age ;
}
public void set_age ( int _age) {
this _age = _age;
}
public String get_name () {
return _name ;
}
public void set_name ( String _name) {
this _name = _name;
System out.println( "Pharmacist ID:" + _id );
System out.println( "Name: " + _name );
System out.println( "Age: " + _age );
System out.println( "Phone: " + _phone );
}
}
Trang 23public class PharmacistController {
static ArrayList < Pharmacist > listPhar = new ArrayList< Pharmacist >();
Scanner sc = new Scanner( System in);
public PharmacistController ( boolean option) {
if (option) {
Pharmacist c1 = new Pharmacist( 1 , "Trung" , 19 , "0230423442" );
Pharmacist c2 = new Pharmacist( 2 , "Khanh" , 19 , "0346346634" );
Pharmacist c3 = new Pharmacist( 3 , "Cong" , 19 , "0935457455" );
System out.println( "+ -+" );
System out.println( "|1 Insert new Pharmacist |" );
System out.println( "|2 View list of Pharmacist |" );
System out.println( "|3 Delete |" );
System out.println( "+ -+ " );
System out.print( "Your choice: " );
int choice = sc nextInt();
public Pharmacist GetPhar ( int id) {
for ( Pharmacist d : listPhar) {
if ( get_id() == id)
return d
Trang 24}
return null ;
}
public Boolean CheckPharId ( int id) {
for ( Pharmacist d : listPhar) {
for ( Pharmacist d : listPhar) {
System out.println( " -" );
// String search = Console.ReadLine();
// foreach (Pharmacist ph in listPhar)
Pharmacist ph = new Pharmacist();
ph set_id(listPhar.size() + 1 );
while ( true ) {
System out.print( "Enter Pharmarcist name: " );
String name = sc nextLine();
ph set_name( name );
System out.print( "Enter Pharmarcist age: " );
int age = sc nextInt();
ph set_age( age );
sc nextLine();
System out.print( "Enter Pharmarcist phone number: " );
Trang 25try {
String phone = sc nextLine();
ph set_phone( phone );
break ;
} catch ( Exception er) {
System out.println( "The Pharmarcist cost input is not in the correct format!" );
Trang 26} else System out.println( "Invalid ID!!!" ); }
}
Index of comments