- To do this we need a validation method for each attribute value
- Though this application might eventually be used by other departments
- At this point in time we will limit it to IT
- And to courses taught by the CS department
- First we check department
private boolean department_good(String value) throws IllegalArgumentException{
boolean value_good = value.equals("IT");
if (! value_good ){
throw new IllegalArgumentException("Department must be IT");
} else {
return value_good;
}
}
- Then catalog_no
private boolean catalog_no_good(String catalog_no) throws IllegalArgumentException{
switch (catalog_no) {
case "114":
case "116":
case "117":
case "244":
case "246":
case "285":
case "341":
case "442":
case "443":
case "444":
case "485":
return true;
default:
throw new IllegalArgumentException(catalog_no + " is not valid");
}
}
- And finally name
private final int MIN_NAME_LEN = 10;
private final int MAX_NAME_LEN = 100;
...
private boolean name_good(String name){
if (name.length() >= MIN_NAME_LEN && name.length() <= MAX_NAME_LEN) {
return true;
} else {
throw new IllegalArgumentException("name must be between " + MIN_NAME_LEN +
" and " + MAX_NAME_LEN + " characters long");
}
}
- Of course we need to update the constructor to use these methods
public Course (String department, String catalog_no, String name)
throws IllegalArgumentException{
if (department_good(department) && catalog_no_good(catalog_no) &&
name_good(name)){
this.department = department;
this.catalog_no = catalog_no;
this.name = name;
}
}
- Now we test
public class CourseTest2 {
public static void main (String[] args){
try {
Course course1 = new Course("XXX", "114", "Introduction to Java");
System.out.println(course1);
} catch (Exception e1 ){
System.out.println(e1);
try {
Course course2 = new Course("IT", "XXXXX", "Introduction to Java");
System.out.println(course2);
} catch (Exception e2) {
System.out.println(e2);
try {
Course course3 = new Course("IT", "114", "");
System.out.println(course3);
} catch (Exception e3){
System.out.println(e3);
}
}
}
Course course = new Course("IT", "114", "Introduction to Java");
System.out.println(course);
}
}
$ java CourseTest2
java.lang.IllegalArgumentException: Department must be IT
java.lang.IllegalArgumentException: XXXXX is not valid
java.lang.IllegalArgumentException: name must be between 10 and 100 characters long
IT 114: Introduction to Java
- CourseWork will have a list of
Course objects
- One for every course the instructor teaches
- It will keep these in an ArrayList
private static ArrayList<Course> courses = new ArrayList<>();
- The main method of CourseWork
contains a
while
loop that interacts with the user
public static void main (String[] args) throws FileNotFoundException{
Scanner console = new Scanner(System.in);
boolean done = false;
while (! done) {
menu();
String choice = console.nextLine();
choice = choice.trim();
if (choice.equals("1")){
add_course(console);
} else if (choice.equals("q")) {
done = true;
} else {
System.out.println(choice + " is not a valid entry");
}
}
}
- The private method menu prints a list of actions
private static void menu(){
String menu = "";
menu += "1 : Add Course\n";
menu += "q : Quit\n";
menu += "? ";
System.out.print(menu);
}
- add_course will not add the course itself
- Why?
- Because creating that would mean creating a Course
object
- And that is a job for the Course class
- Instead it will call an add_course method
in Course to create the object
- And then store it in the courses list
private static void add_course(Scanner console) throws FileNotFoundException {
Course c = Course.add_course(console);
System.out.println(c); //debug
courses.add(c);
}
- The add_course method in Course
gets a Scanner object and uses this to
get the values it needs with the help of get_field_value
- It then creates the Course object and returns it
public static Course add_course(Scanner console) throws FileNotFoundException{
String department = get_field_value(console, "Department");
String course_no = get_field_value(console, "Catalog number");
String name = get_field_value(console, "Name");
Course course = new Course(department, course_no, name);
return course;
}
private static String get_field_value(Scanner console, String field_name)
throws FileNotFoundException {
System.out.print(field_name + ": ");
String field_value = console.nextLine();
return field_value.trim();
}
- Now we can run CourseWork to add a Course
$ java CourseWork
1 : Add Course
q : Quit
? 1
Department: IT
Catalog number: 114
Name: Introduction to Java
IT 114: Introduction to Java
1 : Add Course
q : Quit
? q