In: Computer Science
LAB2 PART 1
FOR THE DATA TYPE CLASS:
If you do not have UML of class Account_yourLastName, you should do
that first
Basesd on the UML, provide the code of data type class Account_yourLastName to hold the information of an account with account number (String), name (String), balance (double), with no-argument constructor, parameter constructor
Also, define some methods to handle the tasks: open account, check current balance, deposit, withdraw, and print monthly statement. At the end of each task we have the output as below:
Task OPEN ACCOUNT
For example: account number is 1567794657, name Mary Lane, and start balance is 500
-display the output:
OPEN NEW ACCOUNT – JAMES SMITH BANK
Account Number: 1567794657
Name:Mary Lane
Balance:500.0
Task CHECK CURENT BALANCE - Display the output:
CHECK CURRENT BALANCE – JAMES SMITH BANK
Account Number: 1567794657
Name:Mary Lane
Current Balance:500.0
Task DEPOSIT -If deposit amount is 200 to above account, display the output:
DEPOSIT – JAMES SMITH BANK
Account Number: 1567794657
Name:Mary Lane
Balance:500.0
Deposit amount: 200.0
New Balance:700.0
Task WITHDRAW -If withdraw amount is 300, display the output:
WITHDRAW – JAMES SMITH BANK
Account Number: 1567794657
Name:Mary Lane
Balance:500.0
Withdraw: 300.00
New Balance: 400.0
Task PRINT MONTHLY STATEMENT - dIsplay the following output:
MONTHLY STATEMENT – JAMES SMITH BANK
Account Number: 1567794657
Name:Mary Lane
End Balance:700.0
FOR THE DRIVER CLASS: provide the pseudo-code or flowchart then write the code for the BankService_yourLastName to provide the application of a bank service. First, provide the following menu of tasks:
BankService_Smith.java
MAIN MENU – JAMES SMITH BANK
Open new account
Check current balance
Deposit
Withdraw
Print monthly statement
0. Exit
If users select other task before opening account then, the program will display message box:“You should open account first”
Task 1: Open new account:
-After reading all information of the account entered from the
keyboard to create an account-Create the object of the class
Account
-then display the output by calling the method Create New
Account
Task 2: Check current balance
-call the method Check Current Balance from data type class to
display the output
Task 3: Deposit
-ask users for depoit amount, call the method deposit from data
type class to display the output
Task 4: Withdraw
-ask users for withdraw amount, call the method withdraw of class
Account_yourLastName
Task 5: Print monthly statement -print the monthly statement
EXIT
When users exit display the message box:
“Thank you. The application is terminating..”
Requirement:
-Get the output of the task Monthly Statement then paste it in the
file with pseudo-code -write the comment on each part in both
classes
COMPILE AND RUN THE PART1 TO GET THE OUTPUT
// Account.java
public class Account {
// data fields
private String account_number;
private String name;
private double balance;
// default constructor
public Account()
{
account_number = null;
name = null;
balance = 0;
}
// parameterized constructor
public Account(String account_number, String name, double balance )
{
this.account_number = account_number;
this.name = name;
this.balance = balance;
}
// method to print the details of the new account created
public void create_new_account()
{
System.out.println("Account Number: "+account_number);
System.out.println("Name: "+name);
System.out.println("Balance: "+balance);
}
// method to print the details with the current balance
public void check_current_balance()
{
System.out.println("Account Number: "+account_number);
System.out.println("Name: "+name);
System.out.println("Current Balance: "+balance);
}
// method to deposit an amount in the account and print the details after depositing
public void deposit(double amount)
{
// check if amount is non-negative
if(amount >= 0)
{
System.out.println("Account Number: "+account_number);
System.out.println("Name: "+name);
System.out.println("Balance: "+balance);
System.out.println("Deposit amount: "+amount);
balance += amount;
System.out.println("New Balance: "+balance);
}
else
System.out.println("Deposit Amount cannot be negative");
}
// method to withdraw the amount from the account and display the details after withdrawing
public void withdraw(double amount)
{
// check if amount is non-negative and there is enough balance
if(amount >=0 && amount <=balance)
{
System.out.println("Account Number: "+account_number);
System.out.println("Name: "+name);
System.out.println("Balance: "+balance);
System.out.println("Withdraw: "+amount);
balance -= amount;
System.out.println("New Balance: "+balance);
}
else
System.out.println("Withdraw Amount cannot be negative or greater than current balance");
}
// method to print monthly statement of the account
public void print_monthly_statement()
{
System.out.println("Account Number: "+account_number);
System.out.println("Name: "+name);
System.out.println("End Balance: "+balance);
}
}
//end of Account.java
// BankService.java
import java.util.Scanner;
public class BankService {
public static void main(String[] args) {
Account account = null; // create a reference of Account class
int choice;
Scanner scan = new Scanner(System.in);
String name, account_number;
double balance, amount;
// loop that continues till the user exits
do
{
System.out.println("MAIN MENU – JAMES SMITH BANK");
System.out.println("1. Open new account");
System.out.println("2. Check current balance");
System.out.println("3. Deposit");
System.out.println("4. Withdraw");
System.out.println("5. Print monthly statement");
System.out.println("0. Exit");
// input fo choice
System.out.print("Enter your choice(0-5) : ");
choice = scan.nextInt();
scan.nextLine();
System.out.println();
switch(choice)
{
case 1: // create a new account
System.out.println("OPEN NEW ACCOUNT – JAMES SMITH BANK");
System.out.print("Enter Account Number : ");
account_number = scan.nextLine();
System.out.print("Enter Name : ");
name = scan.nextLine();
System.out.print("Enter Balance : ");
balance = scan.nextDouble();
account = new Account(account_number, name, balance);
account.create_new_account();
break;
case 2: // check the current balance
System.out.println("CHECK CURRENT BALANCE – JAMES SMITH BANK");
if(account != null) // check if account is created
account.check_current_balance();
else
System.out.println("You should open account first");
break;
case 3: // deposit an amount in the account
System.out.println("DEPOSIT – JAMES SMITH BANK");
if(account != null) // check if account is created
{
System.out.print("Enter deposit amount : ");
amount = scan.nextDouble();
scan.nextLine();
account.deposit(amount);
}else
System.out.println("You should open account first");
break;
case 4: // withdraw an amount from the account
System.out.println("WITHDRAW – JAMES SMITH BANK");
if(account != null) // check if account is created
{
System.out.print("Enter withdraw amount : ");
amount = scan.nextDouble();
scan.nextLine();
account.withdraw(amount);
}else
System.out.println("You should open account first");
break;
case 5: // display the monthly statement of the account
System.out.println("MONTHLY STATEMENT – JAMES SMITH BANK");
if(account != null) // check if account is created
account.print_monthly_statement();
else
System.out.println("You should open account first");
break;
case 0: // exit
break;
default: // wrong choice
System.out.println("Wrong choice");
}
System.out.println();
}while(choice != 0);
// print the terminating message
System.out.println("Thank you. The application is terminating..");
scan.close();
}
}
//end of BankService.java
Output: