It's simple. Person is a superclass. Student and Instructor are its subclasses. The program suppose to allow user create a student or an instructor.
Run
package com.exercise.inheritance1;
import java.util.Scanner;
import org.joda.time.LocalDate;
public class Run {
public static void main(String args[])
{
Boolean done = false;
do
{
System.out.println("Select one of the following:");
System.out.println("\t1 -- Create Student");
System.out.println("\t2 -- Create Instructor");
System.out.println("Enter your selection (0 to exit): ");
Scanner scan = new Scanner(System.in);
String userInput = scan.nextLine();
int selection;
String name;
LocalDate dateOfBirth;
String major;
Double salary;
try
{
selection = Integer.parseInt(userInput);
}
catch(Exception ex)
{
System.out.println("What?");
System.out.println(ex.getMessage());
continue;
}
System.out.println("You selected " + selection);
switch(selection)
{
case 0:
done = true;
break;
case 1:
//Create Student
System.out.println("Name:");
name = scan.nextLine();
System.out.println("Birthdate(YYYY-MM-DD):");
try
{
dateOfBirth = LocalDate.parse(scan.nextLine());
}
catch(IllegalArgumentException ex)
{
System.out.println(String.format("Ooops! Message: \n%s", ex.getMessage()));
continue;
}
System.out.println("Major:");
major = scan.nextLine();
Student stu = new Student(name, dateOfBirth, major);
System.out.println(stu.toString());
break;
case 2:
//Create Instructor
System.out.println("Name:");
name = scan.nextLine();
System.out.println("Birthdate(YYYY-MM-DD):");
try
{
dateOfBirth = LocalDate.parse(scan.nextLine());
}
catch(IllegalArgumentException ex)
{
System.out.println(String.format("Ooops! Message: \n%s", ex.getMessage()));
continue;
}
System.out.println("Salary:");
try
{
salary = Double.parseDouble(scan.nextLine());
}
catch(IllegalArgumentException ex)
{
System.out.println(String.format("Ooops! Message: \n%s", ex.getMessage()));
continue;
}
Instructor ins = new Instructor(name, dateOfBirth, salary);
System.out.println(ins.toString());
break;
default:
System.out.println(String.format("You have selected an invalid number %s", selection));
continue;
}
}while(!done);
System.out.println("See ya!");
}
}
Person
package com.exercise.inheritance1;
import org.joda.time.LocalDate;
public class Person {
protected String name;
protected LocalDate dateOfBirth;
public Person(String name, LocalDate dateOfBirth)
{
setName(name);
setDateOfBirth(dateOfBirth);
}
private void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
private void setDateOfBirth(LocalDate dateOfBirth)
{
this.dateOfBirth = dateOfBirth;
}
public LocalDate getDateOfBirth()
{
return this.dateOfBirth;
}
@Override
public String toString()
{
return String.format("%s: %s\n%s: %s",
"Name", this.getName(), "Date of Birth", this.getDateOfBirth());
}
}
Student
package com.exercise.inheritance1;
import org.joda.time.LocalDate;
public class Student extends Person {
private String major;
public Student(String name, LocalDate dateOfBirth, String major)
{
super(name, dateOfBirth);
setMajor(major);
}
private void setMajor(String major)
{
this.major = major;
}
public String getMajor()
{
return this.major;
}
@Override
public String toString()
{
return String.format("%s\n%s: %s",
super.toString(), "Major", this.getMajor());
}
}
Instructor
package com.exercise.inheritance1;
import org.joda.time.LocalDate;
public class Instructor extends Person {
private double salary;
public Instructor(String name, LocalDate dateOfBirth, double salary)
{
super(name, dateOfBirth);
setSalary(salary);
}
private void setSalary(double salary)
{
this.salary = salary;
}
public double getSalary()
{
return this.salary;
}
@Override
public String toString()
{
return String.format("%s\n%s: %s",
super.toString(), "Salary", this.getSalary());
}
}