Added some practical examples for the challenge.
- Author
- Vngngdn
- Date
- Aug. 15, 2016, 8:39 p.m.
- Hash
- 999d2840ac109864124de477ccb1d59508473c75
- Parent
- eae4a9bb2239f9ac5c566486c08f647ce6275f21
- Modified files
- Challenge 3/Course.java
- Challenge 3/CourseFactory.java
- Challenge 3/CourseItem.java
- Challenge 3/Main.class
- Challenge 3/Main.java
- Challenge 3/Window.java
- Challenge 3/main.java
- Challenge 3/practice.txt
- Challenge 3/reader.java
Challenge 3/Course.java ¶
69 additions and 0 deletions.
View changes Hide changes
+ |
1 |
|
+ |
2 |
private String name; |
+ |
3 |
private int ID; |
+ |
4 |
private int studyPoints; |
+ |
5 |
private boolean mandatory; |
+ |
6 |
private int semester; |
+ |
7 |
private int year; |
+ |
8 |
private int[] requiredCredits; |
+ |
9 |
|
+ |
10 |
public Course(String name, int ID, int studyPoints, boolean mandatory, int semester, int year, int[] requiredCredits) { |
+ |
11 |
this.name = name; |
+ |
12 |
this.ID = ID; |
+ |
13 |
this.studyPoints = studyPoints; |
+ |
14 |
this.mandatory = mandatory; |
+ |
15 |
this.semester = semester; |
+ |
16 |
this.year = year; |
+ |
17 |
this.requiredCredits = requiredCredits; |
+ |
18 |
} |
+ |
19 |
|
+ |
20 |
public void setName(String name) { |
+ |
21 |
this.name = name; |
+ |
22 |
} |
+ |
23 |
|
+ |
24 |
public String getName() { |
+ |
25 |
return name; |
+ |
26 |
} |
+ |
27 |
|
+ |
28 |
public void setID(int ID) { |
+ |
29 |
this.ID = ID; |
+ |
30 |
} |
+ |
31 |
|
+ |
32 |
public int getID() { |
+ |
33 |
return ID; |
+ |
34 |
} |
+ |
35 |
|
+ |
36 |
public void setStudyPoints(int studyPoints) { |
+ |
37 |
this.studyPoints = studyPoints; |
+ |
38 |
} |
+ |
39 |
|
+ |
40 |
public int getStudyPoints() { |
+ |
41 |
return studyPoints; |
+ |
42 |
} |
+ |
43 |
|
+ |
44 |
public void setMandatory(boolean mandatory) { |
+ |
45 |
this.mandatory = mandatory; |
+ |
46 |
} |
+ |
47 |
|
+ |
48 |
public boolean getMandatory() { |
+ |
49 |
return mandatory; |
+ |
50 |
} |
+ |
51 |
|
+ |
52 |
public void setSemester(int semester) { |
+ |
53 |
this.semester = semester; |
+ |
54 |
} |
+ |
55 |
|
+ |
56 |
public int getSemester() { |
+ |
57 |
return semester; |
+ |
58 |
} |
+ |
59 |
|
+ |
60 |
public void setYear(int year) { |
+ |
61 |
this.year = year; |
+ |
62 |
} |
+ |
63 |
|
+ |
64 |
public int getYear() { |
+ |
65 |
return year; |
+ |
66 |
} |
+ |
67 |
|
+ |
68 |
} |
+ |
69 |
Challenge 3/CourseFactory.java ¶
122 additions and 0 deletions.
View changes Hide changes
+ |
1 |
|
+ |
2 |
private List<String> data; |
+ |
3 |
|
+ |
4 |
public CourseFactory(List<String> data) { |
+ |
5 |
this.data = data; |
+ |
6 |
} |
+ |
7 |
|
+ |
8 |
private CourseItem detectDataType(String line) { |
+ |
9 |
Map<String, CourseItem> dataTypes = new HashMap(); |
+ |
10 |
dataTypes.put("ID", CourseItem.ID); |
+ |
11 |
dataTypes.put("SP", CourseItem.STUDY_POINTS); |
+ |
12 |
dataTypes.put("Type", CourseItem.MANDATORY); |
+ |
13 |
dataTypes.put("Jaar", CourseItem.YEAR); |
+ |
14 |
dataTypes.put("Semester", CourseItem.SEMESTER); |
+ |
15 |
dataTypes.put("Vereiste", CourseItem.REQUIRED); |
+ |
16 |
dataTypes.put("", CourseItem.DELIMITER); |
+ |
17 |
|
+ |
18 |
String dataType = line.split(" ")[0]; // bv. "ID = 43" --> "ID" |
+ |
19 |
if(!dataTypes.containsKey(dataType)) { |
+ |
20 |
return CourseItem.TITLE; |
+ |
21 |
} |
+ |
22 |
else { |
+ |
23 |
return dataTypes.get(dataType); |
+ |
24 |
} |
+ |
25 |
} |
+ |
26 |
|
+ |
27 |
public Course[] generateCourses() { |
+ |
28 |
int coursesCount = 1; |
+ |
29 |
for(String line: this.getData()) { |
+ |
30 |
if(line.equals("")) { // An empty line denotes the end of a course |
+ |
31 |
coursesCount++; |
+ |
32 |
} |
+ |
33 |
} |
+ |
34 |
|
+ |
35 |
Course[] courses = new Course[coursesCount]; |
+ |
36 |
Iterator<String> courseIterator = this.getData().iterator(); |
+ |
37 |
int courseIndex = 0; |
+ |
38 |
while(courseIterator.hasNext()) { |
+ |
39 |
String title = ""; |
+ |
40 |
int ID = -1; |
+ |
41 |
int SP = -1; |
+ |
42 |
boolean mandatory = false; |
+ |
43 |
int year = -1; |
+ |
44 |
int semester = -1; |
+ |
45 |
int[] requiredCredits; |
+ |
46 |
String line = courseIterator.next(); |
+ |
47 |
CourseItem lineType = this.detectDataType(line); |
+ |
48 |
|
+ |
49 |
switch(lineType) { |
+ |
50 |
case CourseItem.DELIMITER: |
+ |
51 |
Course newCourse = new Course( |
+ |
52 |
title, |
+ |
53 |
ID, |
+ |
54 |
SP, |
+ |
55 |
mandatory, |
+ |
56 |
year, |
+ |
57 |
semester, |
+ |
58 |
requiredCredits); |
+ |
59 |
courses[courseIndex] = newCourse; |
+ |
60 |
courseIndex++; |
+ |
61 |
break; |
+ |
62 |
case CourseItem.ID: |
+ |
63 |
ID = Integer.parseInt(line.split(" ")[2]); |
+ |
64 |
break; |
+ |
65 |
case CourseItem.STUDY_POINTS: |
+ |
66 |
SP = Integer.parseInt(line.split(" ")[2]); |
+ |
67 |
break; |
+ |
68 |
case CourseItem.YEAR: |
+ |
69 |
year = Integer.parseInt(line.split(" ")[2]); |
+ |
70 |
break; |
+ |
71 |
case CourseItem.SEMESTER: |
+ |
72 |
semester = Integer.parseInt(line.split(" ")[2]); |
+ |
73 |
break; |
+ |
74 |
case CourseItem.MANDATORY: |
+ |
75 |
if(line.split(" ")[2].equals("verplicht")) { |
+ |
76 |
mandatory = true; |
+ |
77 |
} |
+ |
78 |
break; |
+ |
79 |
case CourseItem.TITLE: |
+ |
80 |
title = line; |
+ |
81 |
break; |
+ |
82 |
case CourseItem.REQUIRED: |
+ |
83 |
for(String lineSnippet: line.split(" ")) { |
+ |
84 |
try { |
+ |
85 |
int[] newRequiredCourses = new int[requiredCredits.length + 1]; |
+ |
86 |
for(int i=0; i<requiredCredits.length; i++) { |
+ |
87 |
newRequiredCourses[i] = requiredCredits[i]; |
+ |
88 |
} |
+ |
89 |
newRequiredCourses[newRequiredCourses.length-1] = Integer.parseInt(lineSnippet); |
+ |
90 |
requiredCredits = newRequiredCourses; |
+ |
91 |
} |
+ |
92 |
catch(NumberFormatException e) { |
+ |
93 |
// This is when it's not an integer. safe to ignore. |
+ |
94 |
} |
+ |
95 |
} |
+ |
96 |
break; |
+ |
97 |
} |
+ |
98 |
|
+ |
99 |
|
+ |
100 |
|
+ |
101 |
|
+ |
102 |
|
+ |
103 |
|
+ |
104 |
|
+ |
105 |
|
+ |
106 |
|
+ |
107 |
courseIndex++; |
+ |
108 |
} |
+ |
109 |
|
+ |
110 |
|
+ |
111 |
|
+ |
112 |
public void setData(List<String> data) { |
+ |
113 |
this.data = data; |
+ |
114 |
} |
+ |
115 |
|
+ |
116 |
public List<String> getData() { |
+ |
117 |
return data; |
+ |
118 |
} |
+ |
119 |
|
+ |
120 |
} |
+ |
121 |
|
+ |
122 |
Challenge 3/CourseItem.java ¶
10 additions and 0 deletions.
Challenge 3/Main.class ¶
0 additions and 0 deletions.