Trên là Diagram của bài đó
package tuan05;
public class Faculty {
private String facultyID;
private String lastName;
private String firstName;
private String office;
public Faculty(String facultyID, String lastName, String firstName, String office) {
super();
this.facultyID = facultyID;
this.lastName = lastName;
this.firstName = firstName;
this.office = office;
}
public Faculty(){
this("0","0","0","0");
}
public String getFacultyID() {
return facultyID;
}
public void setFacultyID(String facultyID) {
this.facultyID = facultyID;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getOffice() {
return office;
}
public void setOffice(String office) {
this.office = office;
}
@Override
public String toString() {
return String.format("%10s\n%10s\n%10s\n%10s\n",facultyID,lastName,firstName,office);
}
}
Class
package tuan05;
public class Section{
private String sectionNbr;
private String semester;
private String room;
private static Faculty teacher;
public Section(String sectionNbr, String semester, String room,Faculty teacher) {
super();
this.sectionNbr = sectionNbr;
this.semester = semester;
this.room = room;
Section.teacher = teacher;
}
public Section(){
this("0","0","0",teacher);
}
public String getSectionNbr() {
return sectionNbr;
}
public void setSectionNbr(String sectionNbr) {
this.sectionNbr = sectionNbr;
}
public String getSemester() {
return semester;
}
public void setSemester(String semester) {
this.semester = semester;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
@Override
public String toString() {
return "Mã học phần: " + sectionNbr + "\nHọc kỳ: " + semester + "\nPhòng học: "
+ room + "\nGiảng viên: " + teacher.getLastName() + " " + teacher.getFirstName()
+ "(Khoa: " + teacher.getOffice() + ")";
}
}
Class
package tuan05;
public class Course {
private String courseNbr;
private String courseTitle;
private int credits;
private Section[] sections;
private int i=0;
public Course(){
this("0","0",0);
}
public Course(String courseNbr, String courseTitle, int credits) {
super();
this.courseNbr = courseNbr;
this.courseTitle = courseTitle;
this.credits = credits;
sections = new Section[5];
}
public String getCourseNbr() {
return courseNbr;
}
public void setCourseNbr(String courseNbr) {
this.courseNbr = courseNbr;
}
public String getCourseTitle() {
return courseTitle;
}
public void setCourseTitle(String courseTitle) {
this.courseTitle = courseTitle;
}
public int getCredits() {
return credits;
}
public void setCredits(int credits) {
this.credits = credits;
}
public void addSection(String SectionNbr, String semester, String room, Faculty listFa){
Section st = new Section(SectionNbr,semester,room,listFa);
sections[i]=st;
i++;
}
public void displayAll(){
for(Section sec : sections){
if(sec==null)
break;
System.out.println(sec + "\n");
}
}
@Override
public String toString() {
return "Khóa học: [" + courseNbr + " - " + courseTitle + " (" + credits + "TC)]\n" ;
}
}
Class
package tuan05;
public class Driver {
public static void main(String[] args) {
Faculty f1 = new Faculty("11111","Lê Kim","Khánh","CNTT");
Faculty f2 = new Faculty("22222","Nguyễn Thành","Danh","CNTT");
Course cs = new Course("THVP","Tin học văn phòng",4);
System.out.println(cs);
cs.addSection("120151","I - 2015", "H5.02",f1);
System.out.println("=========================================");
cs.addSection("220151","II - 2015", "B4.01",f2);
cs.displayAll();
}
}
Mình có thử chuyển Faculty sang kiểu mảng nhưng mà chuyển xong thì tè le đủ kiểu lại còn chạy không được >_<