Commit 240fbe4d authored by mohammadreza's avatar mohammadreza

add portal manager

parents
Pipeline #2834 canceled with stages
package portal;
import java.util.ArrayList;
import java.util.Scanner;
public class Course {
private String name;
private String courseId;
private int unitsAmount;
private int capacity;
private int currentSize = 0;
ArrayList<Student> students;
/**
* set course info
* @param name
* @param unitsAmount amounts of unit each course has
* @param courseId
* @param capacity
*/
public Course(String name , int unitsAmount , String courseId , int capacity){
this.name = name;
this.capacity = capacity;
setCourseId(courseId);
setUnitsAmount(unitsAmount);
students = new ArrayList<Student>();
}
/**
* @return course name
*/
public String getName() {
return name;
}
public int getUnitsAmount() {
return unitsAmount;
}
/**
* @param unitsAmount
*/
public void setUnitsAmount(int unitsAmount) {
if (unitsAmount >=1 && unitsAmount <=3){
this.unitsAmount = unitsAmount;
}
else {
System.out.println("Invalid request. unit's num must be between 1 & 3. set again.");
Scanner unit = new Scanner(System.in);
int un = Integer.parseInt(unit.nextLine());
setUnitsAmount(un);
}
}
/**
*
* @return course id
*/
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
if(courseId.length() == 4) {
this.courseId = courseId;
}
else {
System.out.println("Invalid course id. enter again.");
Scanner id = new Scanner(System.in);
String iD = id.nextLine();
setCourseId(iD);
}
}
/**
* @param student who take this course
*/
public boolean chooseCourse(Student student){
if (currentSize < capacity){
students.add(student);
currentSize++;
return true;
}
else{
System.out.println("No capacity to add " + student.getName() + " to course " + this.name);
return false;
}
}
public void removeStudent(Student student){
students.remove(student);
}
/**
* print course info
*/
public void printCourse(){
System.out.print(this.name + ": ");
for (Student i : students){
System.out.print(i.getName() + " ");
}
}
}
package portal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
public class Student {
private String name;
private int studentId;
private int average;
private int maxUnitsToTake ;
private int coursesWantToTake;
ArrayList<Course> courses ;
/**
* set name & id
* @param name
* @param id student id
*/
public Student(String name , int id , int average , int coursesWantToTake ){
this.name = name;
setAverage(average);
setStudentId(id);
this.coursesWantToTake = coursesWantToTake;
maxUnitsToTake = 0;
courses = new ArrayList<Course>();
}
public int getCoursesWantToTake() {
return coursesWantToTake;
}
/**
* @return student's name
*/
public String getName() {
return name;
}
/**
* @param average of scores
*/
public void setAverage(int average) {
if(average >= 0 && average <= 20 ) {
this.average = average;
}
else {
System.out.println("Invalid average. it must be between 1 & 20. set it again.");
Scanner average2 = new Scanner(System.in);
int avg = Integer.parseInt(average2.nextLine());
setAverage(avg);
}
}
/**
* @return average
*/
public int getAverage() {
return average;
}
/**
* @param studentId
*/
public void setStudentId(Integer studentId) {
if(studentId.toString().length() == 7) {
this.studentId = studentId;
}
else{
System.out.println("Invalid student id for " + name + ". enter a valid id.");
Scanner id = new Scanner(System.in);
int sId = Integer.parseInt(id.nextLine());
setStudentId(sId);
}
}
/**
* add course according to average
* check whether the student can take this course or not and also error if the student isn't for this college
* @param course to add
*/
public void addCourse(Course course){
if(Integer.parseInt(course.getCourseId())/100 != (studentId/1000)%100) {
System.out.println("You cannot take this course. it isn't in your college. you must take another.");
return;
}
if(average >= 15){
if (maxUnitsToTake + course.getUnitsAmount() <= 20) {
if (courses.size()==0){
courses.add(course);course.chooseCourse(this);maxUnitsToTake += course.getUnitsAmount();
coursesWantToTake--;
}
else {
int t=0;
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getName().equals(course.getName())) {
t=1;
break;
}
}
if(t==0){
if(course.chooseCourse(this)){
courses.add(course);
maxUnitsToTake += course.getUnitsAmount();
coursesWantToTake--;
}
}
else {
System.out.println("Repetitive unit. take another.");
}
}
}
else {
System.out.println(name + ", Your units must be less than 20. So you should remove some units");
}
}
if(average >= 10 && average<= 15){
if (maxUnitsToTake + course.getUnitsAmount() < 15){
if (courses.size()==0){
if(course.chooseCourse(this)){
courses.add(course);maxUnitsToTake += course.getUnitsAmount();
coursesWantToTake--;
}
}
else {
int t=0;
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getName().equals(course.getName())) {
t=1;
break;
}
}
if(t==0){
if(course.chooseCourse(this)){
courses.add(course);
maxUnitsToTake += course.getUnitsAmount();
coursesWantToTake--;
}
}
else {
System.out.println("Repetitive unit. take another.");
}
}
}
else {
System.out.println(name + ", Your units must be less than 15. So you can't add this unit.");
}
}
if(average <= 10 ){
if (maxUnitsToTake <= 15){
System.out.println("You can't take units");
coursesWantToTake =0;
return;
}
}
}
/**
* @param course to remove
*/
public void removeCourse(Course course){
courses.remove(course);
course.removeStudent(this);
coursesWantToTake++;
maxUnitsToTake-=course.getUnitsAmount();
}
/**
* print student's info
*/
public void printStudent(){
System.out.print(this.name + ": ");
for (Course i : courses){
System.out.print(i.getName() + " ");
}
}
}
package portal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
ArrayList<Course> courses = new ArrayList<Course>();
ArrayList<Student> students = new ArrayList<Student>();
Course course ;
Student student;
// course = new Course("ap" , 2 , "3101", 2 );
// student = new Student("mr" , 9831027 , 18 , 1);
// courses.add(course);
// students.add(student);
// student.addCourse(course);
// student.removeCourse(course);
// Iterator<Course> it = courses.iterator();
Scanner myObj = new Scanner(System.in);
int coursesNum = Integer.parseInt(myObj.nextLine());
/**
* creating Course object
*/
for (int i = 0; i < coursesNum ; i++) {
String courseName = myObj.nextLine();
String courseId = myObj.nextLine();
int courseUnit = Integer.parseInt(myObj.nextLine());
int capacity = Integer.parseInt(myObj.nextLine());
course = new Course(courseName , courseUnit , courseId , capacity);
courses.add(course);
}
int studentsNum = Integer.parseInt(myObj.nextLine());
/**
* creating Student object
*/
for (int i = 0; i < studentsNum ; i++) {
String studentName = myObj.nextLine();
int studentId = Integer.parseInt(myObj.nextLine());
int average = Integer.parseInt(myObj.nextLine());
int coursesWantToTake = Integer.parseInt(myObj.nextLine());
student = new Student(studentName , studentId , average , coursesWantToTake);
students.add(student);
while (student.getCoursesWantToTake() !=0 ){
String courseNameOrId = myObj.nextLine();
int t=0;
for (int j = 0; j < courses.size() ; j++) {
if(courseNameOrId.equals(courses.get(j).getName()) || courseNameOrId.equals(courses.get(j).getCourseId())){
student.addCourse(courses.get(j));
t=1;
}
}
if(t==0){
System.out.println("Undefined course. Please choose another one.");}
}
}
int numberOfStudentsToAddOrRemove = Integer.parseInt(myObj.nextLine());
/**
* adding or removing courses for students one by one
*/
Course course1 = null;
for (int i = 0; i < numberOfStudentsToAddOrRemove ; i++) {
String studentName = myObj.nextLine();
int numberOfCoursesToAddOrRemove = Integer.parseInt(myObj.nextLine());
for (int j = 0; j < numberOfCoursesToAddOrRemove ; j++) {
String courseIdToAddOrRemove = myObj.nextLine();
for (int k = 0; k < courses.size() ; k++) {
if(courses.get(k).getCourseId().equals(courseIdToAddOrRemove)){course1 = courses.get(k);}
}
for (int k = 0; k < students.size() ; k++) {
if(students.get(k).getName().equals(studentName)){
int l=0;
if (students.get(k).courses.contains(course1)){
students.get(k).removeCourse(course1);
}
else {
students.get(k).addCourse(course1);
}
}
}
}
}
for (int i = 0; i < courses.size() ; i++) {
courses.get(i).printCourse();
System.out.println();
}
for (int i = 0; i < students.size() ; i++) {
students.get(i).printStudent();
System.out.println();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment