import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Optional;
class IdException extends Exception
{
public IdException(String exMsg)
{
super(exMsg);
}
}
abstract class Person
{
private String name;
private String ID;
// Person Constructor #1
public Person() throws IdException
{
printEnterInfo();
Scanner scanIn = new Scanner(System.in);
String inputLine = scanIn.nextLine();
while (NumericChecker.isDouble(inputLine) || inputLine.isBlank())
{
Project3.printLines();
System.out.println("Invalid entry- Please try again.");
Project3.printLines();
System.out.printf("%20s", "Full name: ");
inputLine = scanIn.nextLine();
}
setName(inputLine);
System.out.printf("%20s", "ID: ");
String id = scanIn.nextLine().toLowerCase();
if (!id.matches("([a-zA-Z]){2}\\d{4}"))
{
throw new IdException("Sorry Invalid id format-It has to be LetterLetterDigitDigitDigitDigit.");
}
setID(id);
}
// Person Constructor #2
public Person(String name, String ID)
{
this.name = name;
this.ID = ID;
}
abstract void printEnterInfo();
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
public String getID()
{
return this.ID;
}
public void setID(String ID)
{
this.ID = ID;
}
abstract void printInformation();
}
public class Project3
{
static List<Person> persons = new ArrayList<Person>();
public static void main(String [] args)
{
System.out.printf("%n%nWelcome to my Personal Management Program%n%n");
selectChoice();
System.out.format("%nGoodbye!%n%n");
}
// Switch to select main menu choice
public static void selectChoice()
{
int userInput = 0;
while (userInput != 5)
{
userInput = userChoices();
switch(userInput)
{
case 1: // Enter faculty info
printLines();
try
{
persons.add(new Faculty());
System.out.printf("%nThanks!%n");
printLines();
}
catch(IdException ex)
{
printLines();
System.out.println(ex.getMessage());
printLines();
}
break;
case 2: // Enter Student info
printLines();
System.out.printf("Enter the student's info: %n%n");
try
{
persons.add(new Student());
System.out.printf("%nThanks!%n");
printLines();
}
catch(IdException ex)
{
printLines();
System.out.println(ex.getMessage());
printLines();
}
break;
case 3: // Print Tuition Invoice
findInformation();
break;
case 4: // Print Faculty Info
findInformation();
break;
case 5: // exit
break;
default:
printLines();
System.out.printf("Invalid entry- please try again.%n");
printLines();
}
}
}
// User Choices Output and Scan In User Submission
public static int userChoices()
{
Scanner scanIn = new Scanner(System.in);
System.out.format("Choose one of the options: " + "%n%n" +
"1- Add a new Faculty member" + "%n" +
"2- Add a new Student" + "%n"
+ "3- Print tuition invoice for a student" + "%n" +
"4- Print information of a faculty" + "%n" + "5- Exit Program" + "%n%n" +
" Enter your selection: ");
String inputLine = scanIn.nextLine();
while (!NumericChecker.isInteger(inputLine))
{
Project3.printLines();
System.out.println("Invalid entry- Please try again.");
Project3.printLines();
System.out.format("Choose one of the options: " + "%n%n" +
"1- Add a new Faculty member" + "%n" +
"2- Add a new Student" + "%n"
+ "3- Print tuition invoice for a student" + "%n" +
"4- Print information of a faculty" + "%n" + "5- Exit Program" + "%n%n" +
" Enter your selection: ");
inputLine = scanIn.nextLine();
}
return Integer.parseInt(inputLine);
}
// Design Method
public static void printLines()
{
for(int i = 0; i < 63; i++)
{
System.out.print("-");
}
System.out.println();
}
public static Optional<Person> getPersonByID(String ID)
{
return persons.stream().filter(person -> person.getID().equals(ID)).findFirst();
}
public static void findInformation()
{
Scanner scanIn = new Scanner(System.in);
System.out.printf("%nEnter ID: ");
String inputLine = scanIn.nextLine().toLowerCase();
System.out.println();
Optional<Person> personOption = getPersonByID(inputLine);
if (personOption.isPresent())
{
Person person = personOption.get();
person.printInformation();
}
else
{
printLines();
System.out.println("Invalid entry- No information acquired.");
printLines();
}
}
}
class Student extends Person
{
private double studentGPA = 0.00;
private double creditHours = 0.00;
// Constructor #1: Pre-set Student Object
public Student(String name, String ID, double studentGPA, int creditHours)
{
super(name, ID);
this.studentGPA = studentGPA;
this.creditHours = creditHours;
}
public double getStudentGPA()
{
return this.studentGPA;
}
public void setStudentGPA(double studentGPA)
{
this.studentGPA = studentGPA;
}
public void setCreditHours(double creditHours)
{
this.creditHours = creditHours;
}
public double getCreditHours()
{
return this.creditHours;
}
void printEnterInfo()
{
System.out.printf("%20s", "Full name: ");
}
// Constructor #2: Student Information Intake
public Student() throws IdException
{
super();
Scanner scanIn = new Scanner(System.in);
System.out.printf("%20s", "Cumulative GPA: ");
String inputLine = scanIn.nextLine();
while (!NumericChecker.isDouble(inputLine) || inputLine.isBlank())
{
Project3.printLines();
System.out.println("Invalid entry- Please try again.");
Project3.printLines();
System.out.printf("%20s", "Cumulative GPA: ");
inputLine = scanIn.nextLine();
}
setStudentGPA(Double.parseDouble(inputLine));
System.out.printf("%20s", "Credit hours: ");
inputLine = scanIn.nextLine();
while (!NumericChecker.isDouble(inputLine) || inputLine.isBlank())
{
Project3.printLines();
System.out.println("Invalid entry- Please try again.");
Project3.printLines();
System.out.printf("%20s", "Credit hours: ");
inputLine = scanIn.nextLine();
}
setCreditHours(Double.parseDouble(inputLine));
}
// Print and calculate Student Tuition Invoice
public void printInformation()
{
double creditHourPrice = 236.45;
double administrativeFee = 52.00;
double studentDiscount = 0.00;
double tuitionInvoice = (creditHourPrice * getCreditHours()) + administrativeFee;
if (this.studentGPA >= 3.85)
{
studentDiscount = tuitionInvoice * .25;
tuitionInvoice = tuitionInvoice * .75;
}
Project3.printLines();
System.out.printf("%-20s" + "%s" + "%n%n" + "Credit Hours: %.2f" +
" ($236.45/credit hour) %n%n" + "Fees: $52 %n%n%nTotal payment: " +
"$%.2f" + " ($%.2f discount applied) %n", getName(),
getID(), this.creditHours, tuitionInvoice, studentDiscount);
Project3.printLines();
}
}
class Faculty extends Person
{
private String facultyDept;
private String facultyRank;
// Constructor #1: Pre-set Faculty Object
public Faculty(String name, String ID, String facultyDept, String facultyRank) throws IdException
{
super(name, ID);
this.facultyDept = facultyDept;
this.facultyRank = facultyRank;
}
public void setFacultyDept(String facultyDept)
{
while(this.facultyDept != facultyDept)
{
Scanner scanIn = new Scanner(System.in);
switch(facultyDept.toLowerCase())
{
case "mathematics":
this.facultyDept = facultyDept;
break;
case "engineering":
this.facultyDept = facultyDept;
break;
case "arts and science":
this.facultyDept = facultyDept;
break;
default:
Project3.printLines();
System.out.format("Sorry entered department (%s) is invalid.%n", facultyDept);
Project3.printLines();
System.out.printf("%20s", "Department: ");
facultyDept = scanIn.nextLine();
}
}
}
public String getFacultyDept()
{
return this.facultyDept;
}
public void setFacultyRank(String facultyRank)
{
while (!facultyRank.toLowerCase().equals("adjunct") &&
!facultyRank.toLowerCase().equals("professor"))
{
Scanner scanIn = new Scanner(System.in);
Project3.printLines();
System.out.format("Sorry entered rank (%s) is invalid.%n", facultyRank);
Project3.printLines();
System.out.printf("%20s", "Rank: ");
facultyRank = scanIn.nextLine();
}
this.facultyRank = facultyRank;
}
public String getFacultyRank()
{
return this.facultyRank;
}
// Constructor #2: Faculty Intake
public Faculty() throws IdException
{
super();
Scanner scanIn = new Scanner(System.in);
System.out.printf("%20s", "Rank: ");
setFacultyRank(scanIn.nextLine());
System.out.printf("%20s", "Department: ");
setFacultyDept(scanIn.nextLine());
}
void printEnterInfo()
{
System.out.printf("Enter faculty info: %n%n" + "%20s", "Full name: ");
}
// Print Faculty Information
public void printInformation()
{
Project3.printLines();
System.out.printf("%s%n%n" + "%s Department, " + "%s%n",
getName(), getFacultyDept(), getFacultyRank());
Project3.printLines();
}
}
class NumericChecker
{
public static boolean isDouble(String strNum)
{
if (strNum == null)
{
return false;
}
try
{
Double.parseDouble(strNum);
}
catch (NumberFormatException objExp)
{
return false;
}
return true;
}
public static boolean isInteger(String strNum)
{
if (strNum == null)
{
return false;
}
try
{
Integer.parseInt(strNum);
}
catch (NumberFormatException objExp)
{
return false;
}
return true;
}
}