☕ Java For phc-bot 4

PHC-Shade

Honorary Poster
Java:
import java.util.Scanner;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Random;

class Students {
    String name;
    int age;
    String address;
    String courseIntended;
    double gpa;
    int score;

    public Students(String name, int age, String address, String courseIntended, double gpa, int score) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.courseIntended = courseIntended;
        this.gpa = gpa;
        this.score = score;
    }
    }

public class CollegeAdmissionSystem {
    public ArrayList<Students> students = new ArrayList<>();
    String[] courses = {"BSCS", "BSIT", "BSIS", "BSEMC"};
    int[] reqscore = {65, 70, 75, 80};
    Scanner input = new Scanner(System.in);
    String info, answer;
    private static final double REQUIRED_GPA = 93.0;

    public  CollegeAdmissionSystem() {
        System.out.println("| Thank you for Checking on Biglang Gwapo University |");
        System.out.println("Here are the offered Courses: " + Arrays.toString(courses));
        
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        do {   
            System.out.print("Would you like to register? (Yes or No): ");
            answer = input.nextLine().trim().toLowerCase();

            System.out.println("----------------------------");   

            switch (answer) {
                case "yes":
                    studentReg();
                    break;
                case "no":
                    System.out.println("Thank you! Have a great day!");
                    break;
                default:
                    System.out.println("Invalid input. Please enter Yes or No.");
            }
        } while (!answer.equals("yes") && !answer.equals("no"));
        
        input.close();
    }

    public void studentReg() {
        info = "Required Info: Name, Age, Address, Course Intended, and GPA";
        boolean detailsCorrect = false;

        while (!detailsCorrect) {
            System.out.println(info);
            System.out.println("----------------------------");   
            System.out.print("Name: ");
            String name = input.nextLine();
            String ci = "";

            int age = 0;
            boolean validAge = false;

            while (!validAge) {
                System.out.print("Age: ");
                if (input.hasNextInt()) {
                    age = input.nextInt();
                    if (age < 15 || age > 100) {
                        System.out.println("Invalid Age Range. Please enter an age between 15 and 100.");
                    } else {
                        validAge = true;
                    }
                } else {
                    System.out.println("Please enter a valid Age.");
                }
                input.nextLine();
            }

            System.out.print("Address: ");
            String address = input.nextLine();

            boolean validCourse = false;
            
            while (!validCourse) {
                System.out.print("Course Intended: ");
                ci = input.nextLine().toUpperCase();

                for (String course : courses) {
                    if (ci.equalsIgnoreCase(course)) {
                        validCourse = true;
                        break;
                    }
                }
                if (!validCourse) {
                    System.out.println("Invalid Course Input. Please select from the offered courses.");
                }
            }

            double gpa;
            while (true) {
                System.out.print("GPA: ");
                if (input.hasNextDouble()) {
                    gpa = input.nextDouble();
                    if (gpa < 0 || gpa > 100) {
                        System.out.println("Please enter a GPA between 0 and 100.");
                        continue;
                    }
                    break;                 } else {
                    System.out.println("Enter a valid GPA.");
                    input.nextLine();
                }
            }

            input.nextLine();

            System.out.println("----------------------------");

            System.out.println("Please double-check your details:");
            System.out.printf("Name: %s\nAge: %d\nAddress: %s\nCourse Intended: %s\nGPA: %.2f\n",
                              name, age, address, ci, gpa);

            System.out.print("Are the information correct? (Yes or No): ");
            String response = input.nextLine().trim();

            if (response.equalsIgnoreCase("Yes")) {
                detailsCorrect = true;

          
           students.add(new Students(name, age, address, ci, gpa, 0));

                System.out.println("----------------------------");

                System.out.println("Thanks for applying. Please wait while we verify your details...");

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("----------------------------");

                if (gpa >= REQUIRED_GPA) {
                    System.out.println("Congratulations! You are eligible to take the entrance exam.");   
                    Schedule();
                } else {
                    System.out.println("Thank you for applying. Unfortunately, your GPA does not meet the required 93. We wish you the best in your future endeavors.");
                }
            } else {
                System.out.println("Please re-enter your details.");
            }
        }
    }

    public void Schedule() {
            int year = 2024;
            int month = 4;
            int score = -1;
            Random random = new Random();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

            System.out.println("University Admission Process");
            System.out.println("----------------------------\n");

            boolean satisfied = false;

            while (!satisfied) {
                System.out.println("Scheduling your exam.");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
               int day = random.nextInt(30) + 1;
                LocalDate randomDate = LocalDate.of(year, month, day);
                String formattedDate = randomDate.format(formatter);

                System.out.println("Your Exam Date is " + formattedDate);
                System.out.print("Are you satisfied with your scheduled date? (Yes or No): ");
                String answer = input.nextLine();

                if (answer.equalsIgnoreCase("Yes")) {
                    satisfied = true;
                } else {
                
                    System.out.println("Scheduling a new exam date\n");
                    System.out.println("----------------------------\n");
                    
                }
            }


            while (true) {
                  System.out.print("Step 2: Input your exam score:");
                try {
                    score = input.nextInt();
            if (score < 0 || score > 100 ) {
                System.out.println("Input valid score ");
                    continue;
            }   
            break;
                } catch (InputMismatchException e) {
                    System.out.println("Invalid input. Please enter a valid number score.");
                    input.next();
                }
            }
                    if (!students.isEmpty()) {
                        students.get(students.size() - 1).score = score;
                    } else {
                        System.out.println("No student details found to update the score.");
                    }
            
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("----------------------------\n");
          
            if (score>60) {
                System.out.println("Congratulations! You are qualified to be admitted to the University.");
                Course();
            }else {
                    System.out.println("Sorry your score did not qualify to be admitted to this University");
                }
                    }   
    
    public void Course() {
        if (students.isEmpty()) {
                    System.out.println("No student details found to determine course qualification.");
                    return;
                }
    
                Students lastStudent = students.get(students.size() - 1);
                String desiredCourse = lastStudent.courseIntended;
                int score = lastStudent.score;
        
                int courseIndex = -1;
                for (int i = 0; i < courses.length; i++) {
                    if (desiredCourse.equalsIgnoreCase(courses[i])) {
                        courseIndex = i;
                        break;
                    }
                }
        boolean isQualified = score >= reqscore[courseIndex];
        
        if (isQualified) {
            System.out.println("You are qualified for the course");
            System.out.println("You have successfully chosen: " + desiredCourse);
            Subjects();
        } else {
            System.out.println("You are not qualified for the desired course.");
            System.out.println("Please choose another course.");
            input.nextLine();
            String otherCourse = input.nextLine();
            System.out.println("You have successfully chosen: " + otherCourse);
        }
    }
            
            public void displayStudents() {
                for(Students student : students){
                    System.out.printf(
                        "Name: %s\nAge: %d\nAddress: %s\nCourse Intended: %s\nGPA: %.2f\nScore: %d\n",
                        student.name,
                        student.age,
                        student.address,
                        student.courseIntended,
                        student.gpa,
                        student.score);
                    }
                }
            
    public void Subjects() {
        String[] subjects = {"Math", "History", "CC10", "CC11", "Philosophy", "Theology", "NSTP", "PE"};
        
        ArrayList<String> enrolledSubjects = new ArrayList<>();
        
        System.out.println("----------------------------");
        
        boolean showSubjects = true;
        while (true) {
            if (showSubjects) {
                System.out.println("Here are the offered Subjects");
                System.out.println("----------------------------\n");
                for (int i = 0; i < subjects.length; i++) {
                    System.out.println("[" + (i + 1) + "] " + subjects[i]);         
                }
                System.out.println("----------------------------\n");
            }

            System.out.print("Do you want to Add, Remove Subjects or Exit (Type: Add, Remove, Exit): ");
            String response = input.nextLine();
            
            if (response.equalsIgnoreCase("Add")) {
                addSubjects(subjects, enrolledSubjects, input);
                showSubjects = false;
            } else if (response.equalsIgnoreCase("Remove")) {
                removeSubjects(subjects, enrolledSubjects, input);
                showSubjects = false;
            } else if (response.equalsIgnoreCase("Exit")) {
                break;
            } else {
                System.out.println("Invalid option. Please try again.");
            }
        }

        System.out.println("You are currently enrolled in: " + enrolledSubjects);

        input.close();
    }

    private static void addSubjects(String[] subjects, ArrayList<String> enrolledSubjects, Scanner input) {
        System.out.print("Enter the number of the subject you want to add: ");
        
        try {
            int subjectNumber = input.nextInt();
            input.nextLine();
            if (subjectNumber > 0 && subjectNumber <= subjects.length) {
                String subjectToAdd = subjects[subjectNumber - 1];
                if (!enrolledSubjects.contains(subjectToAdd)) {
                    enrolledSubjects.add(subjectToAdd);
                    System.out.println(subjectToAdd + " has been added.");
                } else {
                    System.out.println("You are already enrolled in " + subjectToAdd);
                }
            } else {
                System.out.println("Invalid subject number.");
            }
        } catch (Exception e) {
            System.out.println("Please enter a valid number.");
            input.nextLine();
        }
    }

    private static void removeSubjects(String[] subjects, ArrayList<String> enrolledSubjects, Scanner input) {
        if (enrolledSubjects.isEmpty()) {
            System.out.println("You are not currently enrolled in any subjects.");
            return;
        }

        System.out.println("Here are your currently enrolled Subjects:");
        for (int i = 0; i < enrolledSubjects.size(); i++) {
            System.out.println("[" +(i + 1) + "] " + enrolledSubjects.get(i));
        }

        System.out.print("Enter the number of the subject you want to remove: ");
        
        try {
            int subjectNumber = input.nextInt();
            input.nextLine();
            if (subjectNumber > 0 && subjectNumber <= enrolledSubjects.size()) {
                String subjectToRemove = enrolledSubjects.remove(subjectNumber - 1);
                System.out.println(subjectToRemove + " has been removed.");
            } else {
                System.out.println("Invalid subject number.");
            }
        } catch (Exception e) {
            System.out.println("Please enter a valid number.");
            input.nextLine();
        }
    }


    public static void main(String[] args) {
        new CollegeAdmissionSystem();
    }
}

bakit nag dodouble print ang subject
----------------------------
Here are the offered Subjects
----------------------------

[1] Math
[2] History
[3] CC10
[4] CC11
[5] Philosophy
[6] Theology
[7] NSTP
[8] PE
----------------------------

Do you want to Add, Remove Subjects or Exit (Type: Add, Remove, Exit): Invalid option. Please try again.
Here are the offered Subjects
----------------------------

[1] Math
[2] History
[3] CC10
[4] CC11
[5] Philosophy
[6] Theology
[7] NSTP
[8] PE
----------------------------

Do you want to Add, Remove Subjects or Exit (Type: Add, Remove, Exit):
eh wala pa naman akong ininput after natapos ang course class
 
Ang isyu sa pag-double print ng listahan ng mga offered subjects ay dahil sa pagkakatawag ng System.out.println("Here are the offered Subjects"); sa loob ng addSubjects at removeSubjects methods. Kada tawag sa isa sa mga ito, ito ay nag-iinstruct na i-print muli ang listahan ng mga offered subjects bago iproseso ang karagdagang o pag-alis ng mga subjects.

Para maiwasan ang double print ng listahan ng mga offered subjects, maaari mong tanggalin ang pag-print nito sa loob ng addSubjects at removeSubjects methods. Dito ay mayroon akong ilang mga rekomendasyon para sa iyo:

1. I-update ang addSubjects at removeSubjects methods: Alisin ang pag-print ng mga offered subjects sa loob ng dalawang methods na ito. Dapat lamang itong iprint sa loob ng Subjects method bago pumasok sa loop at i-check kung ang user ay nais magdagdag o magtanggal ng subjects.

2. I-dry run ang process: Siguraduhing na hindi ka mag-i-print ng listahan ng mga offered subjects sa loob ng addSubjects at removeSubjects methods upang maiwasan ang pag-double print.

Narito ang pagkakabago sa addSubjects at removeSubjects methods:

Java:
private static void addSubjects(String[] subjects, ArrayList<String> enrolledSubjects, Scanner input) {
    System.out.print("Enter the number of the subject you want to add: ");

    try {
        int subjectNumber = input.nextInt();
        input.nextLine();
        if (subjectNumber > 0 && subjectNumber <= subjects.length) {
            String subjectToAdd = subjects[subjectNumber - 1];
            if (!enrolledSubjects.contains(subjectToAdd)) {
                enrolledSubjects.add(subjectToAdd);
                System.out.println(subjectToAdd + " has been added.");
            } else {
                System.out.println("You are already enrolled in " + subjectToAdd);
            }
        } else {
            System.out.println("Invalid subject number.");
        }
    } catch (Exception e) {
        System.out.println("Please enter a valid number.");
        input.nextLine();
    }
}

private static void removeSubjects(String[] subjects, ArrayList<String> enrolledSubjects, Scanner input) {
    if (enrolledSubjects.isEmpty()) {
        System.out.println("You are not currently enrolled in any subjects.");
        return;
    }

    System.out.println("Here are your currently enrolled Subjects:");
    for (int i = 0; i < enrolledSubjects.size(); i++) {
        System.out.println("[" + (i + 1) + "] " + enrolledSubjects.get(i));
    }

    System.out.print("Enter the number of the subject you want to remove: ");

    try {
        int subjectNumber = input.nextInt();
        input.nextLine();
        if (subjectNumber > 0 && subjectNumber <= enrolledSubjects.size()) {
            String subjectToRemove = enrolledSubjects.remove(subjectNumber - 1);
            System.out.println(subjectToRemove + " has been removed.");
        } else {
            System.out.println("Invalid subject number.");
        }
    } catch (Exception e) {
        System.out.println("Please enter a valid number.");
        input.nextLine();
    }
}

Dahil sa pag-update na ito, ang listahan ng mga offered subjects ay hindi na mag-double print sa bawat pag-tawag sa addSubjects at removeSubjects methods. Mangyaring subukan ito at ipaalam sa akin kung mayroon pang ibang katanungan o pangangailangan ng tulong.
 

About this Thread

  • 1
    Replies
  • 302
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
341
Guests online
1,084
Total visitors
1,425

Forum statistics

Threads
2,273,575
Posts
28,950,428
Members
1,235,837
Latest member
ninzi
Back
Top