I'm currently try to write a program as below :
A college would like to automate managing courses offered to students. initially only three courses are involved. each courses has a code, a name, students enrolled, a current number of student, a maximum number of students allowed (quota) which is 10. a user should be able to add\drop a student to\from the course. a student can only be added to a course if the student is not yet enrolled in the course and the current number of students is not exceeding the maximum number allowed. a student can be dropped from a course only if she\he is enrolled in the course .

To test your program , you need to read an input file that contains ;
- *begin to indicate a course followed by the course code and name
- an integer to indicate the number of students enrolled so far in the course
- a list of matric numbers to represent students enrolled in the course,one matric number per line
- *end to indicate the end of data

Below is an example input data :
*begin SSK3100 Programming I
3 //number of current students enrolled in the course
19810
19910
20875
*begin SSK3101 Programming II
2
18910
19005
*begin SSK3107 Data Structures
4
18877
18777
18776
18976
*end

A kind-hearted person has help me with my code,but the problem is I still can't get the output to add and drop student..Any help would be greatly appreciated .

The link to my code :
Click Here

Recommended Answers

All 7 Replies

post the code here, I'm not going to log in in another platform to view it.
also be more clear in what exactly the problem is, that isn't really clear from your post.

Sorry,here are the code:

import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.regex.Pattern;

public class CollegeEnrollmentApp {
    public static void main(String[] args) {
     Map<String, Course> courses = new HashMap<String, Course>();
     String filePath = "c:/code/java/data.txt";
     loadStudent(filePath, courses);
     print(courses);

     System.exit(0);
    }

    private static void loadStudent(String filePath, Map<String, Course> courses) {
     Scanner fileIn = null;
     try {
         fileIn = new Scanner(new FileReader(filePath));
         String currentCourseNo = "";
         String currentCourseName = "";
         while (fileIn.hasNextLine()) {
             String record = fileIn.nextLine();

             if (isCourseHeader(record)) {
                 currentCourseNo = parseCourseNo(record);
                 currentCourseName = parseCourseName(record);
                 if (!courses.containsKey(currentCourseNo)) {
                     Course course = new Course(currentCourseNo, currentCourseName);
                     courses.put(currentCourseNo, course);
                 }
             }

             if (isStudentNo(record)) {
                 enrol(courses, currentCourseNo, record);
             }
         }
     } catch (IOException ex) {
         ex.printStackTrace();
     }
    }

    private static void print(Map<String, Course> courses) {
     for (Map.Entry<String, Course> me : courses.entrySet()) {
         System.out.println(me.getValue().formattedString());
     }
    }

    private static boolean isCourseHeader(String record) {
     return record.startsWith("*begin");
    }

    private static boolean isStudentNo(String record) {
     return Pattern.compile("\\d{5}").matcher(record).find();
    }

    private static String parseCourseNo(String record) {
     return record.substring(7, 14);
    }

    private static String parseCourseName(String record) {
     return record.substring(15);
    }

    private static void enrol(Map<String, Course> courses, String courseNo, String studentNo) {
     Course course = courses.get(courseNo);
     course.enrol(studentNo);
    }
}


class Course {
    private String courseNo;
    private String courseName;
    private List<Student> students = new ArrayList<Student>();
    private int maxStudentCount = 10;

    public Course(String courseNo, String courseName) {
     setCourseNo(courseNo);
     setCourseName(courseName);
    }

    public String getCourseNo() {
     return courseNo;
    }

    public void setCourseNo(String courseNo) {
     this.courseNo = courseNo;
    }

    public String getCourseName() {
     return this.courseName;
    }

    public void setCourseName(String courseName) {
     this.courseName = courseName;
    }

    public int getMaxStudentCount() {
     return maxStudentCount;
    }

    public void setMaxStudentCount(int maxStudentCount) {
     this.maxStudentCount = maxStudentCount;
    }

    public void enrol(String studentNo) {
     if (maxStudentCount > students.size()) {
         Student student = new Student(studentNo);
         if (!students.contains(student)) {
             students.add(student);
         } else {
             System.err.printf("Student %s already enrolled with this course %s.%n", studentNo, getCourseName());
         }
     } else {
         System.err.printf("The course %s is full (Max %d).%n", getCourseName(), getMaxStudentCount());
     }
    }

    public int getNumberOfEnrolledStudent() {
     return students.size();
    }

    @Override
    public String toString() {
     return getCourseName();
    }

    public String formattedString() {
     StringBuilder sb = new StringBuilder();
     sb.append(String.format("No:%s, Name:%s, Nrs. of student enrolled: %d.%n", getCourseNo(), getCourseName(), getNumberOfEnrolledStudent()));
     if (!this.students.isEmpty()) {
         for (Student student : students) {
             sb.append(String.format("%s%n", student));
         }
     }
     return sb.toString();
    }
}


class Student {
    private String studentNo;

    public Student(String studentNo) {
     this.studentNo = studentNo;
    }

    public String getStudentNo() {
     return studentNo;
    }

    public void setStudentNo(String studentNo) {
     this.studentNo = studentNo;
    }

    @Override
    public String toString() {
     return getStudentNo();
    }

    @Override
    public boolean equals(Object that) {
     if (!(that instanceof Student)) return false;
     return this.getStudentNo().equals(((Student) that).getStudentNo());
    }

    @Override
    public int hashCode() {
     return this.studentNo.hashCode();
    }
}

My problem is how can I display an output to offer a menu which contains choices to add student or to drop student from course? Could you pls guide me to do it.

Print a list of the courses to System.out. Create a Scanner for System.in. Go into a loop where you print a prompt and wait for the user to enter a command so you can read it with your Scanner, then obey the command, then do it all again repeatedly until the user enters a stop command.

The Scanner will automatically wait for the user to type something when you call nextLine. If the user types list then display the list of courses again. If the user types the number of a course, then set that course as the current course and display that course's information, including the students. If the user types drop 18910 then drop that student from the current course. If the user types add 22222 then add that student to the current course. Print error messages as necessary. If the user types something that makes no sense, print a list of all the available commands.

Sorry to ask but is it correct if I put below coding into my program?If yes where should it be,in the main class or in the Course class?I'm really new to this thing.tq

Scanner scan = new Scanner(System.in);
int menu = 0;
System.out.println("College System Main Menu");
System.out.println();
System.out.println("1. Add a student");
System.out.println("2. Remove a student");
System.out.println("3. Display student list");

System.out.print("Please enter your choice: ");
menu = scan.nextInt();
System.out.println();

switch(menu) {
case 1:
System.out.print("Enter student's matric number: ");
int ID = scan.nextInt();
String name= scan.next();
list.add(new Student(ID));
for (Student s : list){
System.out.println(s.getstudentNo);
}
case 2:
System.out.print("Enter Student id to remove the student from list: ");
int id = scan.nextInt();
int i=id-1;
list.remove(i);
case 3:
System.out.println("Matric number");
for (Student s : list){
System.out.println(s.getstudentNo());
}
}

Write a program to find the result of N students by accepting 5-subject marks from a Mark class and calculate the total and average mark. Display the result of all students in ascending order of rank from Result class (which is a subclass of Mark class). The result should consist of following information:

Rank
Roll Number
Name
Age
marks of the individual subject
Total and average mark.

please help me write the above program.

jignam:

  1. Don't hijack other people's posts. Start your own for your won question.
  2. DaniWeb Member Rules (which you agreed to when you signed up) include:
    "Do provide evidence of having done some work yourself if posting questions from school or work assignments"
    http://www.daniweb.com/community/rules

Create a new thread, post what you have done so far and someone will help you from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.