-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment26.java
162 lines (142 loc) · 3.83 KB
/
Assignment26.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.util.Scanner;
/*
Assignment 26
An educational institute wishes to maintain a database of its employees. The database is devided into a number of classes, namely, staff, teacher, officer and clerk. The data members of the classes are as follows:
staff class - code and name
teacher class - code, name, subject and department
officer class - code, name and grade
clerk class - code, name and department
Specify all the classes and define methods to
create an array of objects and retrieve individual information using code
*/
import java.util.Scanner;
class Staff {
protected int code;
private String name;
Scanner sc = new Scanner(System.in);
public Staff getData() {
System.out.println("Enter staff info : Code and Name");
code = sc.nextInt();
sc.nextLine();
name = sc.nextLine();
return this;
}
public void showInfo() {
System.out.println("Code : " + code + "\nName : " + name);
}
}
class Clerk extends Staff {
private String department;
public Clerk clerkInfo() {
System.out.println("Clerk : ");
getData();
System.out.println("Department : ");
department = sc.nextLine();
return this;
}
public void Info1() {
showInfo();
System.out.println("Staff Type : Clerk");
System.out.println("Clerk Department : " + department);
}
}
class Officer extends Staff {
private String grade;
public Officer officerInfo() {
System.out.println("Officer : ");
getData();
System.out.println("Grade : ");
grade = sc.nextLine();
return this;
}
public void Info2() {
showInfo();
System.out.println("Staff Type : Officer");
System.out.println("Officer Grade : " + grade);
}
}
class Teacher extends Staff {
private String subject;
private String department;
public Teacher teacherInfo() {
System.out.println("Teacher : ");
getData();
System.out.println("Subject and department:");
subject = sc.nextLine();
department = sc.nextLine();
return this;
}
public void Info3() {
showInfo();
System.out.println("Staff Type : Teacher");
System.out.println("Teacher subject " + subject + " and Department : " + department);
}
}
public class Main {
public static void main(String[] args) {
Staff st[] = createEmployeeObject();
Scanner sc = new Scanner(System.in);
System.out.println("Enter staff code to be searched");
int code = sc.nextInt();
Staff re = getEmployeeInfo(st, code);
if(re != null){
if (re instanceof Clerk){
((Clerk) re).Info1();
}
else if(re instanceof Officer)
((Officer)re).Info2();
else if(re instanceof Teacher)
((Teacher) re).Info3();
}
else
System.out.println("Inavlid record");
}
public static Staff[] createEmployeeObject() {
Staff emp[] = {
new Staff().getData(),
new Clerk().clerkInfo(),
new Officer().officerInfo(),
new Teacher().teacherInfo(),
};
return emp;
}
public static Staff getEmployeeInfo(Staff[] s, int code) {
for (Staff st : s) {
if (st.code == code) {
return st;
}
}
return null;
}
}
/*
Enter staff info : Code and Name
101
A
Clerk :
Enter staff info : Code and Name
201
B
Department :
Accounts
Officer :
Enter staff info : Code and Name
301
C
Grade :
Grade A
Teacher :
Enter staff info : Code and Name
401
D
Subject and department:
Optics
Physics
Enter staff code to be searched
301
Code : 301
Name : C
Staff Type : Officer
Officer Grade : Grade A
Process finished with exit code 0
*/