☕ Java What's the best way po to connect two java main files

PHC-Shade

Honorary Poster
So I am making po a college admission system for our mid term project and di ko po alam kung anong best approach to connect two java files since by group activities po and divided ang activities
Java:
package admission;

import java.util.Scanner; // SCANNER CLASS
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.InputMismatchException;

public class AdmissionSystem { // MAIN CLASS DECLARATION

    public static void main(String[] args) {
        // Admission Variables
        String info = "Required Info: Name, Age, Address, Course Intended, and GPA";
        double reqgpa = 93.0;
        String[] courses = {"BSCS", "BSIT", "BSChem", "Nursing"};
        int age;
        String ci = "";

        // Scheduling Variables
        int year = 2024;
        int month = 4;
        int score = -1;
        Scanner input = new Scanner(System.in);
        Random random = new Random();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // INPUT KAY PARA MUBASA SA USER
        System.out.println("Good day! Thank you for applying to Xavier University.");
        System.out.println("Here are the offered courses of Xavier University:");
        for (String course : courses) {
            System.out.println(course);
        }

        // SAME RA GA DIPLAY SA QUESTION
        System.out.print("Would you like to register? (Yes or No): ");
        String answer = input.nextLine().trim();

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

        // KANI PARA IF YES ANG ANSWER SA USER MU PADAYON SHA SA CODE
        if (answer.equalsIgnoreCase("Yes")) {
            boolean detailsCorrect = false;

            // WHILE LOOP         
            while (!detailsCorrect) {
                System.out.println(info);
                System.out.println("----------------------------");

                System.out.print("Name: ");
                String name = input.nextLine().trim();

                // Age input validation
                while (true) {
                    System.out.print("Age: ");
                    if (input.hasNextInt()) {
                        age = input.nextInt(); // Valid integer input
                        break; // Exit loop if valid input is received
                    } else {
                        System.out.println("Enter Valid Age"); // Prompt for valid input
                        input.nextLine(); // Consume the invalid input
                    }
                }

                // Consume newline character after nextInt()
                input.nextLine();

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

                boolean validCourse = false;

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

                    for (String course : courses) {
                        if (ci.equalsIgnoreCase(course)) {
                            validCourse = true; // Set flag to true if valid course is found
                            break;
                        }
                    }

                    if (!validCourse) {
                        System.out.println("Invalid course selection. Please try again."); // Prompt for valid course
                    }
                }

              
                double gpa;
                while (true) {
                    System.out.print("GPA: ");
                    if (input.hasNextDouble()) {
                        gpa = input.nextDouble();
                        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;+
                    System.out.println("----------------------------");

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

                    try {
                        Thread.sleep(2000); // Pause for 2 seconds to simulate processing
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

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

                    // IF ELSE STATEMENT
                    if (gpa >= reqgpa) {
                        System.out.println("Congratulations! You are eligible to take the entrance exam.");   
                        
                        // Scheduling Exam Process
                        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 examAnswer = input.nextLine();

                            if (examAnswer.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();
                            }
                        }
                        
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        
                        System.out.println("----------------------------\n");
                      
                        if (score > 80) {
                            System.out.println("Congratulations! You are qualified to be admitted to the University.");
                        } else {
                            System.out.println("Sorry your score did not qualify to be admitted to this University");
                        }
                        
                    } 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.");
                }
            }
        } else {
            System.out.println("Thank you for checking out Xavier University. Have a great day!");
        }

        input.close();
}

and
Java:
package admission;

import java.util.Scanner;
import java.util.Arrays;

public class Courses {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        String[] courses = {"BSIT", "Nursing", "BSCS", "BSChem"};
        int[] reqscore = {65, 70, 75, 80};
        String desiredCourse;
        boolean validCourse;
        int courseIndex = -1;
        int score ;

        System.out.println("The offered courses are:");
        System.out.println(Arrays.toString(courses));
        
        do {
            System.out.print("Enter the desired course: ");
            desiredCourse = scanner.nextLine();
            validCourse = false;
            
            for (int i = 0; i < courses.length; i++) {
                if (desiredCourse.equalsIgnoreCase(courses[i])) {
                    validCourse = true;
                    courseIndex = i;
                    break;
                }
            }
            
            if (!validCourse) {
                System.out.println("Invalid course selected. Please choose from the available courses.");
            }
        } while (!validCourse);     
        
    
        while (true) {
            System.out.print("Enter your score (out of 100): ");
            if (scanner.hasNextInt()) {
                score = scanner.nextInt();
                break;
            } else {
                System.out.println("Input valid score number");
                scanner.next();
            }
        }

        boolean isQualified = score >= reqscore[courseIndex];
        
        if (isQualified) {
            System.out.println("You are qualified for the course");
            System.out.println("You have successfully chosen: " + desiredCourse);
        } else {
            System.out.println("You are not qualified for the desired course.");
            System.out.println("Please choose another course.");
            scanner.nextLine();
            String otherCourse = scanner.nextLine();
            System.out.println("You have successfully chosen: " + otherCourse);
        }
        
        scanner.close();
    }
}
pa help po and pa suggest po what's the best way po
 
To connect the two Java files AdmissionSystem.java and Courses.java in your college admission system project, you can follow these steps:

### Approach 1: Using Main Class Communication

1. Create a New Java Class for Main Method:
- You can create a new Java class (let's say MainApplication.java) with the main method.
- This class will act as the entry point for your application.

2. Call the Main Methods of Existing Classes:
- Inside the MainApplication class, you can call the main methods of AdmissionSystem and Courses classes.
- This way, you can run both parts of your system from a single entry point.

### Approach 2: Modularize and Use Method Calls

1. Create Methods for Each Functionality:
- Instead of having everything inside the main method of each class, extract logical parts into separate methods within the classes.
- For example, create a method in AdmissionSystem to handle admission details input and verification, and a method in Courses to handle course selection and qualification.

2. Call Methods as Needed:
- From the main method of each class, call the relevant methods to execute specific functionalities.
- This approach helps in modularizing your code and makes it easier to connect different parts of the system.

### Approach 3: Use Class Instances and Objects

1. Create Objects of Other Classes:
- In your AdmissionSystem class, create an instance of the Courses class to interact with its methods.
- This allows you to access the functionalities of the Courses class from within the AdmissionSystem class.

2. Pass Data Between Classes:
- You can pass data (e.g., course selection, qualification status) between the classes by defining methods that return values or setting instance variables.

### Example (Approach 1):

Java:
public class MainApplication {

    public static void main(String[] args) {
        AdmissionSystem admissionSystem = new AdmissionSystem();
        Courses courses = new Courses();

        // Call the methods from both classes
        admissionSystem.handleAdmissionProcess();
        courses.handleCourseSelection();
    }
}

By following one of these approaches, you can effectively connect and integrate the functionalities of AdmissionSystem and Courses classes in your college admission system project. Choose the approach that best fits the structure and requirements of your project.
 
iisang main lang kada system,

isipin mo na parang html lang yung system mo, may main ka (which is index.html) then mag lilink ka ng mga pages mula sa index papunta sa desired page mo ex. admission, courses.

gawa ka ng main class (starter ng system mo) then import mo yung dalawang class mo which is admission saka courses.

tapos sa admission saka courses gawa ka ng function na e cacall mo apg need mo sila.

ex. PickCourse() StartAdmission()

then gawa ka nalang ng switch case saka mo tawagin yung mga function nila..

Special Note:
gagawa ka pala ng instance ng classes mo sa main class para magamit mo yung mga function nila.
ex . Admission admissionClass = new Admission();
then pag tinawag mo yung function nila ganto
admissionClass.StartAdmission();

Mukang naka OOP kau kaya mas maganda manood ka ng videos tungkol dyan para mas maunawaan mo. saka yung binigay ko di pa yan maayos kasi marami kang need ma e consider kapag nag wowork using OOP.
 

About this Thread

  • 2
    Replies
  • 414
    Views
  • 2
    Participants
Last reply from:
ewe we

Trending Topics

Online now

Members online
1,019
Guests online
1,568
Total visitors
2,587

Forum statistics

Threads
2,273,384
Posts
28,949,110
Members
1,235,723
Latest member
xberzerker
Back
Top