In: Computer Science
Java problem
Implement class Course containing: String name, enumeration Type (Foundamental, Specialization, Discipline), enumeration Stream (English, French, German), int creditPoints.
Class Contract has an array of courses with methods addCourse(Course), deleteCourse(type, stream, name) sort(), display().
Courses are sorted by stream, type, name.
If 2 courses are equal raise a custom exception in method sort().
Make Contract implement Storable.
//Course.java
package test2;
public class Course {
private String name;
private enum Type {
Foundamental, Specialization,
Discipline;
}
private Type type;
private enum Stream {
English, French, German;
}
private Stream stream;
private int creditPoints;
public Course(){
this.name = "Not set";
this.type = Type.Discipline;
this.stream = Stream.English;
this.creditPoints = 0;
}
public Course(String name, String type, String stream,
int creditPoints){
this.name = name;
this.type =
Type.valueOf(type);
this.stream =
Stream.valueOf(stream);
this.creditPoints =
creditPoints;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type.name();
}
public void setType(String type) {
this.type =
Type.valueOf(type);
}
public String getStream() {
return stream.name();
}
public void setStream(String stream)
{
this.stream =
Stream.valueOf(stream);
}
public int getCreditPoints() {
return creditPoints;
}
public void setCreditPoints(int
creditPoints) {
this.creditPoints =
creditPoints;
}
public String toString(){
return "Name: " + this.name +
"\n"
+ "Type: " + this.type.name() + "\n"
+ "Stream: " + this.stream.name() + "\n"
+ "Credit Points: " + this.creditPoints +
"\n";
}
}
//test.java
package test2;
public class test {
public static void main(String[] args) {
Course c1 = new
Course("b","Discipline","English",5);
Course c2 = new
Course("a","Discipline","German",5);
Course c3 = new
Course("d","Foundamental","French",5);
Course c4 = new
Course("c","Discipline","English",5);
Course c6 = new
Course("f","Specialization","French",5);
Contract obj = new
Contract();
obj.add(c1);
obj.add(c2);
obj.add(c3);
obj.add(c4);
obj.add(c6);
obj.display();
obj.sortByName();
System.out.println("\nAfter sorting
by Name\n");
obj.display();
obj.sortByType();
System.out.println("\nAfter sorting
by Type\n");
obj.display();
obj.sortByStream();
System.out.println("\nAfter sorting
by Stream\n");
obj.display();
Course c5 = new
Course("c","Discipline","English",5);
obj.add(c5);
System.out.println("\nDuplicate
value exception\n");
obj.sortByName();
}
}