Compare commits
2
Commits
8ddd529c85
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bd02a76215 | ||
|
|
5ecf78a545 |
@@ -1,7 +1,120 @@
|
|||||||
package CourseRegistrationStarter;
|
package CourseRegistrationStarter;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
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:
|
// TODO:
|
||||||
// 1. Create at least 3 courses
|
// 1. Create at least 3 courses
|
||||||
// 2. Create 1 student
|
// 2. Create 1 student
|
||||||
|
|||||||
@@ -5,20 +5,35 @@ public class RegistrationSystem {
|
|||||||
private ArrayList<Course> courses;
|
private ArrayList<Course> courses;
|
||||||
|
|
||||||
public RegistrationSystem() {
|
public RegistrationSystem() {
|
||||||
courses = new ArrayList<>();
|
courses = new ArrayList<Course>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addCourseToSystem(Course c) {
|
public void addCourseToSystem(Course c) {
|
||||||
// TODO: Add the course to the system
|
courses.add(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Course findCourseByCode(String code) {
|
public Course findCourseByCode(String code) {
|
||||||
// TODO: Search for a course by course code
|
int n = courses.size();
|
||||||
// Return the matching course if found, otherwise return null
|
for(int i = 0 ; i < n ; i++){
|
||||||
|
if(courses.get(i).getCourseCode().equals(code)){
|
||||||
|
return courses.get(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showAllCourses() {
|
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("");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ public class Student {
|
|||||||
private String name;
|
private String name;
|
||||||
private String studentId;
|
private String studentId;
|
||||||
private ArrayList<Course> registeredCourses;
|
private ArrayList<Course> registeredCourses;
|
||||||
|
private ArrayList<Course> deletedCourse;
|
||||||
|
|
||||||
public Student(String name, String studentId) {
|
public Student(String name, String studentId) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.studentId = studentId;
|
this.studentId = studentId;
|
||||||
this.registeredCourses = new ArrayList<>();
|
this.registeredCourses = new ArrayList<>();
|
||||||
|
this.deletedCourse = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@@ -21,19 +23,58 @@ public class Student {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean addCourse(Course c) {
|
public boolean addCourse(Course c) {
|
||||||
// TODO: Register the student in the course if capacity allows
|
if(c.getCapacity() > 0&& !existe(c) && !checkdeleted(c)){
|
||||||
// Return true if registration was successful, otherwise false
|
registeredCourses.add(c);
|
||||||
|
c.reduceCapacity();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean dropCourse(Course c) {
|
public boolean dropCourse(Course c) {
|
||||||
// TODO: Remove the course from the student's list
|
int n = registeredCourses.size();
|
||||||
// Restore the course capacity if removal was successful
|
for(int i = 0 ; i < n ; i++){
|
||||||
// Return true if the course was dropped, otherwise false
|
if(registeredCourses.get(i).getCourseName() == c.getCourseName()){
|
||||||
|
registeredCourses.remove(i);
|
||||||
|
c.increaseCapacity();
|
||||||
|
deletedCourse.add(c);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showRegisteredCourses() {
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user