forked from panxuc/EESAST-hw2024-CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Student.cs
100 lines (97 loc) · 2.66 KB
/
Student.cs
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
namespace StudentGradeManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public interface IStudent
{
public string Name { get; set; }
public int ID { get; set; }
public Dictionary<string, Grade> Grades { get; }
public void AddGrade(string course, string credit, string score);
public void AddGrades(List<(string course, int credit, int score)> grades);
public void RemoveGrade(string course);
public void RemoveGrades(List<string> courses);
public int GetTotalCredit();
public double GetTotalGradePoint();
public double GetGPA();
public string ToString();
}
public class Student : IStudent
{
private string name;
public string Name
{
get => name;
public set { name = value; }
}
private int id;
public int ID
{
get => id;
public set { id = value; }
}
private Dictionary<string, Grade> grades;
public Dictionary<string, Grade> Grades
{
get => grades;
}
public void AddGrade(string course, string credit, string score)
{
if(grades.ContainsKey(course))
{
Console.WriteLine("Grade of " + course + " exist");
return;
}
Grade grade = new Grade(int.Parse(credit), int.Parse(score));
grades.Add(course, grade);
}
public void AddGrades(List<(string course, int credit, int score)> grds)
{
foreach (var grd in grds)
{
AddGrade(grd.course,grd.credit, grd.score);
}
}
public void RemoveGrade(string course)
{
if (grades.ContainsKey(course))
grades.Remove(course);
else Console.WriteLine("Grade of " + course + " not exist");
}
public void RemoveGrades(List<string> courses)
{
foreach (string course in courses)
RemoveGrade(course);
}
public int GetTotalCredit()
{
int sum = 0;
foreach (var value in grades.Values)
sum += value.Credit;
return sum;
}
public double GetTotalGradePoint()
{
double sum = 0;
foreach (var value in grades.Values)
sum += value.GradePoint();
return sum;
}
public double GetGPA()
{
double sum = 0;
foreach (var value in grades.Values)
sum += value.Credit * value.GradePoint();
return sum / GetTotalCredit();
}
public string ToString()
{
var stdent = $"Name:{name}\nID: { id }\nCourse\tCredit\tScore\n";
foreach (var grade in grades)
{
stdent += $"{grade.Key}\t{grade.Value.Credit}\t{grade.Value.Score}\n";
}
}
}