-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdao.java
More file actions
83 lines (65 loc) · 1.98 KB
/
dao.java
File metadata and controls
83 lines (65 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.ArrayList;
import java.util.List;
public class Student{
private String name;
private int rollNo;
Student(String name, int rollNo){
this.name = name;
this.rollNo = rollNo;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getRollNo(){
return rollNo;
}
public void setRollNo(int rollNo){
this.rollNo = rollNo;
}
}
public interface StudentDao{
public List<Student> getAllStudent();
public Student getStudent(int rollNo);
public void updateStudent(Student student);
public void deleteStudent(Student student);
}
public class StudentDaoImpl implements StudentDao{
List<Student> students;
public StudentDaoImpl(){
students = new ArrayList<Student>();
Student student1 = new Student("John",0);
Student student2 = new Student("Mark",1);
students.add(student1);
students.add(student2);
}
public void deleteStudent(Student student){
students.remove(student.getRollNo());
System.out.println("Student: Roll# "+ student.getRollNo() + ", deleted.");
}
public List<Student> getAllStudents(){
return students;
}
public Student getStudent(int rollNo){
return students.get(rollNo);
}
public void updateStudent(Student student){
students.get(student.getRollNo()).setName(student.getName());
System.out.println("Student: Roll# "+ student.getRollNo() + ", updated.");
}
}
public class dao{
public static void main(String[] args){
StudentDao studentDao = new StudentDaoImpl();
for(Student student : studentDao.getAllStudents()){
System.out.println("Student: [Roll#: "+ student.getRollNo() +", Name: "+ student.getName() + " ]");
}
Student student = studentDao.getAllStudents().get(0);
student.setName("Jaime");
studentDao.updateStudent(student);
studentDao.getStudent(0);
System.out.println("Student: [Roll#: "+ student.getRollNo() +", Name: "+ student.getName() +" ]");
}
}