public Section(String class_id, String class_name, String section_no)
public class Course {
private String department;
private String catalog_no;
private String name;
public Course (String department, String catalog_no, String name) {
this.department = department;
this.catalog_no = catalog_no;
this.name = name;
}
public String toString() {
return department + " " + catalog_no + ": " + name;
}
public String department(){
return department;
}
public String catalog_no(){
return catalog_no;
}
public String name(){
return name;
}
}
public class CourseTest1 {
public static void main (String[] args){
Course1 course = new Course1("IT", "114", "Introduction to Java");
System.out.println(course);
System.out.println("Department: " + course.department());
System.out.println("Catalog number: " + course.catalog_no());
System.out.println("Course name: " + course.name());
}
}
$ java CourseTest1
IT 114: Introduction to Java
Department: IT
Catalog number: 114
Course name: Introduction to Java
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;
}
}
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");
}
}
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");
}
}
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;
}
}
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
public class Semester {
private String season;
private int year;
public Semester(String season, int year){
this.season = season;
this.year = year;
}
public String toString(){
return season + " " + Integer.toString(year);
}
public int year(){
return year;
}
public String season(){
return season;
}
}
public class SemesterTest1 {
public static void main (String[] args){
Semester s = new Semester("Spring", 2020);
System.out.println(s);
System.out.println("Season: " + s.season());
System.out.println("Year: " + s.year());
}
}
$ java SemesterTest1
Spring 2020
Season: Spring
Year: 2020
private boolean season_good(String season) throws IllegalArgumentException{
switch (season) {
case "Fall":
case "Spring":
case "Summer":
return true;
default:
throw new IllegalArgumentException(season + " is not a valid season");
}
}
private final int YEAR_MIN = 2020;
private final int YEAR_MAX = 2050;
...
private boolean year_good(int year) throws IllegalArgumentException {
if (year >= YEAR_MIN && year <= YEAR_MAX){
return true;
} else {
throw new IllegalArgumentException("year must be between " + YEAR_MIN + " and " + YEAR_MAX);
}
}
public Semester(String season, int year) throws IllegalArgumentException{
if (season_good(season) && year_good(year)) {
this.season = season;
this.year = year;
}
}
public class SemesterTest2 {
public static void main (String[] args){
try {
Semester s1 = new Semester("XXXX", 2020);
System.out.println(s1);
} catch (Exception e1) {
System.out.println(e1);
try {
Semester s2 = new Semester("Spring", 2000);
System.out.println(s2);
} catch (Exception e2) {
System.out.println(e2);
}
}
Semester s = new Semester("Spring", 2020);
System.out.println(s);
}
}
$ java SemesterTest2
java.lang.IllegalArgumentException: XXXX is not a valid season
java.lang.IllegalArgumentException: year must be between 2020 and 2050
Spring 2020
private static ArrayList<Course> courses = new ArrayList<>();
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");
}
}
}
private static void menu(){
String menu = "";
menu += "1 : Add Course\n";
menu += "q : Quit\n";
menu += "? ";
System.out.print(menu);
}
private static void add_course(Scanner console) throws FileNotFoundException {
Course c = Course.add_course(console);
System.out.println(c); //debug
courses.add(c);
}
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();
}
$ 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
private static final String store_filename = "coursework_store.txt";
private static final String separator = "-------------------------------------\n";
private static final String end_of_file = "=====================================";
while loop when the user quits
} else if (choice.equals("q")) {
store_state();
done = true;
public String store_string(){
String store_text = "course\n";
store_text += department + "\n";
store_text += catalog_no + "\n";
store_text += name + "\n";
return store_text;
}
private static void store_state() throws IOException{
String store_text = "";
for (int i = 0; i < courses.size(); i++){
Course c = courses.get(i);
store_text += c.store_string();
store_text += separator;
}
store_text += end_of_file;
FileWriter output = new FileWriter(store_filename);
output.write(store_text);
output.close();
}
$ 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 ? 1 Department: IT Catalog number: 244 Name: Introduction to Linux/Unix IT 244: Introduction to Linux/Unix 1 : Add Course q : Quit ? q $ cat coursework_store.txt course IT 114 Introduction to Java ------------------------------------- course IT 244 Introduction to Linux/Unix ------------------------------------- =====================================
while loop
private static void restore_state() throws FileNotFoundException{
File store_file = new File(store_filename);
if (store_file.exists()) {
Scanner input = new Scanner(store_file);
String line = input.nextLine();
while ( ! line.equals(end_of_file)){
if (line.equals("course")){
Course c = Course.restored_course(input);
System.out.println(c); // debug
courses.add(c);
}
line = input.nextLine();
}
}
}
public static Course restored_course(Scanner input) {
String department = input.nextLine();
String catalog_no = input.nextLine();
String name = input.nextLine();
input.nextLine(); // skip over the entry_separator
Course course = new Course(department, catalog_no, name);
return course;
}
while loop
public static void main (String[] args) throws FileNotFoundException, IOException{
restore_state();
Scanner console = new Scanner(System.in);
boolean done = false;
while (! done) {
....