2 Commits
Author SHA1 Message Date
mahdi_Goudarzi bd02a76215 write main class 2026-04-27 02:55:03 -07:00
mahdi_Goudarzi 5ecf78a545 complet classes and write tese class 2026-04-27 01:57:33 -07:00
3 changed files with 181 additions and 12 deletions
+113
View File
@@ -1,7 +1,120 @@
package CourseRegistrationStarter;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
RegistrationSystem register = new RegistrationSystem();
Course riazi1 = new Course("riazi1" , "200" , 5);
Course riazi2 = new Course("riazi2" , "201" , 4);
Course ap = new Course("ap" , "202" , 10);
Course bp = new Course("bp" , "203" , 0);
register.addCourseToSystem(riazi1);
register.addCourseToSystem(riazi2);
register.addCourseToSystem(ap);
register.addCourseToSystem(bp);
Student daneshjo = new Student("ali" , "404222000");
boolean yes = true;
while( yes == true ){
System.out.println("----------Menu----------");
System.out.println("1. show all course");
System.out.println("2. add course");
System.out.println("3. delete course");
System.out.println("4.my course");
System.out.println("5.exit");
System.out.print("choose item :");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
n = (n % 5);
switch (n)
{
case 1:
{
register.showAllCourses();
break;
}
case 2:
{
System.out.println("please enter the code of course");
String courseCode = input.next();
if(register.findCourseByCode(courseCode) != null){
if(daneshjo.addCourse(register.findCourseByCode(courseCode))){
System.out.println("done");
}
else{
if(register.findCourseByCode(courseCode).getCapacity() <=0){
System.out.println("cours is full");
}
else if(daneshjo.existe(register.findCourseByCode(courseCode))){
System.out.println("you have get this course befor");
}
else {
System.out.println("you have deleted this course befor");
}
}
}
else {
System.out.println("invalid course");
}
break;
}
case 3:
{
System.out.println("please enter the code of course");
String courseCode = input.next();
if(register.findCourseByCode(courseCode) != null){
if(daneshjo.dropCourse(register.findCourseByCode(courseCode))){
System.out.println("done");
}
else {
System.out.println("invaild");
}
}
else{
System.out.println("Invalid course");
}
break;
}
case 4:
{
daneshjo.showRegisteredCourses();
break;
}
case 0:
{
yes = false;
break;
}
}
}
// TODO:
// 1. Create at least 3 courses
// 2. Create 1 student
@@ -5,20 +5,35 @@ public class RegistrationSystem {
private ArrayList<Course> courses;
public RegistrationSystem() {
courses = new ArrayList<>();
courses = new ArrayList<Course>();
}
public void addCourseToSystem(Course c) {
// TODO: Add the course to the system
courses.add(c);
}
public Course findCourseByCode(String code) {
// TODO: Search for a course by course code
// Return the matching course if found, otherwise return null
int n = courses.size();
for(int i = 0 ; i < n ; i++){
if(courses.get(i).getCourseCode().equals(code)){
return courses.get(i);
}
}
return null;
}
public void showAllCourses() {
// TODO: Print all courses in the system
int n = courses.size();
System.out.println("All courses :");
for(int i = 0 ; i < n ; i++){
System.out.printf("course name: %10s " , courses.get(i).getCourseName());
System.out.printf("course code: %10s " , courses.get(i).getCourseCode());
System.out.printf("cours capacity: %10s " , courses.get(i).getCapacity());
System.out.println("");
}
}
}
+47 -6
View File
@@ -5,11 +5,13 @@ public class Student {
private String name;
private String studentId;
private ArrayList<Course> registeredCourses;
private ArrayList<Course> deletedCourse;
public Student(String name, String studentId) {
this.name = name;
this.studentId = studentId;
this.registeredCourses = new ArrayList<>();
this.deletedCourse = new ArrayList<>();
}
public String getName() {
@@ -21,19 +23,58 @@ public class Student {
}
public boolean addCourse(Course c) {
// TODO: Register the student in the course if capacity allows
// Return true if registration was successful, otherwise false
if(c.getCapacity() > 0&& !existe(c) && !checkdeleted(c)){
registeredCourses.add(c);
c.reduceCapacity();
return true;
}
return false;
}
public boolean dropCourse(Course c) {
// TODO: Remove the course from the student's list
// Restore the course capacity if removal was successful
// Return true if the course was dropped, otherwise false
int n = registeredCourses.size();
for(int i = 0 ; i < n ; i++){
if(registeredCourses.get(i).getCourseName() == c.getCourseName()){
registeredCourses.remove(i);
c.increaseCapacity();
deletedCourse.add(c);
return true;
}
}
return false;
}
public void showRegisteredCourses() {
// TODO: Print all registered courses
int n = registeredCourses.size();
System.out.println(this.name + "\'s Courses :");
for(int i = 0 ; i < n ; i++){
System.out.printf("course name: %10s " , registeredCourses.get(i).getCourseName());
System.out.printf("cours code: %10s " , registeredCourses.get(i).getCourseCode());
System.out.println("");
}
}
public boolean checkdeleted(Course c){
int n = deletedCourse.size();
for(int i = 0 ; i < n ; i++){
if (deletedCourse.get(i).getCourseCode().equals(c.getCourseCode())){
return true;
}
}
return false;
}
public boolean existe(Course c){
int n = registeredCourses.size();
for(int i = 0 ; i < n ; i++){
if(registeredCourses.get(i).getCourseCode().equals(c.getCourseCode())){
return true;
}
}
return false;
}
}