Untitled
unknown
plain_text
4 years ago
4.2 kB
16
Indexable
ANSWER :
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Scanner;
public class Banking {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Menu: \nEnter 1 for Bank Manager \nEnter 2 for Bank Customer");
int choice = sc.nextInt();
switch(choice){
case 1:
System.out.println("Enter 1 to add info \nEnter 2 to display info");
int infoChoice = sc.nextInt();
sc.nextLine();
while(infoChoice==1 || infoChoice ==2 ){
if(infoChoice == 1){
System.out.println("Enter Customer ID:");
int customerID = sc.nextInt();
sc.nextLine();
System.out.println("Enter Customer Name:");
String name = sc.nextLine();
System.out.println("Enter email address:");
String email = sc.nextLine();
addCustomerInfo(customerID, name, email);
}
else if(infoChoice == 2){
displayCustomerInfo();
}
else {
System.out.println("Exiting!");
}
System.out.println("Enter 1 to add info \nEnter 2 to display info or any other digit to exit:");
infoChoice = sc.nextInt();
sc.nextLine();
}
break;
case 2:
System.out.println("Enter your customerID");
int id = sc. nextInt();
sc.nextLine();
BankCustomer customer = new BankCustomer(id);
System.out.println("Enter 1 to deposit amount \nEnter 2 to withdraw \nEnter 3 to digit interest earned");
int customerChoice = sc.nextInt();
sc.nextLine();
while(customerChoice >=1 || customerChoice <= 3){
if(customerChoice == 1){
System.out.println("Enter amount to be deposited:");
double depositAmount = sc.nextDouble();
sc.nextLine();
customer.makeDeposit(depositAmount);
}
else if(customerChoice == 2){
System.out.println("Enter amount to be withdrawn:");
double withdraw = sc.nextDouble();
sc.nextLine();
customer.withdrawMoney(withdraw);
}
else if(customerChoice ==3){
System.out.println("Enter accountID:");
int accountID = sc.nextInt();
sc.nextLine();
System.out.println("Interest Earned:" + customer.interestEarned(accountID));
}
else {
System.out.println("Exiting!");
}
System.out.println("Enter 1 to deposit amount \nEnter 2 to withdraw \nEnter 3 to check interest earned or any other key to exit:");
customerChoice = sc.nextInt();
sc.nextLine();
}
break;
default:
System.out.println("Wrong Choice!");
}
}
public static void addCustomerInfo(int customerID, String name, String email){
try{
//Creating the File object
FileOutputStream fos=new FileOutputStream("BankCustInfo.txt",true);
String info = "\n"+ String.valueOf(customerID) + "\t" + name + "\t" + email;
byte[] b = info.getBytes();
fos.write(b);
fos.close();
System.out.print("Information added.");
}
catch(Exception e){
e.printStackTrace();
}
}
public static void displayCustomerInfo(){
try{
//Creating the File object
InputStream inputStream = new FileInputStream("BankCustInfo.txt");
Scanner sc = new Scanner(inputStream);
//StringBuffer to store the contents
StringBuffer sb = new StringBuffer();
while(sc.hasNext()) {
sb.append(sc.nextLine()+"\n");
}
System.out.println(sb);
}
catch(Exception e){
e.printStackTrace();
}
}
}
class BankCustomer{
int customerID;
double balance=0.0;
BankCustomer(int customerID)
{
this.customerID = customerID;
}
public void makeDeposit(double depositAmount){
balance = balance + depositAmount;
displayCurrentBalance();
}
public void withdrawMoney(double withdrawAmount){
if(withdrawAmount > balance)
System.out.println("Insufficient Balance");
else
balance = balance - withdrawAmount;
displayCurrentBalance();
}
public void displayCurrentBalance(){
System.out.println("Current Balance: "+ balance);
}
public double interestEarned(int accountID){
double interest;
int time = 1; // you can change time (given in years)
AccountType accountTyp= new AccountType(accountID);
interest = (balance * accountTyp.interestRate * time) / 100 ;
return interest;
}
static class AccountType{
int accountID;
double interestRate;
String accType;
public AccountType(int accountID){
this.accountID = accountID;
if(accountID==1){
accType = "Checking Account";
interestRate = 0.01;
}
else if(accountID == 2){
accType ="Savings Account";
interestRate = 0.02;
}
}
}
}
Editor is loading...