Skip to content

Commit 63d2a72

Browse files
aananya27tstreamDOTh
authored andcommitted
fixes #77 (#141)
1 parent 5170e74 commit 63d2a72

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class DaoPatternDemo {
2+
public static void main(String[] args) {
3+
StudentDao studentDao = new StudentDaoImpl();
4+
5+
//print all students
6+
for (Student student : studentDao.getAllStudents()) {
7+
System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
8+
}
9+
10+
11+
//update student
12+
Student student =studentDao.getAllStudents().get(0);
13+
student.setName("Michael");
14+
studentDao.updateStudent(student);
15+
16+
//get the student
17+
studentDao.getStudent(0);
18+
System.out.println("Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Student {
2+
private String name;
3+
private int rollNo;
4+
5+
Student(String name, int rollNo){
6+
this.name = name;
7+
this.rollNo = rollNo;
8+
}
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
18+
public int getRollNo() {
19+
return rollNo;
20+
}
21+
22+
public void setRollNo(int rollNo) {
23+
this.rollNo = rollNo;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import java.util.List;
2+
3+
public interface StudentDao {
4+
public List<Student> getAllStudents();
5+
public Student getStudent(int rollNo);
6+
public void updateStudent(Student student);
7+
public void deleteStudent(Student student);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class StudentDaoImpl implements StudentDao {
5+
6+
//list is working as a database
7+
List<Student> students;
8+
9+
public StudentDaoImpl(){
10+
students = new ArrayList<Student>();
11+
Student student1 = new Student("Robert",0);
12+
Student student2 = new Student("John",1);
13+
students.add(student1);
14+
students.add(student2);
15+
}
16+
@Override
17+
public void deleteStudent(Student student) {
18+
students.remove(student.getRollNo());
19+
System.out.println("Student: Roll No " + student.getRollNo() + ", deleted from database");
20+
}
21+
22+
//retrive list of students from the database
23+
@Override
24+
public List<Student> getAllStudents() {
25+
return students;
26+
}
27+
28+
@Override
29+
public Student getStudent(int rollNo) {
30+
return students.get(rollNo);
31+
}
32+
33+
@Override
34+
public void updateStudent(Student student) {
35+
students.get(student.getRollNo()).setName(student.getName());
36+
System.out.println("Student: Roll No " + student.getRollNo() + ", updated in the database");
37+
}
38+
}

0 commit comments

Comments
 (0)