Как обработать исключение на одинаковые вхождения в sorted dictionary? - C#
Формулировка задачи:
Добрый вечер, помогите пожалуйста правильно использовать исключение ExistExceptions, оно должно срабатывать в если студент с такой фамилией и именем уже есть в списке. когда я пытаюсь внести в sorted dictionary, то получаю ошибку, т.к. такой ключ уже есть. Но как-то не могу понять, куда и как правильно обработать это исключение.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hometask10
{
class Student
{
public string Name { get; set; }
public string Lastname { get; set; }
public Student(string name, string lastname)
{
this.Name = name;
this.Lastname = lastname;
}
public class CompareByLastname : IComparer<Student>
{
public int Compare(Student x, Student y)
{
try
{
if ((x.Lastname == y.Lastname) & (x.Name == y.Name))
{
throw new ExistExceptions();
}
else
{
if (x.Lastname == y.Lastname)
{
return String.Compare(x.Name, y.Name);
}
return String.Compare(x.Lastname, y.Lastname);
}
}
catch (ExistExceptions ee)
{
Console.WriteLine("{0}", ee.Message);
return 0;
}
}
}
}
class RangeException : Exception
{
public override string Message
{
get
{
return "Range Exception";
}
}
}
class ExistExceptions : Exception
{
public override string Message
{
get
{
return "Exist Exceptions";
}
}
}
class DictionaryStudents
{
public static SortedDictionary<Student, int> dic = new SortedDictionary<Student, int>(new Student.CompareByLastname());
public void AddStudent(Student st, int mark)
{
try
{
dic.Add(st, mark);
if (mark < 0)
{
dic.Remove(st);
throw new RangeException();
}
}
catch (RangeException re)
{
Console.WriteLine("Mark {0} is out of bounds [0..100] Exception : {1}", mark, re.Message);
}
}
public void ListStudents()
{
foreach (KeyValuePair<Student, int> entry in DictionaryStudents.dic)
{
Student s = entry.Key;
int v = entry.Value;
Console.WriteLine("Student's name : {0}. lastname: {1}. mark : {2}", s.Name, s.Lastname, v);
}
}
public void AddStudents(int n)
{
Random r = new Random();
for (int i = 0; i < n; i++)
{
Console.WriteLine("Enter student's name");
String s1 = Console.ReadLine();
Console.WriteLine("Enter student's surname");
String s2 = Console.ReadLine();
Student s = new Student(s1, s2);
dic.Add(s, r.Next(0, 100));
}
}
}
class Program
{
static void Main(string[] args)
{
DictionaryStudents ds = new DictionaryStudents();
Student s1 = new Student("Vasya", "Zoro");
Student s2 = new Student("Ivan", "Doom");
Student s3 = new Student("Nikolay", "Fedorchenko");
Student s4 = new Student("Roman", "Ares");
Student s5 = new Student("Bogdan", "Ares");
Student s6 = new Student("Bogdan", "Ares");
try
{
ds.AddStudent(s1, 95);
ds.AddStudent(s2, 64);
ds.AddStudent(s3, -70);
ds.AddStudent(s4, 80);
ds.AddStudent(s5, 90);
ds.AddStudent(s6, 78);
//if(){
//throw new ExistExceptions();
//}
}
catch (ExistExceptions ee)
{
Console.WriteLine(ee.Message);
}
//ds.AddStudents(3);
ds.ListStudents();
}
}
}Решение задачи: «Как обработать исключение на одинаковые вхождения в sorted dictionary?»
textual
Листинг программы
if (mark < 0) throw new RangeException(); dic.Add(st, mark);