← Back to Midterms

Student Payment System with Validation Counter

Explanation & Learnings

For this activity, we were tasked to create a Student Payment System that further challenges our understanding of Java control structures. Compared to the previous ATM program, this one had more moving parts — tracking a student's information, their remaining tuition balance, discount eligibility, and a transaction counter all at the same time. The structure was still built around a do-while loop with a switch statement, but the logic inside each case was noticeably more complex.

One of the trickier parts was implementing the discount feature. I had to make sure that it could only be applied once, only to scholars, and only when there was still a remaining balance. Managing all three conditions without them conflicting with each other took some careful thinking. I handled it using a "discountCounter" variable that flags whether the discount has already been availed, which felt like a simple but effective solution.

I also found it meaningful that the exit case wasn't just a goodbye message — it doubled as a transaction summary, displaying the student's name, ID, total number of transactions, and final balance. That detail made the program feel closer to something you'd actually encounter in a real school system.

StudentPaymentSystem.java
package midtermact4;

import java.util.Scanner;

public class StudentPaymentSystemDeOro {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("===== STUDENT INFORMATION =====");
        System.out.print("Enter Student Name: ");
        String studNa = sc.nextLine();

        System.out.print("Enter Student ID: ");
        String studID = sc.nextLine();

        System.out.print("Enter Total Tuition Fee: PHP ");
        double tuition = sc.nextDouble();

        int choice;
        double balance = tuition;
        int discountCounter = 0;
        int transactionCounter = 0;

        do {
            System.out.println("\n===== PAYMENT MENU =====");
            System.out.println("1. Pay Tuition");
            System.out.println("2. Check Balance");
            System.out.println("3. Apply Discount");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");

            choice = sc.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("\n===== PAY TUITION =====");

                    if (balance <= 0) {
                        System.out.println("No remaining balance.");
                    } else {
                        System.out.print("Enter payment amount: PHP ");
                        double payment = sc.nextDouble();

                        if (payment <= 0) {
                            System.out.println("Error! Payment amount must be greater than zero.");
                        } else if (payment > balance) {
                            System.out.println("Invalid Payment");
                        } else {
                            balance -= payment;
                            transactionCounter++;
                            System.out.printf("Payment successful. Remaining Balance: PHP %.2f%n", balance);

                            if (balance <= 0) {
                                System.out.println("No remaining balance.");
                            }
                        }
                    }
                    break;

                case 2:
                    System.out.println("\n===== CHECK BALANCE =====");
                    transactionCounter++;

                    if (balance <= 0) {
                        System.out.println("No remaining balance.");
                    } else {
                        System.out.printf("Remaining Balance: PHP %.2f%n", balance);
                    }
                    break;

                case 3:
                    System.out.println("\n===== APPLY DISCOUNT =====");

                    if (balance <= 0) {
                        System.out.println("No remaining balance.");
                    } else if (discountCounter == 1) {
                        System.out.println("Discount already availed. It can only be applied once.");
                    } else {
                        System.out.println("Select Student Type:");
                        System.out.println("1. Regular Student");
                        System.out.println("2. Scholar");
                        System.out.print("Enter choice: ");

                        int studType = sc.nextInt();

                        if (studType == 2) {
                            double discount = balance * 0.20;
                            balance -= discount;
                            discountCounter++;
                            transactionCounter++;
                            System.out.printf("Discount applied: PHP %.2f%n", discount);
                            System.out.printf("New Balance: PHP %.2f%n", balance);
                        } else if (studType == 1) {
                            System.out.println("Regular students are not eligible for discount.");
                            transactionCounter++;
                        } else {
                            System.out.println("Error! Please enter a valid choice.");
                        }
                    }
                    break;

                case 4:
                    System.out.println("\n===== TRANSACTION SUMMARY =====");
                    System.out.println("Student Name: " + studNa);
                    System.out.println("Student ID: " + studID);
                    System.out.println("Total Transactions: " + transactionCounter);
                    System.out.printf("Final Balance: PHP %.2f%n", balance);
                    System.out.println("Transaction ended. Thank you for using our student service.");
                    break;

                default:
                    System.out.println("Error! Please try again from the menu.");
            }
        } while (choice != 4);
    }
}
Terminal
===== STUDENT INFORMATION =====
Enter Student Name: China
Enter Student ID: 2024-02641
Enter Total Tuition Fee: PHP 10000

===== PAYMENT MENU =====
1. Pay Tuition
2. Check Balance
3. Apply Discount
4. Exit
Enter your choice: 2

===== CHECK BALANCE =====
Remaining Balance: PHP 10000.00