Data Structure
META-INF/MANIFEST.MF
Manifest-Version: 1.0
students/Course.class
package students; public synchronized class Course { private String courseId; private double credits; private String grade; public void Course(); public String getCourseId(); public void setCourseId(String); public double getCredits(); public void setCredits(double); public String getGrade(); public void setGrade(String); public String toString(); public int hashCode(); public boolean equals(Object); }
students/Course.java
students/Course.java
package
students
;
import
java
.
util
.
Objects
;
public
class
Course
{
private
String
courseId
;
private
double
credits
;
private
String
grade
;
public
String
getCourseId
()
{
return
courseId
;
}
public
void
setCourseId
(
String
courseId
)
{
this
.
courseId
=
courseId
;
}
public
double
getCredits
()
{
return
credits
;
}
public
void
setCredits
(
double
credits
)
{
this
.
credits
=
credits
;
}
public
String
getGrade
()
{
return
grade
;
}
public
void
setGrade
(
String
grade
)
{
this
.
grade
=
grade
;
}
@
Override
public
String
toString
()
{
return
"Course{"
+
"courseId="
+
courseId
+
", credits="
+
credits
+
", grade="
+
grade
+
'}'
;
}
@
Override
public
int
hashCode
()
{
int
hash
=
7
;
hash
=
59
*
hash
+
Objects
.
hashCode
(
this
.
courseId
);
return
hash
;
}
@
Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
{
return
true
;
}
if
(
obj
==
null
)
{
return
false
;
}
if
(
getClass
()
!=
obj
.
getClass
())
{
return
false
;
}
final
Course
other
=
(
Course
)
obj
;
if
(
!
Objects
.
equals
(
this
.
courseId
,
other
.
courseId
))
{
return
false
;
}
return
true
;
}
}
students/Student.class
package students; public synchronized class Student { private String firstName; private String lastName; private String idNo; private java.util.ArrayList courses; private double totalCredits; private double gpa; public void Student(); public java.util.ArrayList getCourses(); public void setCourses(java.util.ArrayList); public String getFirstName(); public void setFirstName(String); public String getLastName(); public void setLastName(String); public String getIdNo(); public void setIdNo(String); public double getTotalCredits(); public void setTotalCredits(double); public double getGpa(); public void setGpa(double); public int hashCode(); public boolean equals(Object); public String toString(); public static String generateRndomId(java.util.Random, String, int); }
students/Student.java
students/Student.java
package
students
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Objects
;
import
java
.
util
.
Random
;
public
class
Student
{
private
String
firstName
;
private
String
lastName
;
private
String
idNo
;
private
ArrayList
<
Course
>
courses
;
private
double
totalCredits
;
private
double
gpa
;
public
ArrayList
<
Course
>
getCourses
()
{
return
courses
;
}
public
void
setCourses
(
ArrayList
<
Course
>
courses
)
{
this
.
courses
=
courses
;
}
public
String
getFirstName
()
{
return
firstName
;
}
public
void
setFirstName
(
String
firstName
)
{
this
.
firstName
=
firstName
;
}
public
String
getLastName
()
{
return
lastName
;
}
public
void
setLastName
(
String
lastName
)
{
this
.
lastName
=
lastName
;
}
public
String
getIdNo
()
{
//return idNo;
return
generateRndomId
(
new
Random
(),
"0123456789"
,
6
);
}
public
void
setIdNo
(
String
idNo
)
{
this
.
idNo
=
generateRndomId
(
new
Random
(),
"0123456789"
,
6
);
}
public
double
getTotalCredits
()
{
return
totalCredits
;
}
public
void
setTotalCredits
(
double
totalCredits
)
{
this
.
totalCredits
=
totalCredits
;
}
public
double
getGpa
()
{
return
gpa
;
}
public
void
setGpa
(
double
gpa
)
{
this
.
gpa
=
gpa
;
}
@
Override
public
int
hashCode
()
{
int
hash
=
7
;
hash
=
29
*
hash
+
Objects
.
hashCode
(
this
.
idNo
);
return
hash
;
}
@
Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
{
return
true
;
}
if
(
obj
==
null
)
{
return
false
;
}
if
(
getClass
()
!=
obj
.
getClass
())
{
return
false
;
}
final
Student
other
=
(
Student
)
obj
;
if
(
!
Objects
.
equals
(
this
.
idNo
,
other
.
idNo
))
{
return
false
;
}
return
true
;
}
/* @Override
public String toString() {
return "Student{" + "firstName=" + firstName + ", lastName=" + lastName + ", idNo=" + idNo + ", totalCredits=" + totalCredits + ", gpa=" + gpa + '}';
}
*/
@
Override
public
String
toString
()
{
return
"Student{"
+
"firstName="
+
firstName
+
", lastName="
+
lastName
+
", idNo="
+
idNo
+
", courses="
+
courses
+
", totalCredits="
+
totalCredits
+
", gpa="
+
gpa
+
'}'
;
}
//generate random id
public
static
String
generateRndomId
(
Random
rng
,
String
characters
,
int
length
)
{
char
[]
text
=
new
char
[
length
];
for
(
int
i
=
0
;
i
<
length
;
i
++
)
{
text
[
i
]
=
characters
.
charAt
(
rng
.
nextInt
(
characters
.
length
()));
}
return
new
String
(
text
);
}
}
students/Driver.class
package students; public synchronized class Driver { private static java.util.ArrayList student; public void Driver(); public static java.util.ArrayList loaddata(); public static Student crunchifyCSVtoArrayList(String); public static java.util.ArrayList addStudent(String, String, java.util.ArrayList); public static void display(java.util.ArrayList); public static void storeData(); public static void main(String[]) throws java.io.IOException; }
students/Driver.java
students/Driver.java
package
students
;
import
java
.
io
.
BufferedReader
;
import
java
.
io
.
FileReader
;
import
java
.
io
.
FileWriter
;
import
java
.
io
.
IOException
;
import
java
.
util
.
ArrayList
;
public
class
Driver
{
private
static
ArrayList
<
Student
>
student
;
public
static
ArrayList
<
Student
>
loaddata
(){
BufferedReader
file
=
null
;
ArrayList
<
Student
>
students
=
new
ArrayList
<>
();
try
{
String
crunchifyLine
;
file
=
new
BufferedReader
(
new
FileReader
(
"students.txt"
));
boolean
isfirst
=
false
;
while
((
crunchifyLine
=
file
.
readLine
())
!=
null
)
{
if
(
isfirst
){
students
.
add
(
crunchifyCSVtoArrayList
(
crunchifyLine
));
}
isfirst
=
true
;
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
if
(
file
!=
null
)
file
.
close
();
}
catch
(
IOException
crunchifyException
)
{
crunchifyException
.
printStackTrace
();
}
}
return
students
;
}
public
static
Student
crunchifyCSVtoArrayList
(
String
crunchifyCSV
)
{
ArrayList
<
String
>
crunchifyResult
=
new
ArrayList
<
String
>
();
Student
s
=
new
Student
();
student
=
new
ArrayList
<>
();
ArrayList
<
Course
>
courses
=
new
ArrayList
<>
();
Course
c
=
new
Course
();
if
(
crunchifyCSV
!=
null
)
{
String
[]
splitData
=
crunchifyCSV
.
split
(
"\\s*,\\s*"
);
int
counter
=
0
;
int
coursesplitor
=
0
;
for
(
int
i
=
0
;
i
<
splitData
.
length
;
i
++
)
{
String
element
=
""
;
if
(
!
(
splitData
[
i
]
==
null
)
||
!
(
splitData
[
i
].
length
()
==
0
))
{
element
=
splitData
[
i
].
trim
();
}
if
(
counter
<
3
){
crunchifyResult
.
add
(
element
);
if
(
counter
==
0
){
s
.
setLastName
(
element
);
}
if
(
counter
==
1
){
s
.
setFirstName
(
element
);
}
if
(
counter
==
2
){
s
.
setIdNo
(
element
);
}
}
else
{
if
(
element
.
equalsIgnoreCase
(
"-999"
)){
break
;
}
if
(
coursesplitor
<
3
){
if
(
coursesplitor
==
0
){
c
.
setCourseId
(
element
);
}
if
(
coursesplitor
==
1
){
c
.
setCredits
(
Double
.
parseDouble
(
element
));
}
if
(
coursesplitor
==
2
){
c
.
setGrade
(
element
);
courses
.
add
(
c
);
c
=
new
Course
();
coursesplitor
=
-
1
;
}
}
else
{
coursesplitor
=
0
;
}
}
if
(
counter
>=
3
){
coursesplitor
++
;
}
counter
++
;
}
}
s
.
setCourses
(
courses
);
s
.
setTotalCredits
(
utils
.
calculateCredits
(
s
));
s
.
setGpa
(
utils
.
calculateGpa
(
s
));
return
s
;
}
public
static
ArrayList
<
Student
>
addStudent
(
String
lastName
,
String
firstName
,
ArrayList
<
Student
>
students
){
Student
student
=
new
Student
();
student
.
setFirstName
(
firstName
);
student
.
setLastName
(
lastName
);
students
.
add
(
student
);
return
students
;
}
public
static
void
display
(
ArrayList
<
Student
>
students
){
for
(
Student
s
:
students
){
System
.
out
.
println
(
s
);
}
}
public
static
void
storeData
(){
}
public
static
void
main
(
String
args
[])
throws
IOException
{
ArrayList
<
Student
>
students
=
loaddata
();
display
(
students
);
students
=
new
ArrayList
<>
();
}
}
students/utils.class
package students; public synchronized class utils { private static java.util.HashMap gradepoints; static void <clinit>(); public void utils(); public static void loadGradepoints(); public static double calculateCredits(Student); public static double calculateGpa(Student); public static void writeLine(java.io.Writer, java.util.List) throws java.io.IOException; public static void writeLine(java.io.Writer, java.util.List, char, char) throws java.io.IOException; private static String followCVSformat(String); public static void writetoOuputFile(java.util.ArrayList) throws java.io.IOException; }
students/utils.java
students/utils.java
package
students
;
import
java
.
io
.
FileWriter
;
import
java
.
io
.
IOException
;
import
java
.
io
.
Writer
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
List
;
public
class
utils
{
private
static
HashMap
<
String
,
String
>
gradepoints
=
new
HashMap
<>
();
public
static
void
loadGradepoints
(){
gradepoints
.
put
(
"A+"
,
"4"
);
gradepoints
.
put
(
"A"
,
"4"
);
gradepoints
.
put
(
"A-"
,
"3.7"
);
gradepoints
.
put
(
"B+"
,
"3.3"
);
gradepoints
.
put
(
"B"
,
"3"
);
gradepoints
.
put
(
"B-"
,
"2.7"
);
gradepoints
.
put
(
"C+"
,
"2.3"
);
gradepoints
.
put
(
"C"
,
"2"
);
gradepoints
.
put
(
"C-"
,
"1.7"
);
gradepoints
.
put
(
"D+"
,
"1.3"
);
gradepoints
.
put
(
"D"
,
"1.0"
);
gradepoints
.
put
(
"D-"
,
"0.7"
);
gradepoints
.
put
(
"F"
,
"0.0"
);
}
public
static
double
calculateCredits
(
Student
s
){
double
creditstotal
=
0.0
;
ArrayList
<
Course
>
courses
=
s
.
getCourses
();
loadGradepoints
();
for
(
Course
c
:
courses
){
creditstotal
+=
c
.
getCredits
();
}
return
creditstotal
;
}
public
static
double
calculateGpa
(
Student
s
){
double
gpa
=
0.0
;
double
totalcredits
=
calculateCredits
(
s
);
double
totalgradepoints
=
0.0
;
ArrayList
<
Course
>
courses
=
s
.
getCourses
();
for
(
Course
c
:
courses
){
String
key
=
c
.
getGrade
();
String
value
=
gradepoints
.
get
(
key
);
double
dbValue
=
Double
.
parseDouble
(
value
);
totalgradepoints
+=
dbValue
*
c
.
getCredits
();
}
gpa
=
totalgradepoints
/
totalcredits
;
return
gpa
;
}
public
static
void
writeLine
(
Writer
w
,
List
<
String
>
values
)
throws
IOException
{
writeLine
(
w
,
values
,
','
,
' '
);
}
public
static
void
writeLine
(
Writer
w
,
List
<
String
>
values
,
char
separators
,
char
customQuote
)
throws
IOException
{
boolean
first
=
true
;
if
(
separators
==
' '
)
{
separators
=
','
;
}
StringBuilder
sb
=
new
StringBuilder
();
for
(
String
value
:
values
)
{
if
(
!
first
)
{
sb
.
append
(
separators
);
}
if
(
customQuote
==
' '
)
{
sb
.
append
(
followCVSformat
(
value
));
}
else
{
sb
.
append
(
customQuote
).
append
(
followCVSformat
(
value
)).
append
(
customQuote
);
}
first
=
false
;
}
sb
.
append
(
"\r\n"
);
w
.
append
(
sb
.
toString
());
}
private
static
String
followCVSformat
(
String
value
)
{
String
result
=
value
;
if
(
result
.
contains
(
"\""
))
{
result
=
result
.
replace
(
"\""
,
"\"\""
);
}
return
result
;
}
public
static
void
writetoOuputFile
(
ArrayList
<
Student
>
students
)
throws
IOException
{
ArrayList
<
String
>
stringArray
=
new
ArrayList
<>
();
String
csvFile
=
"studentresults.txt"
;
FileWriter
writer
=
new
FileWriter
(
csvFile
);
for
(
Student
s
:
students
){
String
firstName
=
s
.
getFirstName
();
String
lastName
=
s
.
getLastName
();
String
idNo
=
s
.
getIdNo
();
ArrayList
<
Course
>
courses
=
s
.
getCourses
();
double
totalcredits
=
s
.
getTotalCredits
();
double
gpa
=
s
.
getGpa
();
stringArray
.
add
(
firstName
);
stringArray
.
add
(
lastName
);
stringArray
.
add
(
idNo
);
for
(
Course
course
:
courses
){
String
courseid
=
course
.
getCourseId
();
String
credits
=
Double
.
toString
(
course
.
getCredits
());
String
grade
=
course
.
getGrade
();
stringArray
.
add
(
courseid
);
stringArray
.
add
(
credits
);
stringArray
.
add
(
grade
);
}
stringArray
.
add
(
Double
.
toString
(
totalcredits
));
stringArray
.
add
(
Double
.
toString
(
gpa
));
utils
.
writeLine
(
writer
,
stringArray
);
writer
.
flush
();
stringArray
=
new
ArrayList
<>
();
}
writer
.
close
();
}
}
students/students.txt
Jones, Mary, 903452, COS 4342, 2.5, A, COS 3311, 4, B+, -999, 0, 0 Martin,Joseph,312345,4598,3,C,1122,3,A-,2467,4,A,-999,0,0