a

parent 74252292
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Copy of inf101.Lab4</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
This diff is collapsed.
package inf101.Lab4;
/**
* An exception that is thrown when an error occurs
* while parsing a CSV file.
*
* @author eivind
*/
public class CSVParseException extends Exception {
private static final long serialVersionUID = 8028319771516912890L;
/**
* Construct the exception with a message explaining
* the error that occurred.
* @param msg
*/
public CSVParseException(String msg){
super(msg);
}
}
package inf101.Lab4;
public class DateTime {
//TODO
}
/**
*
*/
package inf101.Lab4;
/**
*
* A Lecture belongs to one subject and has a startTime
* and an endTime.
*
* @author eivind
*
*/
public class Lecture {
private Subject subject;
private DateTime startTime;
private DateTime endTime;
public Subject getSubject() {
return subject;
}
public DateTime getStartTime() {
return startTime;
}
public DateTime getEndTime() {
return endTime;
}
/**
* Construct a Lecture belonging to the given subject and
* having the given start and end times.
*
* @param subject
* @param startTime
* @param endTime
*/
public Lecture(Subject subject, DateTime startTime, DateTime endTime) {
this.subject = subject;
this.startTime = startTime;
this.endTime = endTime;
}
/**
*
* Parse a Lecture from a line a csv file. The format of the lecture
* is as follows:
*
* <pre>
* [Subject Code]; YYYY-MM-DD; [Starttime]; [Endtime]; [course credits]; [course level]; [Norwegian Title]; [English Title]
* </pre>
*
* Starttime and Endtime is formatted as follows:
* <pre>
* HHMM
* </pre>
*
* @see Subject#parseLevel
* @param str
* @return The lecture parsed
* @throws CSVParseException
*/
public static Lecture parseLecture(String str) throws CSVParseException{
//TODO
return null;
}
//TODO Mangler toString
}
package inf101.Lab4;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
*
* A simple application that reads the file with the name FILENAME and
* looks for all lectures with the code LOOKFORSUBJECTCODE.
*
* @author eivind
*
*/
public class ReadCSV {
public static final String LOOK_FOR_SUBJECT_CODE = "ITAL111";
public static final String FILENAME = "UiB_subjectSchedules_2015v.txt";
public static void main(String[] args) {
ArrayList<Lecture> lectures = parseLectures(FILENAME);
for (Lecture l : lectures) {
if (l.getSubject().getCode().equals(LOOK_FOR_SUBJECT_CODE)) {
System.out.println(l);
}
}
}
/**
*
* Open a the file with the given filename, and attempt
* to read it as a csv file containing lectures in the
* format specified by {@link Lecture#parseLecture}.
*
* The first line is assumed to contain column headers, and is
* therefore skipped.
*
*/
private static ArrayList<Lecture> parseLectures(String filename) {
ArrayList<Lecture> lectures = new ArrayList<Lecture>();
try (FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr)) {
br.readLine(); //Skip the line containing the column headers
String line = br.readLine();
while (line != null) {
lectures.add(Lecture.parseLecture(line));
line = br.readLine();
}
} catch (FileNotFoundException e) {
System.out.println("Could not find file!");
} catch (IOException e) {
System.out.println("Could not read file!");
} catch (CSVParseException e) {
System.out.println("Error occured while parsing csv file!");
e.printStackTrace();
}
return lectures;
}
}
package inf101.Lab4;
/**
* Class representing a subject.
*
* @author eivind
*
*/
public class Subject {
public enum Level {
UNDERGRADUATE, POSTGRADUATE, PHDKURS, AARSSTUDIUM, EVU
}
public String getCode() {
return code;
}
private String code;
private String englishTitle;
private String norwegianTitle;
private Level level;
private double studiepoeng;
/**
* @param code
* The subject code, e.g. "INF101".
* @param englishTitle
* The english title for the subject.
* @param norwegianTitle
* The norwegian title for the subject.
* @param level
* The level of the subject.
* @param studypoints
* The number of studypoint awarded upon completion of the
* course.
*/
public Subject(String code, String englishTitle, String norwegianTitle,
Level level, double studypoints) {
this.code = code;
this.englishTitle = englishTitle;
this.norwegianTitle = norwegianTitle;
this.level = level;
this.studiepoeng = studypoints;
}
/**
* Parse a string containing a course level, that is, one of the following:
* "undergraduate", "postgraduate", "evu", "arsstudium", or "phdkurs".
*
* @param string
* The level to parse.
* @return The level represented by the string, if any.
* @throws CSVParseException
* Thrown if the string does not match any of the above.
*/
public static Level parseLevel(String string) throws CSVParseException {
//TODO
return null;
}
}
package inf101.Lab4;
import static org.junit.Assert.*;
import org.junit.Test;
public class SubjectTest {
/**
* Tests that parsing the string "phdkurs" as a level results
* in Level.PHDKURS.
*/
@Test
public void testParsePhdKurs() {
try {
assertTrue(Subject.parseLevel("phdkurs") == Subject.Level.PHDKURS);
} catch (CSVParseException e) {
assertTrue(false);
}
}
/**
* Tests that attempting to parse "notASubject" as
* a level results in a CSVParseException.
*/
@Test
public void testParseNotASubject() {
try {
Subject.parseLevel("notASubject");
assertTrue(false);
} catch (CSVParseException e) {
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment