0% found this document useful (0 votes)
8 views19 pages

Java Lab

The document contains multiple Java experiments demonstrating various programming concepts. Experiment 1 showcases a synchronized ticket booking system, Experiment 2 implements different payment methods using an interface, and Experiment 3 introduces custom exceptions for a bank account withdrawal process. Each experiment includes input code and expected output, illustrating the functionality of the implemented features.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views19 pages

Java Lab

The document contains multiple Java experiments demonstrating various programming concepts. Experiment 1 showcases a synchronized ticket booking system, Experiment 2 implements different payment methods using an interface, and Experiment 3 introduces custom exceptions for a bank account withdrawal process. Each experiment includes input code and expected output, illustrating the functionality of the implemented features.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment 1

INPUT

class TicketBooking {​
int availableTickets = 5;​

// synchronized method to avoid overbooking​
synchronized void bookTicket(String user, int tickets) {​
if (availableTickets >= tickets) {​
[Link](user + " is booking " + tickets +
" tickets...");​
try {​
[Link](1000); // simulate processing time​
} catch (InterruptedException e) {​
[Link](e);​
}​
availableTickets -= tickets;​
[Link]("Booking successful for " +
user);​
[Link]("Remaining tickets: " +
availableTickets);​
} else {​
[Link]("Sorry " + user + ", not enough
tickets available.");​
}​
}​
}​

class User extends Thread {​
TicketBooking booking;​
String user;​
int tickets;​

User(TicketBooking booking, String user, int tickets) {​
[Link] = booking;​
[Link] = user;​
[Link] = tickets;​
}​

public void run() {​
[Link](user, tickets);​
}​
}​

public class Main {​
public static void main(String[] args) {​
TicketBooking booking = new TicketBooking();​

User u1 = new User(booking, "User-1", 2);​
User u2 = new User(booking, "User-2", 2);​
User u3 = new User(booking, "User-3", 2);​

[Link]();​
[Link]();​
[Link]();​
}​
}

OUTPUT
EXPERIMENT 2

INPUT

// Interface​
interface Payment {​
void makePayment(double amount);​
}​

// Parent class​
class Gateway {​
String gatewayName = "HardikPay";​

void connect() {​
[Link]("Connected to " + gatewayName +
" payment gateway.");​
}​
}​

// 1. Credit Card​
class CreditCardPayment extends Gateway implements Payment
{​

public void makePayment(double amount) {​
connect();​
[Link]("Processing Credit Card payment
of Rs." + amount);​
[Link]("Credit Card payment
successful.\n");​
}​
}​

// 2. Debit Card​
class DebitCardPayment extends Gateway implements Payment {​

public void makePayment(double amount) {​
connect();​
[Link]("Processing Debit Card payment
of Rs." + amount);​
[Link]("Debit Card payment
successful.\n");​
}​
}​

// 3. UPI​
class UPIPayment extends Gateway implements Payment {​

public void makePayment(double amount) {​
connect();​
[Link]("Processing UPI payment of Rs."
+ amount);​
[Link]("UPI payment successful.\n");​
}​
}​

// 4. Net Banking​
class NetBankingPayment extends Gateway implements Payment
{​

public void makePayment(double amount) {​
connect();​
[Link]("Processing Net Banking payment
of Rs." + amount);​
[Link]("Net Banking payment
successful.\n");​
}​
}​

// 5. Wallet​
class WalletPayment extends Gateway implements Payment {​

public void makePayment(double amount) {​
connect();​
[Link]("Processing Wallet payment of
Rs." + amount);​
[Link]("Wallet payment successful.\n");​
}​
}​

// Main class​
public class Main {​
public static void main(String[] args) {​

Payment p1 = new CreditCardPayment();​
Payment p2 = new DebitCardPayment();​
Payment p3 = new UPIPayment();​
Payment p4 = new NetBankingPayment();​
Payment p5 = new WalletPayment();​

[Link](2500);​
[Link](1500);​
[Link](1200);​
[Link](5000);​
[Link](800);​
}​
}​
OUTPUT
EXPERIMENT 3

INPUT

// Custom Exception 1​
class InsufficientBalanceException extends Exception {​
public InsufficientBalanceException(String msg) {​
super(msg);​
}​
}​

// Custom Exception 2​
class InvalidAmountException extends Exception {​
public InvalidAmountException(String msg) {​
super(msg);​
}​
}​

// Custom Exception 3​
class DailyLimitExceededException extends Exception {​
public DailyLimitExceededException(String msg) {​
super(msg);​
}​
}​

class BankAccount {​

private double balance;​
private double dailyLimit = 10000; // daily
withdrawal limit​

public BankAccount(double balance) {​
[Link] = balance;​
}​

public void withdraw(double amount)​
throws InsufficientBalanceException,​
InvalidAmountException,​
DailyLimitExceededException {​

double oldBalance = balance; // save old balance
for rollback​

try {​

if (amount <= 0) {​
throw new InvalidAmountException("Amount
must be greater than 0.");​
}​

if (amount > dailyLimit) {​
throw new
DailyLimitExceededException("Daily withdrawal limit
exceeded.");​
}​

if (amount > balance) {​
throw new InsufficientBalanceException("Not
enough balance.");​
}​

// processing transaction​
[Link]("Processing withdrawal of ₹"
+ amount + "...");​
balance = balance - amount;​

[Link]("Transaction Successful.");​
[Link]("Updated Balance: ₹" +
balance);​
}​

catch (Exception e) {​
// Rollback logic​
balance = oldBalance;​
[Link]("Transaction Failed: " +
[Link]());​
[Link]("Rollback done. Balance
restored to ₹" + balance);​
}​

finally {​
[Link]("Transaction attempt
completed.\n");​
}​
}​
}​

public class Main {​
public static void main(String[] args) {​

BankAccount acc = new BankAccount(5000);​

try {​
[Link](2000); // valid​
[Link](7000); // insufficient balance​
[Link](-500); // invalid amount​
[Link](15000); // exceeds daily limit​
}​
catch (Exception e) {​
// just in case something unexpected happens​
[Link]("Unexpected Error: " +
[Link]());​
}​
}​
}

OUTPUT
EXPERIMENT-4
INPUT

OUTPUT
EXPERIMENT-5
INPUT

OUTPUT
EXPERIMENT-6
INPUT
OUTPUT
EXPERIMENT-7
INPUT
OUTPUT
EXPERIMENT-8
INPUT
OUTPUT

You might also like