0% found this document useful (0 votes)
16 views13 pages

Abdullah

Uploaded by

Abdullah Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views13 pages

Abdullah

Uploaded by

Abdullah Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Code

import java.util.InputMismatchException;

import java.util.Scanner;

class InsufficientFundsException extends Exception {

public InsufficientFundsException(String message) {

super(message);

class BankAccount {

private double balance;

public BankAccount() {

this.balance = 0.0;

public void deposit(double amount) throws IllegalArgumentException {

if (amount <= 0) {

throw new IllegalArgumentException("Deposit amount must be greater than zero.");

balance += amount;

public void withdraw(double amount) throws InsufficientFundsException, IllegalArgumentException


{

if (amount <= 0) {

throw new IllegalArgumentException("Withdrawal amount must be greater than zero.");

}
if (amount > balance) {

throw new InsufficientFundsException("Insufficient funds for withdrawal.");

balance -= amount;

public double getBalance() {

return balance;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

BankAccount account = new BankAccount();

while (true) {

System.out.println("\nChoose an option:");

System.out.println("1. Deposit");

System.out.println("2. Withdraw");

System.out.println("3. Check Balance");

System.out.println("4. Exit");

try {

int choice = scanner.nextInt();

switch (choice) {

case 1:

System.out.print("Enter deposit amount: ");


double depositAmount = scanner.nextDouble();

account.deposit(depositAmount);

System.out.println("Deposit successful.");

break;

case 2:

System.out.print("Enter withdrawal amount: ");

double withdrawalAmount = scanner.nextDouble();

account.withdraw(withdrawalAmount);

System.out.println("Withdrawal successful.");

break;

case 3:

System.out.println("Current balance: " + account.getBalance());

break;

case 4:

System.out.println("Exiting application.");

System.exit(0);

default:

System.out.println("Invalid choice.");

} catch (InsufficientFundsException e) {

System.err.println("Error: " + e.getMessage());

} catch (IllegalArgumentException e) {

System.err.println("Error: " + e.getMessage());

} catch (InputMismatchException e) {

System.err.println("Error: Invalid input. Please enter a valid number.");

scanner.nextLine(); // Clear the invalid input

}
}

Output

Question 2

Scenario: You are tasked with developing a banking application that performs various operations like
deposits, withdrawals, and balance inquiries. The application needs to handle invalid input and runtime
errors effectively, such as attempting to withdraw more money than available.

Task Description:

• Create a class BankAccount with methods for deposit(double amount) and withdraw(double
amount).

• Implement exception handling to manage:

– IllegalArgumentException for negative deposit/withdrawal amounts.

– InsufficientFundsException for withdrawals that exceed the current balance.

• Write a main method to simulate user interactions, capturing any exceptions thrown and
displaying appropriate error messages.

Code:
import java.util.Scanner;

// Exception classes

class InvalidCardException extends Exception {

public InvalidCardException(String message) {

super(message);

class InsufficientFundsException extends Exception {

public InsufficientFundsException(String message) {

super(message);

class PaymentNetworkException extends Exception {

public PaymentNetworkException(String message) {

super(message);

// PaymentMethod interface

interface PaymentMethod {

void processPayment(double amount) throws InvalidCardException, InsufficientFundsException,


PaymentNetworkException;

// CreditCard class

class CreditCard implements PaymentMethod {


@Override

public void processPayment(double amount) throws InvalidCardException {

// Simulate a condition where the card number is invalid

boolean isCardNumberValid = false; // This is just a simulation

if (!isCardNumberValid) {

throw new InvalidCardException("Invalid credit card number.");

System.out.println("Credit card payment processed successfully.");

// PayPal class

class PayPal implements PaymentMethod {

@Override

public void processPayment(double amount) throws InsufficientFundsException {

// Simulate a condition where the PayPal balance is too low

boolean hasSufficientFunds = false; // This is just a simulation

if (!hasSufficientFunds) {

throw new InsufficientFundsException("Insufficient PayPal balance.");

System.out.println("PayPal payment processed successfully.");

// BankTransfer class
class BankTransfer implements PaymentMethod {

@Override

public void processPayment(double amount) throws PaymentNetworkException {

// Simulate a condition where there is a network issue

boolean isNetworkAvailable = false; // This is just a simulation

if (!isNetworkAvailable) {

throw new PaymentNetworkException("Network issue while processing the bank transfer.");

System.out.println("Bank transfer payment processed successfully.");

// Main class

public class PaymentSystem {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

PaymentMethod paymentMethod = null;

System.out.println("Choose a payment method: 1 for Credit Card, 2 for PayPal, 3 for Bank
Transfer");

int choice = scanner.nextInt();

switch (choice) {

case 1:

paymentMethod = new CreditCard();

break;

case 2:
paymentMethod = new PayPal();

break;

case 3:

paymentMethod = new BankTransfer();

break;

default:

System.out.println("Invalid choice.");

return;

System.out.println("Enter the amount to be paid:");

double amount = scanner.nextDouble();

boolean paymentSuccessful = false;

while (!paymentSuccessful) {

try {

paymentMethod.processPayment(amount);

paymentSuccessful = true;

System.out.println("Payment successful!");

} catch (InvalidCardException | InsufficientFundsException | PaymentNetworkException e) {

System.out.println("Payment failed: " + e.getMessage());

System.out.println("Retrying payment...");

// Optionally, you can allow the user to select a different payment method here

// and update the paymentMethod variable accordingly

}
scanner.close();

Question 3

Scenario: In a transportation system, you need to manage various modes of transport like cars, bikes,
and trucks. Each mode of transport has specific capabilities but should also adhere to common
transportation functionalities.

Task Description:

i. Create a package named Transport to contain all transport-related classes.

ii. Define two interfaces: Drivable with methods such as drive() and stop(), and Loadable with
methods such as loadCargo(int weight) and unloadCargo().

iii. Implement classes Car, Bike, and Truck that implement the Drivable interface. The Truck class
should also implement the Loadable interface. Each class should define its unique attributes and
methods while adhering to the interface contracts.

iv. Create main class in package mainpkg and implement all classes in main class.

v. Show the result.

Code

import java.util.Scanner;

// Exception classes

class InvalidCardException extends Exception {

public InvalidCardException(String message) {

super(message);

class InsufficientFundsException extends Exception {

public InsufficientFundsException(String message) {

super(message);
}

class PaymentNetworkException extends Exception {

public PaymentNetworkException(String message) {

super(message);

// PaymentMethod interface

interface PaymentMethod {

void processPayment(double amount) throws InvalidCardException, InsufficientFundsException,


PaymentNetworkException;

// CreditCard class

class CreditCard implements PaymentMethod {

@Override

public void processPayment(double amount) throws InvalidCardException {

// Simulate a condition where the card number is invalid

boolean isCardNumberValid = false; // This is just a simulation

if (!isCardNumberValid) {

throw new InvalidCardException("Invalid credit card number.");

System.out.println("Credit card payment processed successfully.");

}
// PayPal class

class PayPal implements PaymentMethod {

@Override

public void processPayment(double amount) throws InsufficientFundsException {

// Simulate a condition where the PayPal balance is too low

boolean hasSufficientFunds = false; // This is just a simulation

if (!hasSufficientFunds) {

throw new InsufficientFundsException("Insufficient PayPal balance.");

System.out.println("PayPal payment processed successfully.");

// BankTransfer class

class BankTransfer implements PaymentMethod {

@Override

public void processPayment(double amount) throws PaymentNetworkException {

// Simulate a condition where there is a network issue

boolean isNetworkAvailable = false; // This is just a simulation

if (!isNetworkAvailable) {

throw new PaymentNetworkException("Network issue while processing the bank transfer.");

System.out.println("Bank transfer payment processed successfully.");

}
}

// Main class

public class PaymentSystem {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

PaymentMethod paymentMethod = null;

System.out.println("Choose a payment method: 1 for Credit Card, 2 for PayPal, 3 for Bank
Transfer");

int choice = scanner.nextInt();

switch (choice) {

case 1:

paymentMethod = new CreditCard();

break;

case 2:

paymentMethod = new PayPal();

break;

case 3:

paymentMethod = new BankTransfer();

break;

default:

System.out.println("Invalid choice.");

return;

System.out.println("Enter the amount to be paid:");

double amount = scanner.nextDouble();


boolean paymentSuccessful = false;

while (!paymentSuccessful) {

try {

paymentMethod.processPayment(amount);

paymentSuccessful = true;

System.out.println("Payment successful!");

} catch (InvalidCardException | InsufficientFundsException | PaymentNetworkException e) {

System.out.println("Payment failed: " + e.getMessage());

System.out.println("Retrying payment...");

// Optionally, you can allow the user to select a different payment method here

// and update the paymentMethod variable accordingly

scanner.close();

You might also like