0

1. Create a class Account that has following instance variables:
a. private account number (string)
b. private account holder name (string)
c. private balance (floating-point)
d. private account opening date (string)
e. profitRate (final, floating-point): 5% for all accounts

Supply a default constructor, a parameterized constructor, and a clone constructor. Write a setter and a getter for each private instance variable. Also include following methods:
a. withdraw: deducts a specified amount from balance
b. deposit: adds a specified amount to the balance
c. accountStatement: displays all information of an account
d. creditProfit: calculates the profit using profitRate and updates account balance

Account class should also keep track of number of accounts that have been opened at any given time.

Write another class Bank, which acts as your main class. Inside main method, create a one-dimensional array of objects of Account type. At the start of your program, it should prompt for total number of accounts to be created. After creating an array of desired number of accounts, your program shall provide a console-based menu to user with following options:
1) Open an Account (required input: values for initialization of instance variables of new account object/instance)
2) Withdraw an Amount (required input: account number, amount to withdraw)
3) Deposit an Amount (required input: account number, amount to deposit)
4) Account Statement (required input: account number)
5) Credit Profit (required input: account number)
6) Total Number of Accounts Opened
7) Exit Program
and so far i did this .....
Account class :

class Account{
private String accountNumber;
private String accountHolder;
private float balance;
private String accountOpeningDate;
final float PROFIT_RATE = 0.5f ;
static int count=0;
Account(){
accountNumber = " " ;
accountHolder = " " ;
balance = 0.0f ;
accountOpeningDate = " " ;
count++;
}
Account(String a,String b, String c, Float d){
accountNumber = a ;
accountHolder = b ;
balance = d ;
accountOpeningDate = c ;
count++;
}
Account(Account ac)
{
accountNumber = ac.accountNumber ;
accountHolder = ac.accountHolder ;
balance = ac.balance ;
accountOpeningDate = ac.accountOpeningDate ;
}
void setaccountNumber(String acn){
Math.random();
accountNumber=acn;
}
void setaccountHolder(String ah)
{
accountHolder=ah;
}
void setbalance(float bal)
{
balance=bal;
}
void setaccountOpeningDate(String aod)
{
accountOpeningDate=aod;
}
String getaccountNumber()
{
return accountNumber;
}
String getaccountHolder()
{
return accountHolder;
}
String getaccountOpeningDate()
{
return accountOpeningDate;
}
float getbalance()
{
return balance;
}
void openAcc(String n,String d,String num,float bal)
{
setaccountHolder(n);
setaccountOpeningDate(d);
setaccountNumber(num);
setbalance(bal);
}void withDraw(String num,float bal)
{
[B]Account acc[]=new Account[count];
for(int i=0;i<count;i++){
acc[i]=new Account();
acc[i].getaccountNumber();
[/B]
}
}
}

and now the bank class where all methods are called ...

import javax.swing.JOptionPane;
class Bank{
public static void main(String []args){

String name,number,date;

float bal,profit;

int num=Integer.parseInt(JOptionPane.showInputDialog("Enter"));
Account acc[]=new Account[num];
for(int i=0;i<acc.length;i++)
{
acc[i]=new Account();

System.out.println(Account.count);

System.out.println("What do you want to do ? ");
System.out.println("Open an Account (1)");
System.out.println("WithDraw Amount (2)");
System.out.println("Deposit Amount (3)");
System.out.println("Account Statement (4)");
System.out.println("Credit Profit (5)");
System.out.println("Total Number of Accounts (6)");
System.out.println("Exit (7)");

int ch=Integer.parseInt(JOptionPane.showInputDialog("Enter your choice :"));
switch(ch)
{
case 1:
name=JOptionPane.showInputDialog("Enter your name:");
date=JOptionPane.showInputDialog("Enter Date:");
number=JOptionPane.showInputDialog("Enter Account Number:");
bal=Float.parseFloat(JOptionPane.showInputDialog("Deposting Amount:"));
acc[i].openAcc(name,date,number,bal);
System.out.println(acc[i].getaccountNumber());
break;
case 2:
number=JOptionPane.showInputDialog("Enter Account Number:");
bal=Float.parseFloat(JOptionPane.showInputDialog("Withdrawing Amount:"));
acc[i].withDraw(number,bal);
break;
.
.
.
.
.
.
.
case 7:
System.exit(0);
break;
}
}
}
}

plz anyone ! help me .... i have been trying it myself for long tym .

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by javaAddict
0

What is wrong with your withdraw?
You have the balance in your class. It should take only one argument, the amount to deduct:

withdraw(float amount) {
  if (balance >= amount) {
      balance = balance - amount;
  } else {
     System.out.println("Not enough money");
  } 
}

Then at the main, once you have the number of accounts:

// After creating an array of desired number of accounts
int num = ....
Account [] accounts = new Account[num];

There, you are done. This is the array that you need. Not the one you had in the Account class.

2) Withdraw an Amount (required input: account number, amount to withdraw)
After you get these info, loop the array that you have in the main for the account.

String accountNum = "value entered";
float mon = 10; // value entered
for (int i=0;i<Account.count;i++) {
      if (accountNum.equals(accounts[i].getAccountNumber())) {
             accounts[i].withdraw(mon);
   }
}

Also at your main, don't do that:

for(int i=0;i<acc.length;i++)
{
....
}

Do this:

// create the array here!

int choice = 0;
[B]while (choice!=7) {[/B]

System.out.println("What do you want to do ? ");
System.out.println("Open an Account (1)");
System.out.println("WithDraw Amount (2)");
System.out.println("Deposit Amount (3)");
System.out.println("Account Statement (4)");
System.out.println("Credit Profit (5)");
System.out.println("Total Number of Accounts (6)");
System.out.println("Exit (7)");

choice=Integer.parseInt(JOptionPane.showInputDialog("Enter your choice :"));

switch (choice) {
   case 1:
     ....

   case 7:
        System.out.println("Good bye"); 
        break;
   default:
     System.out.println("Invalid choice"); 
     break;
      
}

}

So whenever the user wants to withdraw or add money, you will loop the array until you find the element of the array with the given account number and call the appropriate method.
The loop upper limit will be the Account.count, because that is how many accounts have been created.

You will only call the constructor at option 1 Create account.
You will create the "latest" element of the array. You should of course check the size of the array every time you create a new account.
Note that Account.count tells you how many accounts have been created so that value must not exceed the length of the array.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.