In: Computer Science
JAVA
The previous assignment for the semester project required that you submit a document that included class diagram document that used UML notation. By now (if not sooner), you should have a good idea of what classes you will need to include in your Java Application.You may, in fact, had identified additional classes you need to implement. For this assignment, you are to write your classes that are required for your Java application (minimum of three).
Additionally, you will need to write test classes in order to test out the classes that can be used to test the functionality of your classes. These test classes should be maintained. For example, if you add a new method to one of your classes in a later part of the development of this Java app, you should account for this in your test classes. This will help verify that your classes still work!
The above information is generally what I am working on. In the long run I am making a Banking application complete with GUI, but for this assignment i just need coding for only some classes as stated above. One class named Account is obvious but the other two can just be what goes with the program. I just need it to be set up that the bank can take in the User's name and customer ID, and then that customer can choose to withdraw, deposit, transfer, or check balance. They can also choose to deposit or withdraw from checking or savings. There is also a loan account that they can check balance. Nothing too complex is needed here.
//I have done two classes named Account and TestAccount. Also added the functionality mentioned above. Kindly comment down below if you have any query.
import java.util.*;
public class Account{
private String userName;
private int custId;
private double savingsBalance;
private double loanBalance;
public Account(String userName, int custId, double
savingsBalance, double loanBalance){
this.userName = userName;
this.custId = custId;
this.savingsBalance =
savingsBalance;
this.loanBalance =
loanBalance;
}
public static Account authenticate(Account[] acc,
String userName, int custId){
for(Account a: acc){
if(a.userName.equals(userName) && a.custId == custId)
return a;
}
return null;
}
public void withdraw(double amount){
if(amount <=
savingsBalance){
savingsBalance
-= amount;
System.out.println("Withdraw successful. Current balance:
"+savingsBalance);
}
else{
System.out.println("Withdraw amount exceeds the total balance in
the account");
}
}
public void deposit(double amount){
savingsBalance += amount;
System.out.println("Deposit
successful. Current balance: "+savingsBalance);
}
public void transfer(double amount, Account
obj){
if(amount <=
savingsBalance){
savingsBalance
-= amount;
obj.savingsBalance += amount;
System.out.println("Transfer Successful");
}
else{
System.out.println("Tranfer amount exceeds your account
balance");
}
}
public double getBalance(){
return savingsBalance;
}
public double checkLoanBalance(){
return loanBalance;
}
}
class TestAccount{
public static void main(String[] args){
Account acc[] = new Account[5]; /*
For testing created 5 users*/
acc[0] = new
Account("Vishal",1,100000.00,100.00); /*Initializing user1*/
acc[1] = new
Account("Vicky",2,100000.00,100.00); /*Initializing
user2*/
acc[2] = new
Account("Vikash",3,100000.00,100.00); /*Initializing
user3*/
acc[3] = new
Account("Vishu",4,100000.00,100.00); /*Initializing
user4*/
acc[4] = new Account("Vishal
Sah",5,10000.00,100.00); /*Initializing user5*/
Scanner sc = new
Scanner(System.in);
System.out.println("Enter User
name: ");
String userName =
sc.nextLine();
System.out.println("Enter customer
Id: ");
int id = sc.nextInt();
Account a =
Account.authenticate(acc,userName,id);
if(a!=null){
while(true){
System.out.println("1. Withdraw\n"+
"2. Deposit\n"+
"3. Transfer\n"+
"4. Savings Balance\n"+
"5. Loan Balance\n"+
"6. Logout");
int choice = sc.nextInt();
switch(choice){
case 1:
System.out.println("Enter amount to withdraw: ");
double amount = sc.nextDouble();
a.withdraw(amount);
break;
case 2:
System.out.println("Enter amount to deposit: ");
amount = sc.nextDouble();
a.deposit(amount);
break;
case 3:
System.out.println("Enter customer Id to which you want to
transfer: ");
id = sc.nextInt();
System.out.println("Enter the amount: ");
amount = sc.nextDouble();
a.transfer(amount,acc[id-1]);
break;
case 4:
System.out.println("Current savingds acc balance:
"+a.getBalance());
break;
case 5:
System.out.println("Current loan acc balance:
"+a.getBalance());
break;
case 6: System.exit(0);
default:
System.out.println("Invalid input");
}
}
}
else{
System.out.println("Customer not found.");
}
}
}
//Screenshot of the output is attached below.