-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathXploreOPAproblem.cs
More file actions
152 lines (140 loc) · 6.01 KB
/
XploreOPAproblem.cs
File metadata and controls
152 lines (140 loc) · 6.01 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
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
/*
Author: vishva patel
Program: TCS OPA C# hands-On
Please find the pic for the problem statement.
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace XploreOPA
{
class Program
{
static void Main(string[] args)
{
//Pre written code by hackerrank.
List<department> dept = new List<department>
{
new department(){departmentid=1, dept_Name="EEE", dept_location="Mumbai"},
new department() { departmentid = 2, dept_Name = "ECE", dept_location = "Chennai" },
new department() { departmentid = 3, dept_Name = "CSE", dept_location = "Mumbai" },
new department() { departmentid = 4, dept_Name = "IT", dept_location = "Chennai" }
};
//Given in the prewritten code.
List<student> studentlist = new List<student>();
studentlist.Add(new student("Priya", 23, 1));
studentlist.Add(new student("Madhu", 25, 2));
studentlist.Add(new student("Asha", 24, 1));
studentlist.Add(new student("Arpit", 23, 1));
studentlist.Add(new student("Bhavik", 25, 4));
studentlist.Add(new student("Kamala", 23, 2));
Dictionary<int, int> count = new Dictionary<int, int>(); // To maintain the strength of 3 in each department, will be checked while adding students.
count.Add(1, 3); //Key = department id and value = Number of students which cannt exceed 3 in each department.
count.Add(2, 2);
count.Add(3, 0);
count.Add(4, 1);
int choice = Convert.ToInt32(Console.ReadLine()); //Choice to perform operatioin.
try
{
switch (choice)
{
//Viewing the number of students according to the format.
case 1:
string nme = string.Empty;
string location = string.Empty;
foreach (student s in studentlist)
{
foreach (department d in dept)
{
if (s.st_dept_id == d.departmentid)
{
nme = d.dept_Name;
location = d.dept_location;
}
}
Console.WriteLine($"{s.st_Name}({s.st_age}) - Deapartment : {nme},{location}");
}
break;
//Adding to the department.
case 2:
string name = Console.ReadLine();
int age = Convert.ToInt32(Console.ReadLine());
string dept_name = Console.ReadLine();
string dept_loc = Console.ReadLine();
int dept_id = 0;
bool flag = false;
foreach (department d in dept)
{
if (d.dept_Name == dept_name && d.dept_location == dept_loc)
{
dept_id = d.departmentid;
flag = true;
}
}
if (flag == false)
{
Console.WriteLine("Department does not exist");
break;
}
if (age < 18)
{
Console.WriteLine($"{name} is too young to join");
break;
}
if (age > 25)
{
Console.WriteLine($"{name} is too old to join");
break;
}
if (flag == true)
{
if (count[dept_id] == 3)
{
Console.WriteLine("Student limit excedded");
}
else
{
studentlist.Add(new student(name, age, dept_id));
count[dept_id]++;
Console.WriteLine($"{name} has been added successfully");
foreach (student s in studentlist)
{
foreach (department d in dept)
{
if (s.st_dept_id == d.departmentid)
{
name = d.dept_Name;
dept_loc = d.dept_location;
}
}
Console.WriteLine($"{s.st_Name}({s.st_age}) - Deapartment : {name},{dept_loc}");
}
}
}
break;
}
}catch(Exception e)
{
string msg = e.ToString();
}
}
}
class department
{
public int departmentid { get; set; }
public string dept_Name { get; set; }
public string dept_location { get; set; }
}
class student
{
public string st_Name { get; set; }
public int st_age { get; set; }
public int st_dept_id { get; set; }
public student(string name, int age, int id)
{
st_Name = name;
st_age = age;
st_dept_id = id;
}
}
}