how to call and search an array of one class in other class ?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 5
Reputation: ahmedshayan is an unknown quantity at this point 
Solved Threads: 0
ahmedshayan ahmedshayan is offline Offline
Newbie Poster

how to call and search an array of one class in other class ?

 
0
  #1
Oct 21st, 2009
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 :
  1. class Account{
  2. private String accountNumber;
  3. private String accountHolder;
  4. private float balance;
  5. private String accountOpeningDate;
  6. final float PROFIT_RATE = 0.5f ;
  7. static int count=0;
  8. Account(){
  9. accountNumber = " " ;
  10. accountHolder = " " ;
  11. balance = 0.0f ;
  12. accountOpeningDate = " " ;
  13. count++;
  14. }
  15. Account(String a,String b, String c, Float d){
  16. accountNumber = a ;
  17. accountHolder = b ;
  18. balance = d ;
  19. accountOpeningDate = c ;
  20. count++;
  21. }
  22. Account(Account ac)
  23. {
  24. accountNumber = ac.accountNumber ;
  25. accountHolder = ac.accountHolder ;
  26. balance = ac.balance ;
  27. accountOpeningDate = ac.accountOpeningDate ;
  28. }
  29. void setaccountNumber(String acn){
  30. Math.random();
  31. accountNumber=acn;
  32. }
  33. void setaccountHolder(String ah)
  34. {
  35. accountHolder=ah;
  36. }
  37. void setbalance(float bal)
  38. {
  39. balance=bal;
  40. }
  41. void setaccountOpeningDate(String aod)
  42. {
  43. accountOpeningDate=aod;
  44. }
  45. String getaccountNumber()
  46. {
  47. return accountNumber;
  48. }
  49. String getaccountHolder()
  50. {
  51. return accountHolder;
  52. }
  53. String getaccountOpeningDate()
  54. {
  55. return accountOpeningDate;
  56. }
  57. float getbalance()
  58. {
  59. return balance;
  60. }
  61. void openAcc(String n,String d,String num,float bal)
  62. {
  63. setaccountHolder(n);
  64. setaccountOpeningDate(d);
  65. setaccountNumber(num);
  66. setbalance(bal);
  67. }void withDraw(String num,float bal)
  68. {
  69. [B]Account acc[]=new Account[count];
  70. for(int i=0;i<count;i++){
  71. acc[i]=new Account();
  72. acc[i].getaccountNumber();
  73. [/B]
  74. }
  75. }
  76. }

and now the bank class where all methods are called ...
  1. import javax.swing.JOptionPane;
  2. class Bank{
  3. public static void main(String []args){
  4.  
  5. String name,number,date;
  6.  
  7. float bal,profit;
  8.  
  9. int num=Integer.parseInt(JOptionPane.showInputDialog("Enter"));
  10. Account acc[]=new Account[num];
  11. for(int i=0;i<acc.length;i++)
  12. {
  13. acc[i]=new Account();
  14.  
  15. System.out.println(Account.count);
  16.  
  17. System.out.println("What do you want to do ? ");
  18. System.out.println("Open an Account (1)");
  19. System.out.println("WithDraw Amount (2)");
  20. System.out.println("Deposit Amount (3)");
  21. System.out.println("Account Statement (4)");
  22. System.out.println("Credit Profit (5)");
  23. System.out.println("Total Number of Accounts (6)");
  24. System.out.println("Exit (7)");
  25.  
  26. int ch=Integer.parseInt(JOptionPane.showInputDialog("Enter your choice :"));
  27. switch(ch)
  28. {
  29. case 1:
  30. name=JOptionPane.showInputDialog("Enter your name:");
  31. date=JOptionPane.showInputDialog("Enter Date:");
  32. number=JOptionPane.showInputDialog("Enter Account Number:");
  33. bal=Float.parseFloat(JOptionPane.showInputDialog("Deposting Amount:"));
  34. acc[i].openAcc(name,date,number,bal);
  35. System.out.println(acc[i].getaccountNumber());
  36. break;
  37. case 2:
  38. number=JOptionPane.showInputDialog("Enter Account Number:");
  39. bal=Float.parseFloat(JOptionPane.showInputDialog("Withdrawing Amount:"));
  40. acc[i].withDraw(number,bal);
  41. break;
  42. .
  43. .
  44. .
  45. .
  46. .
  47. .
  48. .
  49. case 7:
  50. System.exit(0);
  51. break;
  52. }
  53. }
  54. }
  55. }
plz anyone ! help me .... i have been trying it myself for long tym .
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #2
Oct 21st, 2009
What is wrong with your withdraw?
You have the balance in your class. It should take only one argument, the amount to deduct:
  1. withdraw(float amount) {
  2. if (balance >= amount) {
  3. balance = balance - amount;
  4. } else {
  5. System.out.println("Not enough money");
  6. }
  7. }

Then at the main, once you have the number of accounts:
  1. // After creating an array of desired number of accounts
  2. int num = ....
  3. 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.
  1. String accountNum = "value entered";
  2. float mon = 10; // value entered
  3. for (int i=0;i<Account.count;i++) {
  4. if (accountNum.equals(accounts[i].getAccountNumber())) {
  5. accounts[i].withdraw(mon);
  6. }
  7. }

Also at your main, don't do that:
  1. for(int i=0;i<acc.length;i++)
  2. {
  3. ....
  4. }
Do this:
// create the array here!

int choice = 0;
while (choice!=7) {

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.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

Tags
bank, classes, java

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC