-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradeItem.java
More file actions
executable file
·123 lines (108 loc) · 3.47 KB
/
GradeItem.java
File metadata and controls
executable file
·123 lines (108 loc) · 3.47 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
import java.util.ArrayList;
import java.util.Scanner;
import java.text.DecimalFormat;
/* This class represents the individual grade item within a section */
public class GradeItem {
private double max;
private double mark;
private String description;
private ArrayList<String> comments; // Any comments which accrue over time
// If you just receive a string, we'll assume it needs to be parsed
public GradeItem(String s) {
description = findDescription(s);
max = findMax(s);
mark = 0;
comments = new ArrayList<String>();
}
public GradeItem(String d, double m) {
description = d;
max = m;
mark = 0;
comments = new ArrayList<String>();
}
// Goes through the original line from the rubric and pulls out the description
// Description is the 3rd -> end words
private static String findDescription(String s) {
String response = "";
String[] broken = s.trim().split(" ");
try {
for (int i = 3; i < broken.length; i++)
response+=broken[i]+" ";
}
catch (Exception e) { return s; }
return response;
}
private static double findMax(String s) {
String response = "";
String[] broken = s.trim().split(" ");
try {
for (int i = 0; i < broken[0].length(); i++) {
try {
double d = Double.parseDouble(""+broken[0].charAt(i));
response+=broken[0].charAt(i);
}
catch(Exception e) {}
}
}
catch (Exception e) { return Double.parseDouble(s); }
return Double.parseDouble(response);
}
// Currently, this does not allow for bonus marks. It takes in the number and
// sets the mark, up to the maximum. Returns the mark that was added.
public double addMark(double d) {
if (d > max)
mark = max;
else
mark = d;
return mark;
}
// Returns a string of all comments accrued over time. Checks if there's a period. If not, adds one.
public String getCommentString() {
String s = "";
try {
if (comments.isEmpty())
throw new IndexOutOfBoundsException();
for (String c : comments) {
if (c.trim().charAt(c.length()-1) != '.') //If there isn't a period at the end, add one
c += ".";
s+=c;
}
}
// No comments, return the description
catch (Exception e) {
return description;
}
return s;
}
// Adds a comment to the list of comments
public void addComment(String s) {
comments.add(s);
}
public String toString() {
DecimalFormat df = new DecimalFormat("0.00");
return String.format("%s/%s : %s", df.format(mark), df.format(max), getCommentString());
}
// Tests the grade item and returns the grade received
public double test() {
Scanner sc = new Scanner(System.in);
double m;
String comment;
while(true) {
System.out.print(" " + this + ". Enter your mark: " );
try {
m = sc.nextDouble();
break;
}
catch (Exception e) {
RubricParser.systemMessage("Invalid input. Try again.");
sc.nextLine();
}
}
System.out.print(" Please type any comments you have (leave blank for rubric description): " );
comment = sc.nextLine();
comment = sc.nextLine();
comments.add(comment);
return addMark(m);
}
public double getMax() { return max; }
}