5 Commits
Author SHA1 Message Date
Meraj 9abd43e241 Merge pull request 'Mdevelop' (#3) from Mdevelop into main
Awesome! You nailed this workshop too. Great job completing it(100/120)
2026-05-19 15:07:31 +00:00
Matin f3b3c42688 finial 2026-05-01 01:14:29 +04:30
Matin 8ddc581f61 add menu 2026-04-30 02:19:31 +04:30
Matin 8e69c031e1 matin mortazavi / work shop + bonus 2026-04-20 21:34:24 +04:30
Matin ca498f1f28 matin mortazavi / i did my Assignment + bonus 2026-04-20 19:36:40 +04:30
4 changed files with 162 additions and 5 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ public class Course {
this.courseCode = courseCode;
this.capacity = capacity;
}
// test
public String getCourseName() {
return courseName;
}
+79 -1
View File
@@ -1,7 +1,13 @@
package CourseRegistrationStarter;
import java.lang.reflect.Field ;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
public static void main(String[] args) throws NoSuchFieldException {
// TODO:
// 1. Create at least 3 courses
// 2. Create 1 student
@@ -10,5 +16,77 @@ public class Main {
// 5. Register the student in some courses
// 6. Drop one course
// 7. Print the final registered course list
RegistrationSystem reg = new RegistrationSystem() ;
Course AP = new Course("ap" , "1404" , 2 );
reg.addCourseToSystem(AP) ;
Course DS = new Course("DS" , "2201" , 1 );
reg.addCourseToSystem(DS);
Course DB = new Course("Db" , "3301" , 3 );
reg.addCourseToSystem(DB);
Map<String, Course> courseMap = new HashMap<>();
courseMap.put("AP", AP);
courseMap.put("DS", DS);
courseMap.put("DB", DB);
Student Ali = new Student("ali" , "402222001") ;
//test
Scanner input = new Scanner(System.in) ;
int x = 0 ;
while (x != 6 ) {
System.out.println("menu");
System.out.println("1) Add course ");
System.out.println("2) drop course ");
System.out.println("3) show registered courses");
System.out.println("4 ) show all courses");
System.out.println("5) Show capacities ");
System.out.println("6) exit ");
System.out.println("enter your choice ");
x = input.nextInt() ;
input.nextLine() ;
if (x == 1 )
{
System.out.println("Available courses: AP - DS - DB");
String y = input.nextLine().trim();
Course selected = courseMap.get(y.toUpperCase());
if (selected != null) {
Ali.addCourse(selected);
} else {
System.out.println("No");
}
}
else if (x == 2 ) {
System.out.println("courses names : AP - DS - DB");
String y = input.nextLine().trim();
Course selected = courseMap.get(y.toUpperCase());
if (selected != null) {
Ali.dropCourse(selected);
} else {
System.out.println("No");
}
}
else if (x == 3 ) {
Ali.showRegisteredCourses();
}
else if (x == 4 )
{
reg.showAllCourses();
} else if (x == 5 ) {
reg.showcapacity();
} else if (x == 6) {
continue;
}
}
}
}
@@ -1,5 +1,6 @@
package CourseRegistrationStarter;
import java.util.ArrayList;
import java.util.Objects;
public class RegistrationSystem {
private ArrayList<Course> courses;
@@ -10,15 +11,63 @@ public class RegistrationSystem {
public void addCourseToSystem(Course c) {
// TODO: Add the course to the system
if (courses.contains(c)) {
System.out.println("you cant add this course !!");
}
else
{
courses.add(c);
}
}
// test
public Course findCourseByCode(String code) {
// TODO: Search for a course by course code
// Return the matching course if found, otherwise return null
for (Course item : courses)
{
if (Objects.equals(item.getCourseCode(), code)) {
return item ;
}
}
return null;
}
public Course findCourseByName(String name)
{
for (Course item : courses)
{
if (Objects.equals(item.getCourseCode(), name)) {
return item ;
}
}
return null;
}
public void showAllCourses() {
// TODO: Print all courses in the system
for (Course item : courses)
{
System.out.print("course name : ");
System.out.print(item.getCourseName());
System.out.print(" , course code");
System.out.print(item.getCourseCode());
System.out.print(" , course capacity");
System.out.print(item.getCapacity());
}
}
public void showcapacity(){
for (Course item : courses)
{
System.out.println(item.getCapacity());
}
}
}
@@ -23,17 +23,47 @@ 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 (registeredCourses.size()<= 10) {
if (c.reduceCapacity()) {
registeredCourses.add(c);
return true;
} else {
return false;
}
}
else
{
return false ;
// test
}
}
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 ind = registeredCourses.indexOf(c) ;
if (ind != -1 )
{
registeredCourses.remove(ind) ;
c.increaseCapacity();
return true ;
}
else {
return false;
}
}
public void showRegisteredCourses() {
// TODO: Print all registered courses
for (Course item : registeredCourses)
{
System.out.print("course name : ");
System.out.print(item.getCourseName());
System.out.print(" , course code");
System.out.print(item.getCourseCode());
}
}
}