import java.util.Scanner;
class BankAccount {
String name;
String type;
long number;
long balance;
static float rateOfInterest = 3;
BankAccount() {
this.name = "NIL";
this.type = "NIL";
this.number = 0;
this.balance = 0;
}
BankAccount(String name, String type, long number, long balance) {
this.name = name;
this.type = type;
this.number = number;
this.balance = balance;
}
void deposit(long amount) {
this.balance += amount;
}
boolean withdraw(long amount) {
if (balance - amount <= 10000) {
return false;
} else {
balance -= amount;
return true;
}
}
void display() {
System.out.println();
System.out.println("Name: " + this.name);
System.out.println("Account Type: " + this.type);
System.out.println("Account Number: " + this.number);
System.out.println("Account Balance: " + this.balance);
System.out.println();
}
static void rateOfInterest() {
System.out.println("The current rate of interest is: " + rateOfInterest);
}
}
public class AccountInput {
public static void main(String[] args) {
String name;
String type;
long number;
long balance;
Scanner scanner = new Scanner(System.in);
String choice = null;
System.out.println("Enter account holder name: ");
name = scanner.nextLine();
System.out.println("Enter account type (Savings/FD/Joint): ");
type = scanner.nextLine();
System.out.println("Enter account number: ");
number = scanner.nextLong();
System.out.println("Enter balance amount: ");
balance = scanner.nextLong();
BankAccount bankAccount = new BankAccount(name, type, number, balance);
do {
System.out.print("--- STACK OPERATIONS ---");
System.out.print("\n1. Deposit Money");
System.out.print("\n2. Withdraw Money");
System.out.print("\n3. Display Account Info");
System.out.print("\n4. Show Rate of Interest.");
System.out.print("\n\nYour input: ");
int input = scanner.nextInt();
switch (input) {
case 1:
System.out.println("Enter the amount to deposit: ");
bankAccount.deposit(scanner.nextLong());
bankAccount.display();
break;
case 2:
System.out.println("Enter the amount to withdraw: ");
if (!bankAccount.withdraw(scanner.nextLong())) {
System.out.println("Amount exceeds minimum balance!");
} else {
bankAccount.display();
}
break;
case 3:
bankAccount.display();
break;
case 4:
BankAccount.rateOfInterest();
default:
System.out.println("Invalid input.");
}
System.out.println("Do you want to continue? [y/N]");
choice = scanner.next();
} while (choice.equals("y"));
scanner.close();
}
}
Counter
class Counter {
static int count = 0;
Counter() {
System.out.println("Counter object created.");
count++;
}
static int showCount() {
return count;
}
}
public class CounterInput {
public static void main(String[] args) {
Counter counter1 = new Counter();
Counter counter2 = new Counter();
Counter counter3 = new Counter();
Counter counter4 = new Counter();
System.out.println("The number of objects is: " + Counter.showCount());
}
}