Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java please help why is there not multiply students and staff member in my out put

wirewizard

New Coder
Java:
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

class Person {
    private String name;
    private String address;
    private String phoneNumber;
    private String emailAddress;

    public Person(String name, String address, String phoneNumber, String emailAddress) {
        this.name = name;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.emailAddress = emailAddress;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    @Override
    public String toString() {
        return "Person: " + getName();
    }
}

interface Course {
    void setCourseName(String courseName);
    String getCourseName();

    void addStudent(Student student);
    List<Student> getAssignedStudents();

    void setFaculty(Faculty faculty);
    Faculty getFaculty();

    void addStaff(Staff staff);
    List<Staff> getStaffMembers();

    default void printCourseDetails() {
        System.out.println("Course: " + getCourseName());

        // Print assigned students' details
        System.out.println("Assigned Students:");
        List<Student> students = getAssignedStudents();
        for (Student student : students) {
            System.out.println("Name: " + student.getName());
            System.out.println("Address: " + student.getAddress());
            System.out.println("Email: " + student.getEmail());
            System.out.println("Phone Number: " + student.getPhoneNumber());
            System.out.println();
        }

        // Print faculty teaching the course and their details
        System.out.println("Faculty teaching the course: " + getFaculty().getName());
        Faculty faculty = getFaculty();
        System.out.println("Faculty Details:");
        System.out.println("Name: " + faculty.getName());
        System.out.println("Address: " + faculty.getAddress());
        System.out.println("Email: " + faculty.getEmail());
        System.out.println("Phone Number: " + faculty.getPhoneNumber());
        System.out.println("Office: " + faculty.getOffice());
        System.out.println("Salary: " + faculty.getSalary());
        System.out.println("Date Hired: " + faculty.getDateHired());
        System.out.println("Courses Taught: " + faculty.getCoursesTaught());
        System.out.println();

        // Assign all staff members to the course and list their details
        System.out.println("Staff members assigned to the course:");
        List<Staff> staffMembers = getStaffMembers();
        for (Staff staff : staffMembers) {
            System.out.println("Staff Name: " + staff.getName());
            System.out.println("Title: " + staff.getTitle());
            System.out.println("Office: " + staff.getOffice());
            System.out.println("Salary: " + staff.getSalary());
            System.out.println("Date Hired: " + staff.getDateHired());
            System.out.println();
        }
        System.out.println("------------------------------------------------");
    }
}

class CourseImpl implements Course {
    private String courseName;
    private List<Student> assignedStudents;
    private Faculty faculty;
    private List<Staff> staffMembers;

    public CourseImpl(String courseName) {
        this.courseName = courseName;
        this.assignedStudents = new ArrayList<>();
        this.staffMembers = new ArrayList<>();
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setFaculty(Faculty faculty) {
        this.faculty = faculty;
    }

    public Faculty getFaculty() {
        return faculty;
    }

    public void addStudent(Student student) {
        assignedStudents.add(student);
    }

    public List<Student> getAssignedStudents() {
        return assignedStudents;
    }

    public void addStaff(Staff staff) {
        staffMembers.add(staff);
    }

    public List<Staff> getStaffMembers() {
        return staffMembers;
    }

    @Override
    public void printCourseDetails() {
        System.out.println("Course: " + getCourseName());

        // Print assigned students' details
        System.out.println("Assigned Students:");
        List<Student> students = getAssignedStudents();
        for (Student student : students) {
            System.out.println("Name: " + student.getName());
            System.out.println("Address: " + student.getAddress());
            System.out.println("Email: " + student.getEmail());
            System.out.println("Phone Number: " + student.getPhoneNumber());
            System.out.println();
        }

        // Print faculty teaching the course and their details
        System.out.println("Faculty teaching the course: " + getFaculty().getName());
        Faculty faculty = getFaculty();
        System.out.println("Faculty Details:");
        System.out.println("Name: " + faculty.getName());
        System.out.println("Address: " + faculty.getAddress());
        System.out.println("Email: " + faculty.getEmail());
        System.out.println("Phone Number: " + faculty.getPhoneNumber());
        System.out.println("Office: " + faculty.getOffice());
        System.out.println("Salary: " + faculty.getSalary());
        System.out.println("Date Hired: " + faculty.getDateHired());
        System.out.println("Courses Taught: " + faculty.getCoursesTaught());
        System.out.println();

        // Assign all staff members to the course and list their details
        System.out.println("Staff members assigned to the course:");
        List<Staff> staffMembers = getStaffMembers();
        for (Staff staff : staffMembers) {
            System.out.println("Staff Name: " + staff.getName());
            System.out.println("Title: " + staff.getTitle());
            System.out.println("Office: " + staff.getOffice());
            System.out.println("Salary: " + staff.getSalary());
            System.out.println("Date Hired: " + staff.getDateHired());
            System.out.println();
        }
        System.out.println("------------------------------------------------");
    }
}

class Student extends Person {
    public static final String FRESHMAN = "Freshman";
    private String classStatus;

    public Student(String name, String address, String phoneNumber, String email, String classStatus) {
        super(name, address, phoneNumber, email);
        this.classStatus = classStatus;
    }

    public String getClassStatus() {
        return classStatus;
    }

    @Override
    public String toString() {
        return "Student: " + getName();
    }

    public String getEmail() {
        return super.getEmailAddress();
    }
}

class Employee extends Person {
    private String office;
    private double salary;
    private LocalDate dateHired;

    public Employee(String name, String address, String phoneNumber, String email, String office, double salary, LocalDate dateHired) {
        super(name, address, phoneNumber, email);
        this.office = office;
        this.salary = salary;
        this.dateHired = dateHired;
    }

    public String getOffice() {
        return office;
    }

    public double getSalary() {
        return salary;
    }

    public LocalDate getDateHired() {
        return dateHired;
    }

    @Override
    public String toString() {
        return "Employee: " + getName();
    }
}

class Faculty extends Employee {
    private List<String> coursesTaught;
    private static final List<String> PRE_ASSIGNED_COURSES = List.of(
        "CSC100", "CSC200", "CSC330", "CSC340", "CSC325", "CSC335"
    );

    public Faculty(String name, String address, String phoneNumber, String email, String office, double salary, LocalDate dateHired) {
        super(name, address, phoneNumber, email, office, salary, dateHired);
        coursesTaught = new ArrayList<>();
    }

    public void assignCourse(String course) {
        if (PRE_ASSIGNED_COURSES.contains(course)) {
            coursesTaught.add(course);
            System.out.println(getName() + " is teaching " + course);
        } else {
            System.out.println("Course not available for assignment.");
        }
    }

    public List<String> getCoursesTaught() {
        return coursesTaught;
    }

    @Override
    public String toString() {
        return "Faculty: " + getName();
    }
   
    // Adding getEmail method
    public String getEmail() {
        return super.getEmailAddress();
    }
}

class Staff extends Employee {
    private String title;
    private static final String DEFAULT_TITLE = "Support Staff";

    public Staff(String name, String address, String phoneNumber, String email, String office, double salary, LocalDate dateHired) {
        super(name, address, phoneNumber, email, office, salary, dateHired);
        this.title = DEFAULT_TITLE;
    }

    public Staff(String name, String address, String phoneNumber, String email, String office, double salary, LocalDate dateHired, String title) {
        super(name, address, phoneNumber, email, office, salary, dateHired);
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public String toString() {
        return "Staff: " + getName();
    }

    public String getEmail() {
        return super.getEmailAddress();
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating instances of different entities
        Person person = new Person("John", "123 Main St", "123-456-7890", "[EMAIL][email protected][/EMAIL]");
        Student student = new Student("Alice", "456 Oak St", "987-654-3210", "[EMAIL][email protected][/EMAIL]", Student.FRESHMAN);
        Employee employee = new Employee("Bob", "789 Elm St", "555-555-5555", "[EMAIL][email protected][/EMAIL]", "Office123", 50000, LocalDate.now());
        Faculty faculty = new Faculty("Eve", "101 Pine St", "111-222-3333", "[EMAIL][email protected][/EMAIL]", "Office456", 80000, LocalDate.now());
        Staff staff = new Staff("Grace", "202 Maple St", "444-444-4444", "[EMAIL][email protected][/EMAIL]", "Office789", 40000, LocalDate.now(), "Registrar");

        // Creating a list of employees with 6 Faculty and 4 Staff members
        List<Employee> employees = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            Faculty newFaculty = new Faculty("Faculty" + (i + 1), "Address" + (i + 1), "111-222-333" + i, "faculty" + (i + 1) + "@example.com", "Office" + (i + 1), 60000 + (i * 5000), LocalDate.now());
            newFaculty.assignCourse("CSC" + (100 + i));
            newFaculty.assignCourse("CSC" + (200 + i));
            employees.add(newFaculty);
        }
        for (int i = 0; i < 4; i++) {
            if (i == 0) {
                Staff newStaff = new Staff("Staff" + (i + 1), "Address" + (i + 1), "111-222-444" + i, "staff" + (i + 1) + "@example.com", "Office" + (i + 1), 40000 + (i * 5000), LocalDate.now());
                employees.add(newStaff);
            } else {
                Staff newStaff = new Staff("Staff" + (i + 1), "Address" + (i + 1), "111-222-444" + i, "staff" + (i + 1) + "@example.com", "Office" + (i + 1), 40000 + (i * 5000), LocalDate.now(), "Title" + (i + 1));
                employees.add(newStaff);
            }
        }

        // Creating course instances
        Course csc100 = new CourseImpl("CSC100");
        Course csc200 = new CourseImpl("CSC200");
        Course csc330 = new CourseImpl("CSC330");
        Course csc340 = new CourseImpl("CSC340");
        Course csc325 = new CourseImpl("CSC325");
        Course csc335 = new CourseImpl("CSC335");

        // Adding students to courses
        csc100.addStudent(student);
        csc200.addStudent(student);
        csc330.addStudent(student);
        csc340.addStudent(student);
        csc325.addStudent(student);
        csc335.addStudent(student);
       
      
        csc100.setFaculty(faculty);
        csc200.setFaculty(faculty);
        csc330.setFaculty(faculty);
        csc340.setFaculty(faculty);
        csc325.setFaculty(faculty);
        csc335.setFaculty(faculty);

        for (Employee emp : employees) {
            if (emp instanceof Staff) {
                Staff staffMember = (Staff) emp;
                csc100.addStaff(staffMember);
                csc200.addStaff(staffMember);
                csc330.addStaff(staffMember);
                csc340.addStaff(staffMember);
                csc325.addStaff(staffMember);
                csc335.addStaff(staffMember);
            }
        }

        // Invoking toString() methods
        System.out.println(person);
        System.out.println(student);
        System.out.println(employee);
        System.out.println(faculty);
        System.out.println(staff);

        // Printing course details using the new method
        csc100.printCourseDetails();
        csc200.printCourseDetails();
        csc330.printCourseDetails();
        csc340.printCourseDetails();
        csc325.printCourseDetails();
        csc335.printCourseDetails();
    }
}
 
Last edited by a moderator:
It’s hard to read your code without indentation, but after a quick look I can’t see any code to print multiple students or staffs. At the end of main you print a single student etc. Maybe you are missing a loop here?
 
It’s hard to read your code without indentation, but after a quick look I can’t see any code to print multiple students or staffs. At the end of main you print a single student etc. Maybe you are missing a loop here?
The code lacks readability due to missing indentation, making it challenging to discern the structure. Additionally, a loop is absent for printing multiple students or staff members at the end of the main function, limiting its functionality.
 
You have noticed an issue with your output, which may be related to student and staff member data. Examine your input data and processing logic to ensure that all important information is taken into account. Step-by-step debugging and evaluating your code or system configuration might assist in identifying and addressing any issues with the output.Fm Whatsapp Download
 

New Threads

Buy us a coffee!

Back
Top Bottom