Create a Class Name Bank that holds the Banking details of a customer.
Class Bank: Accno, Customer Name, Balance, Displaydata(), Deposit(), Withdrawal()
Create the instance of the Bank Class as Customers. Use the Constructor method to initialize the
Class Members and display methods to display the balance amount and other details.
Write a Program in java to accept the Account number, mode of transaction
(Withdrawn/Deposit), and Amount (Amount must be greater than zero).
If deposit is chosen, the deposit() must be called and the amount must be added to the balance
and
if the withdrawal method is chosen the withdrawal() must be called to modify the balance
amount,
if the amount to be withdrawn is larger than the balance amount then message must be invoked
to prevent the user from withdrawing an amount more than the balance.
// Bank class to store customer details and perform transactions
class Bank {
private int accNo;
private String customerName;
private double balance;
// Constructor to initialize the account
public Bank(int accNo, String customerName, double balance)
{
this.accNo = accNo;
this.customerName = customerName;
this.balance = balance;
}
// Method to display account details
public void displayData() {
System.out.println("Account Number: " + accNo);
System.out.println("Customer Name: " + customerName);
System.out.println("Balance: " + balance);
}
// Method to deposit amount
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Amount deposited successfully. New Balance: " + balance);
//displayData();
} else {
System.out.println("Invalid deposit amount. Amount must be greater than zero.");
}
}
// Method to withdraw amount without using exceptions
public void withdrawal(double amount) {
if (amount > balance) {
System.out.println("Insufficient balance! Withdrawal failed.");
} else if (amount <= 0) {
System.out.println("Invalid withdrawal amount. Amount must be greater than zero.");
} else {
balance -= amount;
System.out.println("Amount withdrawn successfully. New Balance: " + balance);
//displayData();
}
}
}
======================
// Main class to handle user input and perform transactions
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
// Creating a bank account
System.out.print("Enter Account Number: ");
int accNo = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter Customer Name: ");
String customerName = scanner.nextLine();
System.out.print("Enter Initial Balance: ");
double balance = scanner.nextDouble();
Bank customer = new Bank(accNo, customerName, balance);
customer.displayData();
// Transaction menu loop
while (true) {
System.out.print("Enter mode of transaction (Deposit/Withdraw) or 'x' to exit: ");
String mode = scanner.next().toLowerCase();
if (mode.equals("x"))
{
System.out.println("Exiting transaction menu.");
break;
}
System.out.print("Enter Amount: ");
double amount = scanner.nextDouble();
if (mode.equals("d"))
{
customer.deposit(amount);
} else if (mode.equals("w"))
{
customer.withdrawal(amount);
} else {
System.out.println("Invalid transaction mode.");
}
// Display updated details
customer.displayData();
}
scanner.close();
}
}