-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package IPA50; | ||
import java.sql.Timestamp; | ||
import java.text.SimpleDateFormat; | ||
import java.util.*; | ||
public class Answer { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
BankAccount[] ac = new BankAccount[2]; | ||
for (int i = 0; i < ac.length; i++) { | ||
System.out.println("Account "+(i+1)+":"); | ||
System.out.print("Account Number: "); | ||
String a = sc.nextLine(); | ||
System.out.print("Account Holder Name: "); | ||
String b = sc.nextLine(); | ||
System.out.print("Balance: "); | ||
double c = sc.nextDouble();sc.nextLine(); | ||
ac[i] = new BankAccount(a,b,c); | ||
System.out.println(); | ||
} | ||
System.out.println("Transfer Details:"); | ||
System.out.print("Amount: "); | ||
double amount = sc.nextDouble();sc.nextLine(); | ||
System.out.print("Transaction Code: "); | ||
String tran = sc.nextLine(); | ||
System.out.println(); | ||
BankUtils bu = new BankUtils(ac[0], ac[1], amount, tran); | ||
System.out.println("Before Transfer:"); | ||
for (int i = 0; i < ac.length; i++) | ||
{ | ||
System.out.println("Account "+(i+1)+": "+ac[i].getAcname()+" - "+ac[i].getAcno()+" - "+ac[i].getBalance()); | ||
} | ||
double[] ans = transferFunds(bu); | ||
Timestamp timestamp = new Timestamp(System.currentTimeMillis()); | ||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
String fTimestamp = sdf.format(timestamp); | ||
Transaction t = new Transaction(tran,amount,fTimestamp); | ||
System.out.println(); | ||
|
||
if(ans!=null) | ||
{ | ||
System.out.println("After Transfer:"); | ||
for (int i = 0; i < ans.length; i++) | ||
{ | ||
System.out.println("Account "+(i+1)+": "+ac[i].getAcname()+" - "+ac[i].getAcno()+" - "+ans[i]); | ||
} | ||
System.out.println(); | ||
System.out.println("Transaction Details:"); | ||
System.out.println("Transaction Code: "+t.getTransactionCode()); | ||
System.out.println("Amount Transferred: "+t.getAmount()); | ||
System.out.println("Timestamp: "+t.getTimestamp()); | ||
} | ||
else | ||
{ | ||
System.out.println("Insufficient Balance in Account 1"); | ||
System.out.println("Transaction Code: "+t.getTransactionCode()); | ||
System.out.println("Timestamp: "+t.getTimestamp()); | ||
} | ||
|
||
} | ||
public static double[] transferFunds(BankUtils b) | ||
{ | ||
if(b.fromAccount.getBalance()>b.getAmount()) | ||
{ | ||
double amount1 = b.fromAccount.getBalance()-b.getAmount(); | ||
double amount2 = b.toAccount.getBalance()+b.getAmount(); | ||
double[] amount = {amount1,amount2}; | ||
return amount; | ||
} | ||
return null; | ||
} | ||
} | ||
class BankAccount | ||
{ | ||
private String acno; | ||
private String acname; | ||
private double balance; | ||
public BankAccount(String acno, String acname, double balance) { | ||
this.acno = acno; | ||
this.acname = acname; | ||
this.balance = balance; | ||
} | ||
public String getAcno() { | ||
return acno; | ||
} | ||
public void setAcno(String acno) { | ||
this.acno = acno; | ||
} | ||
public String getAcname() { | ||
return acname; | ||
} | ||
public void setAcname(String acname) { | ||
this.acname = acname; | ||
} | ||
public double getBalance() { | ||
return balance; | ||
} | ||
public void setBalance(double balance) { | ||
this.balance = balance; | ||
} | ||
} | ||
class BankUtils | ||
{ | ||
BankAccount fromAccount; | ||
BankAccount toAccount; | ||
double amount; | ||
String transactionCode; | ||
public BankUtils(BankAccount fromAccount, BankAccount toAccount, double amount, String transactionCode) { | ||
this.fromAccount = fromAccount; | ||
this.toAccount = toAccount; | ||
this.amount = amount; | ||
this.transactionCode = transactionCode; | ||
} | ||
public double getAmount() { | ||
return amount; | ||
} | ||
public void setAmount(double amount) { | ||
this.amount = amount; | ||
} | ||
public String getTransactionCode() { | ||
return transactionCode; | ||
} | ||
public void setTransactionCode(String transactionCode) { | ||
this.transactionCode = transactionCode; | ||
} | ||
} | ||
class Transaction | ||
{ | ||
String transactionCode; | ||
double amount; | ||
String timestamp; | ||
public Transaction(String transactionCode, double amount, String timestamp) { | ||
this.transactionCode = transactionCode; | ||
this.amount = amount; | ||
this.timestamp = timestamp; | ||
} | ||
public String getTransactionCode() { | ||
return transactionCode; | ||
} | ||
public double getAmount() { | ||
return amount; | ||
} | ||
public String getTimestamp() { | ||
return timestamp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
Create a class called BankAccount with the following attributes: | ||
|
||
accountNumber (String) | ||
accountHolderName (String) | ||
balance (double) | ||
Write getters, setters, and a parameterized constructor for the class. | ||
|
||
Create a static method transferFunds in a separate class called BankUtils. The method should take four parameters: | ||
|
||
fromAccount (BankAccount) | ||
toAccount (BankAccount) | ||
amount (double) | ||
transactionCode (String) | ||
The method should transfer the specified amount from the fromAccount to the toAccount, | ||
and return a Transaction object containing the transactionCode, the amount | ||
transferred, and the timestamp of the transaction. If the fromAccount has insufficient | ||
balance, the method should throw an InsufficientBalanceException with an appropriate error message. | ||
|
||
Create a class called Transaction with the following attributes: | ||
|
||
transactionCode (String) | ||
amount (double) | ||
timestamp (String) | ||
Write getters and a parameterized constructor for the class. | ||
|
||
Create an InsufficientBalanceException class that extends the Exception class. | ||
The exception should have a constructor that takes a message as a parameter. | ||
|
||
In the main method, create two BankAccount objects, transfer some funds between | ||
them using the transferFunds method, and print the transaction details to the console. Handle any exceptions. | ||
|
||
Sample Input: | ||
|
||
Account 1: | ||
Account Number: 12345 | ||
Account Holder Name: John | ||
Balance: 5000.0 | ||
|
||
Account 2: | ||
Account Number: 67890 | ||
Account Holder Name: Jane | ||
Balance: 10000.0 | ||
|
||
Transfer Details: | ||
Amount: 2000.0 | ||
Transaction Code: T0001 | ||
|
||
Sample Output: | ||
|
||
Before Transfer: | ||
Account 1: John - 12345 - 5000.0 | ||
Account 2: Jane - 67890 - 10000.0 | ||
|
||
After Transfer: | ||
Account 1: John - 12345 - 3000.0 | ||
Account 2: Jane - 67890 - 12000.0 | ||
|
||
Transaction Details: | ||
Transaction Code: T0001 | ||
Amount Transferred: 2000.0 | ||
Timestamp: 2023-04-01 10:30:00 |