| | |
how to call and search an array of one class in other class ?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 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 :
and now the bank class where all methods are called ...
plz anyone ! help me .... i have been trying it myself for long tym .
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 :
java Syntax (Toggle Plain Text)
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 ...
java Syntax (Toggle Plain Text)
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; } } } }
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:
Then at the main, once you have the number of accounts:
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.
Also at your main, don't do that:
Do this:
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.
You have the balance in your class. It should take only one argument, the amount to deduct:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
// After creating an array of desired number of accounts int num = .... Account [] accounts = new Account[num];
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.
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
for(int i=0;i<acc.length;i++) { .... }
// 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
![]() |
Similar Threads
- sort and search in array of objects (C++)
- accessing array declared within a class (C++)
- [how to]Call functions of one Class from another class (C#)
- how to pass array of value from one calss to another class (VB.NET)
- Initialise array of objects in a class (C++)
- pass an interger from one class to others class (Java)
- class within a class (Java)
- Sorting int array with stack class (C++)
- Java Class Class problem (Java)
- Class in a class... (Java)
Other Threads in the Java Forum
- Previous Thread: What "race condition" is?
- Next Thread: Incompatible types with generics and an inner class.
| Thread Tools | Search this Thread |
.net 6 ajax apple applet array automation bidirectional binary blackberry button buyouts c++ class classes code collections component constructor convert database design detection development dragging eclipse error event firefox forms fractal froglogic functions g2one game google gui html hyper idea image input integer intellijidea8 java javafx javascript jetbrains jni jobs jsp julia key linux loop macosx method methods microsoft mysql netbeans newbie nextline nodes open-source openjavafx oracle oriented parsing php platform problem programming project python radio rails reference ria search service set smash socket software sort string sun swing swt test threads time tree update user web websites whileloop windows







