Name: Nachiket Kale
UID: 2024800052
Experiment No. 2a
AIM: Write a program using constructor
Program 1
PROBLEM A program to simulate a simple banking system in which the initial
STATEMENT : balance and rate of interest are read from the keyboard and these
values are initialised using the constructor member function. The
program consists of the following methods:
● To make deposit
● To withdraw an amount for the balance
● To find compound interest based on the rate of interest. (No. of
times and years shd be taken as an input)
● To know the balance amount
● To display the menu options
import java.util.Scanner;
PROGRAM:
class Customer {
String name;
int acc;
String mode;
int bal;
public Customer() {
this.name = "Default Name";
this.acc = 0;
this.mode = "Saving";
this.bal = 500;
}
public Customer(String name, int acc, String mode, int bal) {
this.name = name;
this.acc = acc;
this.mode = mode;
this.bal = bal;
}
public Customer(Customer customer) {
this.name = customer.name;
this.acc = customer.acc;
this.mode = customer.mode;
this.bal = customer.bal;
}
}
class Options {
public void showOptions(Customer customer, Scanner sc) {
int choice;
do {
System.out.println("\nTo deposit an amount, PRESS 1");
System.out.println("To withdraw an amount after checking the balance, PRESS
2");
System.out.println("To display compound interest, PRESS 3");
System.out.println("To exit, PRESS 0");
choice = sc.nextInt();
switch (choice) {
case 1: {
int amt;
System.out.println("Enter the amount to be deposited:");
amt = sc.nextInt();
customer.bal = amt + customer.bal;
System.out.println("New balance after deposit: " + customer.bal);
break;
}
case 2: {
int drawn;
System.out.println("Enter the amount to be withdrawn:");
drawn = sc.nextInt();
if (drawn <= customer.bal) {
customer.bal = customer.bal - drawn;
System.out.println("New balance after withdrawal: " +
customer.bal);
} else {
System.out.println("Insufficient balance.");
}
break;
}
case 3: {
System.out.println("Displaying compound interest");
double amount, t, y, r;
System.out.println("Enter the number of times interest applied per
time period:");
t = sc.nextInt();
System.out.println("Enter number of years:");
y = sc.nextInt();
System.out.println("Enter rate of interest (in percentage):");
r = sc.nextInt();
amount = customer.bal * Math.pow((1 + r / (100 * t)), t * y);
System.out.println("Amount after compound interest: " + amount);
break;
}
case 0: {
System.out.println("Exiting the system.");
break;
}
default:
System.out.println("Invalid option. Please try again.");
}
} while (choice != 0);
}
}
public class BankSyste {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Customer customer1 = new Customer();
System.out.println("Default Constructor");
System.out.println("Account Holder: " + customer1.name);
System.out.println("Account Number: " + customer1.acc);
System.out.println("Account Mode: " + customer1.mode);
System.out.println("Balance: " + customer1.bal);
Options options = new Options();
options.showOptions(customer1, sc);
System.out.println("\nParameterized Constructor");
Customer customer2 = new Customer("John Doe", 12345, "Checking", 1000);
System.out.println("Account Holder: " + customer2.name);
System.out.println("Account Number: " + customer2.acc);
System.out.println("Account Mode: " + customer2.mode);
System.out.println("Balance: " + customer2.bal);
options.showOptions(customer2, sc);
Customer customer3 = new Customer(customer2);
System.out.println("\nCopy Constructor");
System.out.println("Account Holder: " + customer3.name);
System.out.println("Account Number: " + customer3.acc);
System.out.println("Account Mode: " + customer3.mode);
System.out.println("Balance: " + customer3.bal);
options.showOptions(customer3, sc);
}
}
RESULT:
The program successfully simulates a simple banking system using a constructor to initialize
the initial balance and interest rate. It provides essential banking functionalities like
depositing money, withdrawing funds, checking balance, and calculating compound interest
based on user input. The menu-driven approach ensures ease of use, making the program
interactive and user-friendly. This implementation demonstrates the use of constructors,
encapsulation, and basic banking operations in an efficient manner.
Parameterised constructor:
CONCLUSION: