KEMBAR78
Program Codes Template 1 | PDF | Method (Computer Programming) | Object Oriented Programming
0% found this document useful (0 votes)
1 views7 pages

Program Codes Template 1

.

Uploaded by

bernardmarcella1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

Program Codes Template 1

.

Uploaded by

bernardmarcella1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Group Midterm Task: Applying Objects, Encapsulation, Inheritance and Method

Overriding
CSPC 103 – Object-Oriented Programming

Group Year &


Members: Section:

Source Code of the Program:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static ArrayList<UniversityMember> records = new ArrayList<>();
static boolean running = true;
public static void main(String[] args) throws IOException {
while (running) {
System.out.println("\n=== UNIVERSITY MANAGEMENT SYSTEM ===");
System.out.println("1. Faculty");
System.out.println("2. Staff");
System.out.println("3. Student");
System.out.println("4. Show All Entries");
System.out.println("5. Exit");
System.out.print("Select an option: ");
int choice = safeReadInt();
if (choice == 1) {
showFacultyMenu();
} else if (choice == 2) {
addStaff();
} else if (choice == 3) {
addStudent();
} else if (choice == 4) {
showAllEntries();
} else if (choice == 5) {
System.out.println("Thank you for using this system");
running=false;
break;
} else {
System.out.println("Invalid choice. Try again.");
}
}
}
// === FACULTY MENU ===
static void showFacultyMenu() throws IOException {
System.out.println("--- FACULTY MENU ---");
System.out.println("1. Regular Faculty");
System.out.println("2. Contractual Faculty");
System.out.print("Choose faculty type: ");
int type = safeReadInt();
if (type == 1) addRegularFaculty();
else if (type == 2) addContractualFaculty();
else System.out.println("Invalid faculty type.");
Page 1 of 7
Group Midterm Task: Applying Objects, Encapsulation, Inheritance and Method
Overriding
CSPC 103 – Object-Oriented Programming

}
// ---- Regular Faculty ----
static void addRegularFaculty() throws IOException {
while (true) {
System.out.println("--- ADD REGULAR FACULTY ---");
System.out.print("Name: "); String name = br.readLine();
System.out.print("ID: "); String id = br.readLine();
System.out.print("Department: "); String department = br.readLine();
System.out.print("Base Salary (monthly): "); double base = safeReadDouble();
System.out.print("Subject: "); String subject = br.readLine();
System.out.print("Clothing Allowance: "); double clothing = safeReadDouble();
System.out.print("Sports Allowance: "); double sports = safeReadDouble();
RegularFaculty rf = new RegularFaculty(name, id, department, base, subject, clothing, sports);
records.add(rf);
System.out.println("--- REGULAR FACULTY DETAILS ---");
rf.showRegularFacultyInfo();
System.out.println("Annual Benefits: " + rf.regularFacultyBenefits());
while (true) {
System.out.print("Enter another details? Y/N: ");
String again = br.readLine();

if (again.equalsIgnoreCase("Y")) {
return;
} else if (again.equalsIgnoreCase("N")) {
System.out.println("Thank you for using this system");
running = false;
return;
} else {
System.out.println("Wrong input. Please enter Y/N:");
}
}
}
}
// ---- Contractual Faculty ----
static void addContractualFaculty() throws IOException {
while (true) {
System.out.println("--- ADD CONTRACTUAL FACULTY ---");
System.out.print("Name: "); String name = br.readLine();
System.out.print("ID: "); String id = br.readLine();
System.out.print("Department: "); String department = br.readLine();
System.out.print("Base Salary (monthly): "); double base = safeReadDouble();
System.out.print("Subject: "); String subject = br.readLine();
System.out.print("Contract Duration (Months): "); int duration = safeReadInt();
ContractualFaculty cf = new ContractualFaculty(name, id, department, base, subject, duration);
records.add(cf);
System.out.println("--- CONTRACTUAL FACULTY DETAILS ---");
cf.showContractualFacultyInfo();
System.out.println("Total Contract Benefits: " + cf.contractualFacultyBenefits());
while (true) {
System.out.print("Enter another details? Y/N: ");
String again = br.readLine();

if (again.equalsIgnoreCase("Y")) {
return;
} else if (again.equalsIgnoreCase("N")) {
System.out.println("Thank you for using this system");
running = false;
return;
} else {
System.out.println("Wrong input. Please enter Y/N:");
Page 2 of 7
Group Midterm Task: Applying Objects, Encapsulation, Inheritance and Method
Overriding
CSPC 103 – Object-Oriented Programming

}
}
}
}
// ---- Staff ----
static void addStaff() throws IOException {
while (true) {
System.out.println("--- ADD STAFF ---");
System.out.print("Name: ");
String name = br.readLine();
System.out.print("ID: ");
String id = br.readLine();
System.out.print("Department: ");
String department = br.readLine();
System.out.print("Base Salary (monthly): ");
double base = safeReadDouble();
System.out.print("Position: ");
String position = br.readLine();
System.out.print("Overtime Hours: ");
int hours = safeReadInt();
System.out.print("Overtime Rate: ");
double rate = safeReadDouble();
Staff st = new Staff(name, id, department, base, position, hours, rate);
records.add(st);
System.out.println("--- STAFF DETAILS ---");
st.showStaffInfo();
System.out.println("Annual Benefits: " + st.staffBenefits());
while (true) {
System.out.print("Enter another details? Y/N: ");
String again = br.readLine();

if (again.equalsIgnoreCase("Y")) {
return;
} else if (again.equalsIgnoreCase("N")) {
System.out.println("Thank you for using this system");
running = false;
return;
} else {
System.out.println("Wrong input. Please enter Y/N:");
}
}
}
}
// ---- Student ----
static void addStudent() throws IOException {
while (true) {
System.out.println("--- ADD STUDENT ---");
System.out.print("Name: ");
String name = br.readLine();
System.out.print("ID: ");
String id = br.readLine();
System.out.print("Department: ");
String department = br.readLine();
System.out.print("Course: ");
String course = br.readLine();
System.out.print("GPA: ");
double gpa = safeReadDouble();
Student s = new Student(name, id, department, course, gpa);
records.add(s);
System.out.println("--- STUDENT DETAILS ---");
Page 3 of 7
Group Midterm Task: Applying Objects, Encapsulation, Inheritance and Method
Overriding
CSPC 103 – Object-Oriented Programming

s.showStudentInfo();
System.out.println("Scholarship Status: " + s.scholarshipStatus());
while (true) {
System.out.print("Enter another details? Y/N: ");
String again = br.readLine();

if (again.equalsIgnoreCase("Y")) {
return;
} else if (again.equalsIgnoreCase("N")) {
System.out.println("Thank you for using this system");
running = false;
return;
} else {
System.out.println("Wrong input. Please enter Y/N:");
}
}
}
}
// ---- Show All Entries (Grouped) ----
static void showAllEntries() {
if (records.isEmpty()) {
System.out.println("\n(No entries yet.)");
return;
}
System.out.println("\n=== ALL SAVED ENTRIES (" + records.size() + ") ===");
// Regular Faculty
boolean hasRegular = false;
for (UniversityMember m : records) {
if (m instanceof RegularFaculty) {
if (!hasRegular) {
System.out.println("\n--- REGULAR FACULTY ---");
hasRegular = true;
}
RegularFaculty rf = (RegularFaculty) m;
rf.showRegularFacultyInfo();
System.out.println("Annual Benefits: " + rf.regularFacultyBenefits());
System.out.println();
}
}
// Contractual Faculty
boolean hasContractual = false;
for (UniversityMember m : records) {
if (m instanceof ContractualFaculty) {
if (!hasContractual) {
System.out.println("\n--- CONTRACTUAL FACULTY ---");
hasContractual = true;
}
ContractualFaculty cf = (ContractualFaculty) m;
cf.showContractualFacultyInfo();
System.out.println("Total Contract Benefits: " + cf.contractualFacultyBenefits());
System.out.println();
}
}
// Staff
boolean hasStaff = false;
for (UniversityMember m : records) {
if (m instanceof Staff) {
if (!hasStaff) {
System.out.println("\n--- STAFF ---");
hasStaff = true;
Page 4 of 7
Group Midterm Task: Applying Objects, Encapsulation, Inheritance and Method
Overriding
CSPC 103 – Object-Oriented Programming

}
Staff st = (Staff) m;
st.showStaffInfo();
System.out.println("Annual Benefits: " + st.staffBenefits());
System.out.println();
}
}
// Students
boolean hasStudent = false;
for (UniversityMember m : records) {
if (m instanceof Student) {
if (!hasStudent) {
System.out.println("\n--- STUDENTS ---");
hasStudent = true;
}
Student s = (Student) m;
s.showStudentInfo();
System.out.println("Scholarship Status: " + s.scholarshipStatus());
System.out.println();
}
}
}
// === SAFE INPUT HELPERS ===
static int safeReadInt() throws IOException {
while (true) {
try { return Integer.parseInt(br.readLine()); }
catch (NumberFormatException e) { System.out.print("Enter a valid integer: "); }
}
}
static double safeReadDouble() throws IOException {
while (true) {
try { return Double.parseDouble(br.readLine()); }
catch (NumberFormatException e) { System.out.print("Enter a valid number: "); }
}
}
}

public class UniversityMember {


private String name;
private String id;
private String department;
public UniversityMember(String name, String id, String department) {
this.name = name;
this.id = id;
this.department = department;
}
public String getName() { return name; }
public String getId() { return id; }
public String getDepartment() { return department; }
public void showMemberInfo() {
System.out.println("Name: " + name);
System.out.println("ID: " + id);
System.out.println("Department: " + department);
}
}

public class Employee extends UniversityMember {


Page 5 of 7
Group Midterm Task: Applying Objects, Encapsulation, Inheritance and Method
Overriding
CSPC 103 – Object-Oriented Programming

private double baseMonthlySalary;


public Employee(String name, String id, String department, double baseMonthlySalary) {
super(name, id, department);
this.baseMonthlySalary = baseMonthlySalary;
}
public double getBaseMonthlySalary() {
return baseMonthlySalary;
}
public void showEmployeeInfo() {
showMemberInfo();
System.out.println("Base Salary (Monthly): " + baseMonthlySalary);
}
}

public class Faculty extends Employee {


private String subject;
public Faculty(String name, String id, String department,
double baseMonthlySalary, String subject) {
super(name, id, department, baseMonthlySalary);
this.subject = subject;
}
public void showFacultyInfo() {
showEmployeeInfo();
System.out.println("Subject: " + subject);
}
}

public class RegularFaculty extends Faculty {


private double clothingAllowance;
private double sportsAllowance;
public RegularFaculty(String name, String id, String department,
double baseMonthlySalary, String subject,
double clothingAllowance, double sportsAllowance) {
super(name, id, department, baseMonthlySalary, subject);
this.clothingAllowance = clothingAllowance;
this.sportsAllowance = sportsAllowance;
}
public void showRegularFacultyInfo() {
showFacultyInfo();
System.out.println("Faculty Type: Regular");
System.out.println("Clothing Allowance: " + clothingAllowance);
System.out.println("Sports Allowance: " + sportsAllowance);
}
public double regularFacultyBenefits() {
return (getBaseMonthlySalary() * 12) + clothingAllowance + sportsAllowance;
}
}

public class ContractualFaculty extends Faculty {


private int contractDuration;
public ContractualFaculty(String name, String id, String department,
double baseMonthlySalary, String subject,
int contractDuration) {
super(name, id, department, baseMonthlySalary, subject);
this.contractDuration = contractDuration;
}
public void showContractualFacultyInfo() {
showFacultyInfo();
System.out.println("Faculty Type: Contractual");
System.out.println("Contract Duration: " + contractDuration + " months");
Page 6 of 7
Group Midterm Task: Applying Objects, Encapsulation, Inheritance and Method
Overriding
CSPC 103 – Object-Oriented Programming

}
public double contractualFacultyBenefits() {
return getBaseMonthlySalary() * contractDuration;
}
}

public class Staff extends Employee {


private String position;
private int overtimeHours;
private double overtimeRate;
public Staff(String name, String id, String department,
double baseMonthlySalary, String position,
int overtimeHours, double overtimeRate) {
super(name, id, department, baseMonthlySalary);
this.position = position;
this.overtimeHours = overtimeHours;
this.overtimeRate = overtimeRate;
}
public void showStaffInfo() {
showEmployeeInfo();
System.out.println("Position: " + position);
System.out.println("Overtime Hours: " + overtimeHours);
System.out.println("Overtime Rate: " + overtimeRate);
}
public double staffBenefits() {
return (getBaseMonthlySalary() * 12) + (overtimeHours * overtimeRate);
}
}

public class Student extends UniversityMember {


private String course;
private double gpa;
public Student(String name, String id, String department, String course, double gpa) {
super(name, id, department);
this.course = course;
this.gpa = gpa;
}
public void showStudentInfo() {
showMemberInfo();
System.out.println("Course: " + course);
System.out.println("GPA: " + gpa);
}
public String scholarshipStatus() {
if (gpa >= 1.00 && gpa <= 1.25) return "President's Lister";
if (gpa >= 1.26 && gpa <= 1.50) return "Chancellor's Lister";
if (gpa >= 1.51 && gpa <= 1.75) return "Dean's Lister";
return "Not Qualified";
}
}

Page 7 of 7

You might also like