Added Java lab programs

This commit is contained in:
2025-12-17 16:36:20 +05:30
parent ac2904f5db
commit 2f4beade3b
46 changed files with 882 additions and 0 deletions
+94
View File
@@ -0,0 +1,94 @@
import java.util.Scanner;
// -------- BASE CLASS --------
class Payment {
double amount;
Payment(double amount) {
this.amount = amount;
}
double processPayment() {
return amount;
}
}
// -------- SUB CLASS : CREDIT CARD --------
class CreditCardPayment extends Payment {
CreditCardPayment(double amount) {
super(amount);
}
double processPayment() {
return amount + (amount * 0.02); // 2% service charge
}
}
// -------- SUB CLASS : UPI --------
class UPIPayment extends Payment {
UPIPayment(double amount) {
super(amount);
}
double processPayment() {
return amount + 5; // Flat ₹5 charge
}
}
// -------- SUB CLASS : NET BANKING --------
class NetBankingPayment extends Payment {
NetBankingPayment(double amount) {
super(amount);
}
double processPayment() {
return amount + (amount * 0.015); // 1.5% service charge
}
}
// -------- MAIN CLASS --------
public class OnlinePaymentSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter payment amount: ");
double amount = sc.nextDouble();
System.out.println("\nChoose Payment Method:");
System.out.println("1. Credit Card");
System.out.println("2. UPI");
System.out.println("3. Net Banking");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
Payment payment; // Base class reference
switch (choice) {
case 1:
payment = new CreditCardPayment(amount);
break;
case 2:
payment = new UPIPayment(amount);
break;
case 3:
payment = new NetBankingPayment(amount);
break;
default:
System.out.println("Invalid choice");
sc.close();
return;
}
// Dynamic method dispatch
double finalAmount = payment.processPayment();
System.out.println("\nFinal Payable Amount: ₹" + finalAmount);
sc.close();
}
}