-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment.java
More file actions
119 lines (93 loc) · 2.17 KB
/
Copy pathAssignment.java
File metadata and controls
119 lines (93 loc) · 2.17 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
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
import java.util.HashSet;
import java.util.Scanner;
public class Assignment {
HashSet<Department> hs = new HashSet<Department>();
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
HashSet<Department> hs = new HashSet<Department>();
Department d1 = new Department();
d1.setDetails();
d1.printDetails();
hs.add(d1);
Department d2 = new Department();
d2.setDetails();
d2.printDetails();
try {
Department.checkDuplicate(hs, d2);
}
catch (DuplicateException e)
{
System.out.println(e);
System.out.println("Enter other department name: ");
String n = sc.next();
d2.setDeptName(n);
d2.printDetails();
e.printStackTrace();
}
}
}
class Department
{
String deptName;
String location;
int noOfEmp;
static int id =0;
public Department()
{
noOfEmp =0;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getNoOfEmp() {
return noOfEmp;
}
public void setNoOfEmp(int noOfEmp) {
this.noOfEmp = noOfEmp;
}
void setDetails()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter department name:");
String name = sc.next();
System.out.println("Enter location:");
String loc = sc.next();
setDeptName(name);
setLocation(loc);
id = id + 10;
}
void printDetails()
{
System.out.println("Id "+id);
System.out.println("Name "+getDeptName());
System.out.println("Location "+getLocation());
System.out.println("No of Emp "+getNoOfEmp());
}
static void checkDuplicate(HashSet<Department> h, Department d) throws DuplicateException
{
for(Department obj: h)
{
if(obj.deptName.equals(d.deptName))
{
throw new DuplicateException("Department name is duplicate");
}
}
}
}
class DuplicateException extends Exception
{
public DuplicateException(String msg)
{
super(msg);
}
}