Skip to content

Commit da9a90a

Browse files
committed
JECRINDWRK-80 Student Quiz Scores
1 parent 0642dde commit da9a90a

File tree

9 files changed

+271
-111
lines changed

9 files changed

+271
-111
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
/ObjectInstantiation/target/
1414
/10_Refactored_Labs/target/
1515
/ListExamples/target/
16-
/MapExamples/target/
16+
/MapExamples/target/
17+
/SimpleFileIO/target/

0_Labs/StudentQuizzes.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Mick::Jagger::95.23::77.98::86.17::
2+
Barkola::Barky::95.23::77.98::86.17::

0_Labs/src/main/java/StudentQuizScores/App.java

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package StudentQuizScores;
77

8+
import java.io.IOException;
9+
810
/**
911
*
1012
* @author jeffc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package StudentQuizScores;
7+
8+
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.IO;
9+
import java.io.BufferedReader;
10+
import java.io.FileReader;
11+
import java.io.FileWriter;
12+
import java.io.IOException;
13+
import java.io.PrintWriter;
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import java.util.Scanner;
17+
import java.util.Set;
18+
19+
/**
20+
*
21+
* @author jeffc
22+
*/
23+
public class Classes {
24+
25+
UserIO IO = new UserIOImpl();
26+
String firstName, lastName;
27+
List<Student> students = new ArrayList<>();
28+
Student currentStudent;
29+
boolean has = false;
30+
int studentIndex = -1;
31+
32+
public void readData() {
33+
try {
34+
Scanner sc = new Scanner(new BufferedReader(new FileReader("StudentQuizzes.txt")));
35+
while(sc.hasNextLine()) {
36+
37+
String currentLine = sc.nextLine();
38+
String delims = "[::]+";
39+
String[] tokens = currentLine.split(delims);
40+
41+
/*
42+
for (String nextToken : tokens) {
43+
IO.print(nextToken);
44+
}*/
45+
firstName = tokens[0];
46+
lastName = tokens[1];
47+
48+
currentStudent = new Student(firstName, lastName);
49+
students.add(currentStudent);
50+
for (int i = 2; i < tokens.length; i++) {
51+
double currentScore = Double.parseDouble(tokens[i]);
52+
currentStudent.quizzes.put(i-1, currentScore);
53+
}
54+
}
55+
} catch (IOException ex) {
56+
IO.print("Your data have been corrupted. Stop letting them stay out late at night.");
57+
}
58+
}
59+
60+
public void saveData() {
61+
try {
62+
PrintWriter out = new PrintWriter(new FileWriter("StudentQuizzes.txt"));
63+
for (Student currentStudent : students) {
64+
out.print(currentStudent.getFirstName());
65+
out.print("::");
66+
out.print(currentStudent.getLastName());
67+
out.print("::");
68+
69+
for (int quiz : currentStudent.getQuizKeys()) {
70+
out.print(currentStudent.quizzes.get(quiz));
71+
out.print("::");
72+
}
73+
74+
out.println("");
75+
76+
}
77+
78+
out.flush();
79+
out.close();
80+
81+
} catch (IOException ex) {
82+
IO.print("No write file exists. Error #693883");
83+
}
84+
85+
}
86+
87+
public void addNewStudent() {
88+
firstName = IO.readString("Enter the first name: ");
89+
lastName = IO.readString("Enter the last name: ");
90+
has = this.hasStudent(firstName, lastName);
91+
if (has) {
92+
IO.print("That name is already there, ya dingus!");
93+
} else {
94+
Student newStudent = new Student(firstName, lastName);
95+
students.add(newStudent);
96+
newStudent.quizzes.put(1, 95.23d);
97+
newStudent.quizzes.put(2, 77.98d);
98+
newStudent.quizzes.put(3, 86.17d);
99+
IO.print("New student successfully added.");
100+
}
101+
}
102+
103+
public boolean hasStudent(String fN, String lN) {
104+
firstName = fN;
105+
lastName = lN;
106+
107+
has = false;
108+
109+
for (int i = 0; i < students.size(); i++) {
110+
if (students.get(i).getFirstName().equals(firstName) && students.get(i).getLastName().equals(lastName)) {
111+
has = true;
112+
}
113+
}
114+
115+
return has;
116+
}
117+
118+
public void removeStudent() {
119+
firstName = IO.readString("Enter the first name: ");
120+
lastName = IO.readString("Enter the last name: ");
121+
has = hasStudent(firstName, lastName);
122+
if (!has) {
123+
IO.print("That name isn't there, meathead!");
124+
} else {
125+
studentIndex = getStudentIndex(firstName, lastName);
126+
students.remove(studentIndex);
127+
IO.print("New student successfully removed.");
128+
}
129+
}
130+
131+
public int getStudentIndex(String fN, String lN) {
132+
firstName = fN;
133+
lastName = lN;
134+
135+
int index = 0;
136+
137+
for (int i = 0; i < students.size(); i++) {
138+
if (students.get(i).getFirstName().equals(firstName) && students.get(i).getLastName().equals(lastName)) {
139+
index = i;
140+
}
141+
}
142+
143+
return index;
144+
}
145+
146+
public void viewQuizScores() {
147+
firstName = IO.readString("Enter the first name: ");
148+
lastName = IO.readString("Enter the last name: ");
149+
has = hasStudent(firstName, lastName);
150+
if (!has) {
151+
IO.print("That name isn't there, goofball!");
152+
} else {
153+
studentIndex = getStudentIndex(firstName, lastName);
154+
currentStudent = students.get(studentIndex);
155+
156+
IO.print("");
157+
System.out.println("The quiz scores for " + currentStudent.getFirstName()
158+
+ " " + currentStudent.getLastName() + " are:");
159+
Set<Integer> quizKeys = currentStudent.getQuizKeys();
160+
161+
for (int index : quizKeys) {
162+
System.out.println("Quiz " + index);
163+
IO.print(currentStudent.quizzes.get(index) + " ");
164+
}
165+
IO.print("");
166+
}
167+
}
168+
}
169+
170+

0_Labs/src/main/java/StudentQuizScores/Menu.java

+27-110
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
package StudentQuizScores;
77

88
import java.util.ArrayList;
9-
import java.util.HashMap;
109
import java.util.List;
11-
import java.util.Map;
12-
import java.util.Set;
1310
import java.util.Random;
1411

1512
/**
@@ -29,8 +26,11 @@ public void displayMenu() {
2926
int userChoice = 0, studentIndex = -1;
3027
String firstName, lastName;
3128
boolean has;
29+
Classes classes = new Classes();
3230

33-
while (userChoice != 6) {
31+
classes.readData();
32+
33+
while (userChoice != 7) {
3434

3535
IO.print("");
3636
IO.print("Choose your Quest: ");
@@ -39,130 +39,47 @@ public void displayMenu() {
3939
IO.print("3: Remove Student");
4040
IO.print("4: View List of Student Quiz Scores");
4141
IO.print("5: View Average Quiz Scores for One Student");
42-
IO.print("6: Exit");
42+
IO.print("6: Save Data to File");
43+
IO.print("7: Exit");
4344

44-
userChoice = IO.readInt("", 1, 6);
45+
userChoice = IO.readInt("", 1, 7);
4546

4647
switch (userChoice) {
47-
case 1:
48+
case 1: //View Student List
4849
IO.print("The Students are:");
4950
IO.print("=================");
50-
for (Student currentStudent : students) {
51+
for (currentStudent : students) {
5152
System.out.println(currentStudent.getFirstName() + " " + currentStudent.getLastName());
5253
}
5354
break;
5455

55-
case 2:
56-
firstName = IO.readString("Enter the first name: ");
57-
lastName = IO.readString("Enter the last name: ");
58-
has = hasStudent(firstName, lastName);
59-
if (has) {
60-
IO.print("That name is already there, ya dingus!");
61-
} else {
62-
Student newStudent = new Student(firstName, lastName);
63-
students.add(newStudent);
64-
newStudent.quizzes.put(1, 95.23d);
65-
newStudent.quizzes.put(2, 77.98d);
66-
newStudent.quizzes.put(3, 86.17d);
67-
IO.print("New student successfully added.");
68-
}
56+
case 2: //Add New Student
57+
classes.addNewStudent();
6958
break;
7059

71-
case 3:
72-
firstName = IO.readString("Enter the first name: ");
73-
lastName = IO.readString("Enter the last name: ");
74-
has = hasStudent(firstName, lastName);
75-
if (!has) {
76-
IO.print("That name isn't there, meathead!");
77-
} else {
78-
studentIndex = getStudentIndex(firstName, lastName);
79-
students.remove(studentIndex);
80-
IO.print("New student successfully removed.");
81-
}
60+
case 3: //Remove Student
61+
classes.removeStudent();
8262
break;
8363

84-
case 4:
85-
firstName = IO.readString("Enter the first name: ");
86-
lastName = IO.readString("Enter the last name: ");
87-
has = hasStudent(firstName, lastName);
88-
if (!has) {
89-
IO.print("That name isn't there, goofball!");
90-
} else {
91-
studentIndex = getStudentIndex(firstName, lastName);
92-
currentStudent = students.get(studentIndex);
93-
94-
IO.print("");
95-
System.out.println("The quiz scores for " + currentStudent.getFirstName()
96-
+ " " + currentStudent.getLastName() + " are:");
97-
Set<Integer> quizKeys = currentStudent.getQuizKeys();
98-
99-
for (int index : quizKeys) {
100-
System.out.println("Quiz " + index);
101-
IO.print(currentStudent.quizzes.get(index) + " ");
102-
}
103-
104-
IO.print("");
105-
}
64+
case 4: // View List of Student Quiz Scores
65+
classes.viewQuizScores();
10666
break;
10767

108-
case 5:
109-
firstName = IO.readString("Enter the first name: ");
110-
lastName = IO.readString("Enter the last name: ");
111-
has = hasStudent(firstName, lastName);
112-
if (!has) {
113-
IO.print("That name isn't there, goofball!");
114-
} else {
115-
studentIndex = getStudentIndex(firstName, lastName);
116-
currentStudent = students.get(studentIndex);
117-
118-
System.out.println("The average quiz score for " + currentStudent.getFirstName() + " " +
119-
currentStudent.getLastName() + " is: ");
120-
Set<Integer> quizKeys = currentStudent.getQuizKeys();
121-
122-
int numberOfQuizzes = 0;
123-
double sumOfQuizzes = 0;
124-
125-
for (int index : quizKeys) {
126-
numberOfQuizzes++;
127-
sumOfQuizzes += currentStudent.quizzes.get(index);
128-
}
129-
130-
System.out.println(sumOfQuizzes / numberOfQuizzes);
131-
}
68+
case 5: // View Average Quiz Scores for One Student
69+
classes.viewAveQuizScore();
70+
break;
71+
72+
case 6: // Save Data to File
73+
classes.saveData();
13274
break;
133-
13475
}
13576
}
13677
IO.print("Thanks for playing. See you again!");
78+
classes.saveData();
79+
IO.print("Your data have been saved automatically.");
80+
IO.print("You're welcome.");
13781
}
138-
139-
140-
public boolean hasStudent(String fN, String lN) {
141-
String firstName = fN, lastName = lN;
142-
143-
boolean has = false;
144-
145-
for (int i = 0; i < students.size(); i++) {
146-
if (students.get(i).getFirstName().equals(firstName) && students.get(i).getLastName().equals(lastName)) {
147-
has = true;
148-
}
149-
}
150-
151-
return has;
152-
}
153-
154-
public int getStudentIndex(String fN, String lN) {
155-
String firstName = fN, lastName = lN;
156-
157-
int index = 0;
158-
159-
for (int i = 0; i < students.size(); i++) {
160-
if (students.get(i).getFirstName().equals(firstName) && students.get(i).getLastName().equals(lastName)) {
161-
index = i;
162-
}
163-
}
164-
165-
return index;
166-
}
167-
16882
}
83+
84+
85+

SimpleFileIO/OutFile.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
this is a line in my file...
2+
this is a second line in my file...
3+
this is a third line in my file...

SimpleFileIO/nbactions.xml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>process-classes</goal>
10+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
11+
</goals>
12+
<properties>
13+
<exec.args>-classpath %classpath tsguild.simplefileio.SimpleFileIO</exec.args>
14+
<exec.executable>java</exec.executable>
15+
</properties>
16+
</action>
17+
</actions>

0 commit comments

Comments
 (0)