Skip to content

Commit af4a504

Browse files
committedNov 22, 2017
GradeCompare2 - with proper encapsulation
1 parent 3c07cd4 commit af4a504

File tree

4 files changed

+211
-22
lines changed

4 files changed

+211
-22
lines changed
 

‎src/GradeCompare.java

+22-10
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,30 @@
55
public class GradeCompare {
66

77
private ArrayList<Integer[]> data = new ArrayList<>(); // has scores
8-
private ArrayList<String> subjects; // has subjects
8+
//private ArrayList<String> subjects; // has subjects
99
private ArrayList<String> names = new ArrayList<>(); //has names.
1010

1111
private ArrayList<Student> students = new ArrayList<>();
1212

1313
public GradeCompare(ArrayList<String[]> d) {
1414
for(int i=0;i<d.size();i++) {
1515
if(i==0) {
16-
this.subjects = new ArrayList<>(
16+
// this.subjects = new ArrayList<>(
17+
// Arrays.asList(
18+
// Arrays.copyOfRange(d.get(i),1,d.get(i).length)//removes title of first column.
19+
// ));//remove
20+
Student.setSubjects(new ArrayList<String>(
1721
Arrays.asList(
1822
Arrays.copyOfRange(d.get(i),1,d.get(i).length)//removes title of first column.
19-
));
23+
)));
2024
}
2125
else {
2226
this.names.add(d.get(i)[0]);
2327
Integer[] temp = new Integer[4];
2428
for(int j = 1;j<d.get(i).length;j++) {
2529
temp[j-1] = Integer.parseInt(d.get(i)[j]);
2630
}
27-
this.data.add(temp);
31+
this.data.add(temp);//remove
2832
students.add(new Student(d.get(i)[0],temp));
2933

3034
}
@@ -36,7 +40,7 @@ public ArrayList<String> getNames() {
3640
}
3741

3842
public ArrayList<String> getSubjects(){
39-
return this.subjects;
43+
return Student.getSubjects();
4044
}
4145
public String findTopSubjectScorer(String subject) {//ある科目の最高点数の持ち主の名前を見つける
4246
int column = getSubectColumn(subject);
@@ -47,6 +51,7 @@ public String findTopSubjectScorer(String subject) {//ある科目の最高点
4751
public Integer findTopScoreInSubject(String subject) {//ある科目の最高点数を見つける
4852
int column = getSubectColumn(subject);
4953
int row = getHighestRowInColumn(column);
54+
Student student = getStudentFromName(subject);
5055
return this.data.get(row)[column];
5156
}
5257

@@ -78,7 +83,7 @@ public double averageScore(String name) {
7883
for(Integer score:this.data.get(row)) {
7984
average += score;
8085
}
81-
return (average / ((double) this.subjects.size()));
86+
return (average / ((double) Student.getSubjects().size()));
8287
}
8388
public int studentHighScore(String name) {
8489
int row = getNameRow(name);
@@ -105,9 +110,9 @@ public int studentLowScore(String name) {
105110
public ArrayList<String> getSubjectsFromScore(String name,int score) {
106111
int row = getNameRow(name);
107112
ArrayList<String> result = new ArrayList<String>();
108-
for(int i=0;i<this.subjects.size();i++) {
113+
for(int i=0;i<Student.getSubjects().size();i++) {
109114
if(this.data.get(row)[i]==score) {
110-
result.add(this.subjects.get(i));
115+
result.add(Student.getSubjects().get(i));
111116
}
112117
}
113118
return result;
@@ -117,9 +122,16 @@ public ArrayList<Student> getOrderedList(){
117122
Collections.sort(this.students);
118123
return this.students;
119124
}
120-
125+
private Student getStudentFromName(String name) {
126+
for(Student student:this.students) {
127+
if(student.getName()==name) {
128+
return student;
129+
}
130+
}
131+
return null;
132+
}
121133
private int getSubectColumn(String subject) {//科目の配列順番号を返す
122-
return this.subjects.indexOf(subject);
134+
return Student.getSubjects().indexOf(subject);
123135
}
124136
private int getNameRow(String name) {
125137
return this.names.indexOf(name);

‎src/GradeCompare2.java

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.Collections;
4+
5+
public class GradeCompare2 {
6+
private ArrayList<Student> students = new ArrayList<>();
7+
public GradeCompare2(ArrayList<String[]> d) {
8+
for(int i=0;i<d.size();i++) {
9+
if(i==0) {
10+
11+
Student.setSubjects(new ArrayList<String>(
12+
Arrays.asList(
13+
Arrays.copyOfRange(d.get(i),1,d.get(i).length)//removes title of first column.
14+
)));
15+
}
16+
else {
17+
Integer[] temp = new Integer[4];
18+
for(int j = 1;j<d.get(i).length;j++) {
19+
temp[j-1] = Integer.parseInt(d.get(i)[j]);
20+
}
21+
this.students.add(new Student(d.get(i)[0],temp));
22+
23+
}
24+
}
25+
}
26+
public ArrayList<String> getNames() {//生徒の名前を返す
27+
ArrayList<String> names = new ArrayList<>();
28+
for(Student student:this.students) {
29+
names.add(student.getName());
30+
}
31+
return names;
32+
}
33+
34+
public int findTopScoreInSubject(String subject) {//ある科目の最高点数を見つける
35+
int topScore = 0;
36+
for(Student student : this.students) {
37+
if(student.getSubjectScore(subject)>topScore) {
38+
topScore = student.getSubjectScore(subject);
39+
}
40+
}
41+
return topScore;
42+
}
43+
44+
public String findTopSubjectScorer(String subject) {//ある科目の最高点数の持ち主の名前を見つける
45+
Student topScorer = this.students.get(0);
46+
for(Student student : this.students) {
47+
if(student.getSubjectScore(subject)>topScorer.getSubjectScore(subject)) {
48+
topScorer = student;
49+
}
50+
}
51+
return topScorer.getName();
52+
}
53+
54+
public double getAverageWithoutLowest(String subject) {//科目の平均を計算する(少点数二位まで)
55+
if(Student.getSubjects().indexOf(subject)<0) {
56+
System.out.println("生徒たちはその科目を取ってません");
57+
return 0;
58+
}
59+
double total = 0d;
60+
int min = 100;
61+
for(Student student:this.students) {
62+
int score = student.getSubjectScore(subject);
63+
if(score<min) {
64+
min = score;
65+
}
66+
total += score;
67+
}
68+
total -= min;
69+
70+
return (total / ((double) this.students.size()-1));
71+
}
72+
73+
public int totalScore(String name) {//ある人の合計点数を計算する
74+
Student student = getStudentFromName(name);
75+
return student.totalMarks();
76+
}
77+
78+
public int studentHighScore(String name) {//三番に使われる、個人の生徒の高い点数を見つける
79+
Student student = getStudentFromName(name);
80+
return student.highestScore();
81+
}
82+
83+
public int studentLowScore(String name) {
84+
Student student = getStudentFromName(name);
85+
return student.lowestScore();
86+
}
87+
public double averageScore(String name) {
88+
Student student = getStudentFromName(name);
89+
return student.averageScore();
90+
}
91+
92+
public ArrayList<String> getSubjectsFromScore(String name,int score) {
93+
Student student = getStudentFromName(name);
94+
return student.getSubjectsFromScore(score);
95+
}
96+
97+
public ArrayList<Student> getOrderedList(){
98+
Collections.sort(this.students);
99+
return this.students;
100+
}
101+
102+
public ArrayList<String> getSubjects(){
103+
return Student.getSubjects();
104+
}
105+
106+
private Student getStudentFromName(String name) {
107+
for(Student student:this.students) {
108+
if(student.getName()==name) {
109+
return student;
110+
}
111+
}
112+
return null;
113+
}
114+
115+
116+
}

‎src/Main.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public static void main(String[] args) {
99
//new TeamScore();
1010
//new RunFileExercise();
1111
CsvReader csvReader = new CsvReader(System.getProperty("user.dir")+"/files/csvPractice01.csv");
12-
GradeCompare grades = new GradeCompare(csvReader.getData());
12+
GradeCompare2 grades = new GradeCompare2(csvReader.getData());
13+
1314
System.out.println("1.数学の最高得点");
1415
System.out.println(grades.findTopScoreInSubject("数学"));
1516
System.out.println("\n2.各科目の最高得点者の氏名と得点\n");
@@ -19,21 +20,21 @@ public static void main(String[] args) {
1920
System.out.println("\n4. 人別の全科目合計点・全科目平均点・最高点だった教科と点数・最低点だった教科と点数\n");
2021
printScoreDetails(grades);
2122
System.out.println("\n5. 成績の合計点順のランキング\n");
22-
printRankings(grades);
23+
printRankings(grades);
2324
}
2425

25-
public static void topScorers(GradeCompare grades) {
26+
public static void topScorers(GradeCompare2 grades) {
2627
//findTopSubjectScorer
2728
for(String subject :grades.getSubjects()) {
2829
System.out.println(subject+"の最高得点者は、"+grades.findTopSubjectScorer(subject)+"、"+grades.findTopScoreInSubject(subject)+"点です。");//全部同じ人でした。。。。
2930
}
3031
}
31-
public static void printSubjectAverages(GradeCompare grades) {
32+
public static void printSubjectAverages(GradeCompare2 grades) {
3233
for (String subject:grades.getSubjects()) {
3334
System.out.println(subject+"の平均点数(小数点第二位まで)は"+grades.getAverageWithoutLowest(subject)+"点です。");
3435
}
3536
}
36-
public static void printScoreDetails(GradeCompare grades) {
37+
public static void printScoreDetails(GradeCompare2 grades) {
3738
DecimalFormat df = new DecimalFormat("#.00");
3839
String toPrint = "";
3940
for(String name:grades.getNames()) {
@@ -54,7 +55,7 @@ public static String makeSubjectString(ArrayList<String> array) {//配列を文
5455
}
5556
return result;
5657
}
57-
public static void printRankings(GradeCompare grades) {
58+
public static void printRankings(GradeCompare2 grades) {
5859
ArrayList<Student> rankings = grades.getOrderedList();
5960
for(int i = 0; i<rankings.size();i++) {
6061
System.out.println((i+1)+"位: "+rankings.get(i).getName()+"さん "+rankings.get(i).totalMarks()+"点");

‎src/Student.java

+66-6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.Collections;
14

25
public class Student implements Comparable<Student>{
36
private String name;
4-
private Integer[] marks;
7+
private ArrayList<Integer> marks; //Collections.max などを使うためにこうしました・
8+
private static ArrayList<String> subjects;
59

610
public Student(String name, Integer[] marks) {
711
this.name = name;
8-
this.marks = marks;
12+
this.marks = new ArrayList<>(Arrays.asList(marks));
913
}
1014

1115
public String getName() {
1216
return this.name;
1317
}
1418

15-
public int totalMarks() {
19+
public int totalMarks() {//合計点数を計算する
1620
int total = 0;
1721
for(Integer mark:marks) {
1822
total += mark;
1923
}
2024
return total;
2125
}
22-
2326
@Override
24-
public int compareTo(Student other) {
25-
// TODO Auto-generated method stub
27+
public int compareTo(Student other) {//ランキングのために使われる
2628
if(this.totalMarks()<other.totalMarks()) {
2729
return 1;
2830
}
@@ -34,5 +36,63 @@ else if(this.totalMarks()>other.totalMarks()) {
3436
}
3537
}
3638

39+
public Integer getSubjectScore(String subject) {//ある科目のこの生徒の点数を表す
40+
int index = Student.getSubjects().indexOf(subject);
41+
if(index >= 0) {
42+
return this.marks.get(index);
43+
}
44+
else {
45+
System.out.println(this.name+"は"+subject+"をとっておりません");
46+
return -1;
47+
}
48+
}
49+
50+
public int highestScore() {//三番に使われる、個人の生徒の高い点数を見つける
51+
return Collections.max(this.marks);
52+
}
53+
public int lowestScore() { //三番に使われる、個人の生徒の低い点数を見つける
54+
return Collections.min(this.marks);
55+
}
56+
57+
public double averageScore() {//平均点数を見つける
58+
double total = 0d;
59+
for(Integer score:this.marks) {
60+
total +=score;
61+
}
62+
return (total / ((double) this.marks.size()));
63+
}
64+
65+
public ArrayList<String> getSubjectsFromScore(int score) {//四番に使われる、何科目でscoreという点数をもらった?
66+
ArrayList<String> subjects = new ArrayList<>();
67+
for(int i = 0; i < this.marks.size();i++) {
68+
if(this.marks.get(i)==score) {
69+
subjects.add(Student.getSubjects().get(i));
70+
}
71+
}
72+
if(subjects.size()>0) {
73+
return subjects; //もしほかのところに使う場合にはnullチェックができるために
74+
}
75+
return null; //その点数で科目がなかった
76+
}
77+
78+
79+
//科目リストについて:
80+
public static void setSubjects(ArrayList<String> subjects) {
81+
Student.subjects = subjects;
82+
}
83+
public static ArrayList<String> getSubjects() {
84+
return Student.subjects;
85+
86+
}
87+
@Override
88+
public String toString() {//テストのためにありました
89+
String result = "";
90+
result += this.getName();
91+
for(int i = 0;i<this.marks.size();i++){
92+
result += Student.getSubjects().get(i)+" - "+this.marks.get(i)+"\n";
93+
}
94+
return result;
95+
}
96+
3797

3898
}

0 commit comments

Comments
 (0)
Please sign in to comment.