code and setup

This commit is contained in:
2026-05-02 17:53:39 +03:30
parent 9cee42eb50
commit 500afde0c8
10 changed files with 673 additions and 56 deletions
+67
View File
@@ -0,0 +1,67 @@
package models;
import java.util.ArrayList;
import java.util.List;
public class Course {
private static int nextId = 1;
private int id;
private String name;
private Teacher teacher;
private List<Student> students;
public Course(String name) {
this.id = nextId++;
this.name = name;
this.students = new ArrayList<>();
}
public int getId() { return id; }
public String getName() { return name; }
public Teacher getTeacher() { return teacher; }
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public boolean addStudent(Student student) {
if (students.contains(student)) {
return false;
}
students.add(student);
return true;
}
public List<Student> getStudents() {
return students; // ساده، بدون unmodifiable
}
public void showStudents() {
if (students.isEmpty()) {
System.out.println(" No students enrolled yet.");
} else {
System.out.println(" Students in " + name + ":");
for (Student s : students) {
System.out.println(" - " + s.getUsername());
}
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Course course = (Course) obj;
return id == course.id;
}
@Override
public String toString() {
if (teacher != null) {
return "[ID:" + id + "] " + name + " (Teacher: " + teacher.getUsername() + ")";
} else {
return "[ID:" + id + "] " + name + " (No teacher assigned)";
}
}
}
+15
View File
@@ -0,0 +1,15 @@
package models;
import java.util.ArrayList;
import java.util.List;
public class Student extends User {
private List<Course> enrolledCourses;
public Student(String username, String password) {
super(username, password);
this.enrolledCourses = new ArrayList<>();
}
//TODO(complete the student class)
}
+53
View File
@@ -0,0 +1,53 @@
package models;
import java.util.ArrayList;
import java.util.List;
public class Teacher extends User {
private List<Course> myCourses;
public Teacher(String username, String password) {
super(username, password);
this.myCourses = new ArrayList<>();
}
public boolean addCourse(Course course) {
if (myCourses.contains(course)) {
return false;
}
myCourses.add(course);
course.setTeacher(this);
return true;
}
public boolean removeCourse(Course course) {
if (myCourses.remove(course)) {
course.setTeacher(null);
return true;
}
return false;
}
public List<Course> getMyCourses() {
return myCourses;
}
public void showMyCourses() {
if (myCourses.isEmpty()) {
System.out.println(" You don't teach any course yet.");
} else {
System.out.println("\n My Courses:");
for (int i = 0; i < myCourses.size(); i++) {
System.out.println(" " + (i+1) + ". " + myCourses.get(i));
}
}
}
public void showStudentsInCourse(Course course) {
if (!myCourses.contains(course)) {
System.out.println(" You don't teach this course!");
return;
}
course.showStudents();
}
}
+42
View File
@@ -0,0 +1,42 @@
package models;
public abstract class User {
protected String username;
protected String password; // ساده نگه می‌داریم برای سطح مقدماتی
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() { return username; }
public boolean checkPassword(String password) {
return this.password.equals(password);
}
public static String validateUsername(String username) {
if (username == null || username.trim().isEmpty()) {
return "Username cannot be empty!";
}
if (username.length() < 3) {
return "Username must be at least 3 characters!";
}
return null;
}
public static String validatePassword(String password) {
if (password == null || password.length() < 4) {
return "Password must be at least 4 characters!";
}
return null;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
User user = (User) obj;
return username.equalsIgnoreCase(user.username);
}
}