Uploaded by ayesha981606

Assignment-2 OOP JAVA

advertisement
COMSATS University Islamabad, Lahore Campus
Course Title:
Course Instructor/s:
Topic
Out Date:
Student’s Name:
Spring 2021 – Assignment
Object Oriented Programing Course Code:
CSC141
Dr. Allah Bux Sargana
Program Name: BSCS
Assignment-2
Due Date:
11th June 2021
Reg. No.
Important Instructions:
1. Zero tolerance for plagiarism: Plagiarism from any sources including, internet
sources and your fellow students, will result in ZERO marks.
2. Submission requirements:
a. Start page(s): this sheet
b. Code
c. Screenshots of results (Input & output)
3. Late submission policy: deduction @ of 20% of total marks per day
Question:
Assume that a bank maintains two kinds of accounts for customers: a savings account
and the other as a current account. The savings account provides compound interest and
withdrawal facilities but no cheque book facility. The current account provides a
chequebook facility but no interest. Current account holders should also maintain a
minimum balance, and if the balance falls below this level, a service charge is imposed.
Create a class account that stores customers name, account number and type of
account. From this derives the class cur_acct and sav_acct from making them more
specific to their requirements. Include necessary constructors and member functions to
achieve the following tasks: A menu should be displayed to the user to perform the
following tasks.
a)
b)
c)
d)
e)
Accept deposit from a customer and update the balance.
Display the balance.
Compute and deposit interest.
Permit withdrawal and update the balance.
Check for the minimum balance, impose a penalty, necessary, and update the
balance.
Create two files, one for current account holders and the other for saving account holders,
to store the data permanently. All the information regarding account holders should be
stored in these files. When required to display information regarding an account holder,
retrieve the information from the file and display it.
Moreover, apply the validations on all fields; for example, the balance must be a non-zero
and numeric value. The customer's name must be a text; the account number can be
alphanumeric with a specified format etc.
Code:
package account;
public abstract class Account {
private String name;
protected float balance;
private int accNo;
public Account() {
this.name = "Smith";
this.balance = 0.0f;
this.accNo = (int) (Math.random() * (9001)) + 1000;
}
public Account(String name) {
this.name = name;
this.balance = 0.0f;
this.accNo = (int) (Math.random() * (9001)) + 1000;
}
public void display() {
System.out.println("Account Number :" + accNo + " Name :" + name
+ " Balance :$" + balance);
}
public void deposit(float m) {
this.balance += m;
}
public abstract void withdraw(float m);
public float getbalance() {
return balance;
}
public String getUserName() {
return name;
}
}
package account;
public class Cheque extends Account {
private static final float minimBalance=1000;
private static final float overLimitCharge=5;
public Cheque() {
super();
}
public Cheque(String name) {
super(name);
}
@Override
public void withdraw(float m) {
if(getbalance()-m>=minimBalance)
{
super.balance-=m;
}
else if(getbalance()-m>=0)
{
super.balance-=(m+overLimitCharge);
}
else
{
System.out.println("* Insufficient Balance *");
}
}
}
package account;
public class Saving extends Account {
private static final float eachTimeCharge = 3.9f;
public Saving() {
super();
}
public Saving(String name) {
super(name);
}
@Override
public void withdraw(float m) {
if (getbalance() - (m + eachTimeCharge) >= 0) {
balance -= (m + eachTimeCharge);
}
}
}
package account;
import java.util.ArrayList;
public class Bank {
private String bankName;
private ArrayList<Account> accounts = null;
public Bank() {
this.accounts = new ArrayList<Account>();
}
Bank(String name) {
this.accounts = new ArrayList<Account>();
this.bankName=name;
}
public void add(Account a)
{
this.accounts.add(a);
}
public void display()
{
System.out.println("Bank Name :"+bankName);
for(int i=0;i<accounts.size();i++)
{
if(accounts.get(i) instanceof Saving)
{
System.out.print("Savings :");
(accounts.get(i)).display();
}
else
{
System.out.print("Checking :");
accounts.get(i).display();
}
}
}
public void display(String name)
{
System.out.println("Bank Name :"+name);
for(int i=0;i<accounts.size();i++)
{
if(accounts.get(i) instanceof Saving)
{
System.out.print("Savings :");
(accounts.get(i)).display();
}
else
{
System.out.print("Checking :");
accounts.get(i).display();
}
}
}
}
public class Test {
public static void main(String[] args) {
//Creating an instance of Savings class
Saving s=new Saving("John");
//Performing deposit operation
s.deposit(5000);
s.display();
//Performing withdraw operation
s.withdraw(2000);
s.display();
//Creating an instance of Checking class
Cheque c=new Cheque("John");
//Performing deposit operation
c.deposit(3000);
c.display();
//Performing withdraw operation
c.withdraw(1000);
c.display();
Bank b=new Bank("CIBC");
Saving s1=new Saving("John");
s1.deposit(5000);
Cheque c1=new Cheque("John");
c1.deposit(3000);
b.add(s1);
b.add(c1);
b.display();
b.display("HDFC");
}
}
Output Screenshots:
Download