-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentReport.go
102 lines (92 loc) · 2.83 KB
/
StudentReport.go
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
package main
import (
"fmt"
)
type StudentReport struct {
Name string
Major string
SemestersOrdered []string
Grades map[string]map[string]*CourseGrade
}
func NewStudentReport(name string, major string, grades map[string]map[string]*CourseGrade) *StudentReport {
return &StudentReport{
Name: name,
Major: major,
Grades: grades,
}
}
// make a copy of the student report
func (sr * StudentReport) Copy() *StudentReport {
newGrades := make(map[string]map[string]*CourseGrade)
for term, courseGrades := range sr.Grades {
newGrades[term] = make(map[string]*CourseGrade)
for courseCode, courseGrade := range courseGrades {
newGrades[term][courseCode] = courseGrade.Copy()
}
}
return &StudentReport{
Name: sr.Name,
Major: sr.Major,
SemestersOrdered: sr.SemestersOrdered,
Grades: newGrades,
}
}
func (sr *StudentReport) PrintReport() {
fmt.Printf("Student Name: %s\n", sr.Name)
fmt.Println(sr.Major)
fmt.Printf("Total credits: %d\n", sr.GetTotalCredits())
for term := range sr.Grades {
fmt.Println(term)
for _, courseGrade := range sr.Grades[term] {
fmt.Println(courseGrade.String())
}
fmt.Println("-------------------------------------")
}
}
func (sr *StudentReport) AddNewTerm(term string) bool {
if sr.Grades[term] == nil{
sr.SemestersOrdered = append(sr.SemestersOrdered, term)
sr.Grades[term] = make(map[string]*CourseGrade)
return true
}
return false
}
func (sr *StudentReport) AddCourseGrade(semester string , courseGrade *CourseGrade) bool {
if sr.Grades == nil {
sr.Grades = make(map[string]map[string]*CourseGrade)
}
if sr.Grades[semester] == nil {
sr.Grades[semester] = make(map[string]*CourseGrade)
}
if sr.Grades[semester][courseGrade.CourseCode] != nil {
return false
}
sr.Grades[semester][courseGrade.CourseCode] = courseGrade
return true
}
func (sr *StudentReport) ModifySemesterCourseGrade(semester string, courseCode string, newGrade string) *CourseGrade {
sr.Grades[semester][courseCode].Grade = newGrade
return sr.Grades[semester][courseCode]
}
func (sr *StudentReport) ModifyCourseGrade( courseCode string, newGrade string) {
// loop through all semesters
for _, termGrades := range sr.Grades {
for _, courseGrade := range termGrades {
if courseGrade.CourseCode == courseCode {
courseGrade.Grade = newGrade
}
}
}
}
func (sr *StudentReport) GetTotalCredits() int {
total := 0
for term := range sr.Grades {
termTotal := 0
for _, courseGrade := range sr.Grades[term] {
termTotal += courseGrade.Credits
}
fmt.Printf("Term %s: %d\n", term, termTotal)
total += termTotal
}
return total
}