LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   inheretance and ArrayList in java ..i want to know the mistake in my code (https://www.linuxquestions.org/questions/programming-9/inheretance-and-arraylist-in-java-i-want-to-know-the-mistake-in-my-code-943410/)

4four 05-05-2012 10:37 AM

inheretance and ArrayList in java ..i want to know the mistake in my code
 
hello every body

i am tring to solve this problem ,but i think there is smth wrong
cna any body help me

Quote:

Write a program client program that deals with different classes:
1. BankAccount class similar to the one in textbook but with additional data member accountName a string.
2. BranchBank class which consists of an arraylist of element of type BankAccount (should be protected) and an IdBranch an integer identifying the branch id. This class should contain the class constructor which reads from a file bank the branch id and the information about the accounts to be created in this branch. This class should have the following methods: searchAccount which takes as a parameter an account name and returns the corresponding BankAccount. addAccount which will add a new account in the branch.

this is my code

dwhitney67 05-05-2012 10:45 AM

Really? I did not see the following in your requirements.
Code:

public class BranchBank extends BankAccount{
Ask yourself if this statement rings true: A BranchBank "is a" BankAccount.

A BranchBank contains one or more BankAccounts... keyword being "contains".


Quote:

Originally Posted by 4four (Post 4671007)
hello every body

i am tring to solve this problem ,but i think there is smth wrong
cna any body help me

If you need help with spelling words, don't hesitate to ask.

4four 05-05-2012 12:54 PM

Quote:

Originally Posted by dwhitney67 (Post 4671012)
Really? I did not see the following in your requirements.
Code:

public class BranchBank extends BankAccount{
Ask yourself if this statement rings true: A BranchBank "is a" BankAccount.

A BranchBank contains one or more BankAccounts... keyword being "contains".



If you need help with spelling words, don't hesitate to ask.

it is not correct but accorrding to this statement
Quote:

BranchBank class which consists of an arraylist of element of type BankAccount (should be protected)
I think that I have to write it like this

dugan 05-05-2012 01:30 PM

I think you misunderstood.

Quote:

BranchBank class which consists of an arraylist of element of type BankAccount (should be protected) and an IdBranch an integer...
Code:

public class BranchBank
{
    // dot dot dot...
    protected ArrayList<BankAccount> accounts;
}


4four 05-05-2012 02:06 PM

Quote:

Originally Posted by dugan (Post 4671129)
I think you misunderstood.



Code:

public class BranchBank
{
    // dot dot dot...
    protected ArrayList<BankAccount> accounts;
}


aha. i want to write like this but it is correct to write this statement
protected ArrayList<BankAccount> accounts;
inside BranchBank without using inheretance

4four 05-05-2012 02:10 PM

now i will modify it .what about the rest of the code?

dugan 05-05-2012 02:10 PM

Quote:

what about the rest of the code?
It's your homework, not ours. We're not going to write the rest of the code for you.

If that's not what you meant then please clarify.

4four 05-05-2012 02:19 PM

thanx

4four 05-05-2012 02:44 PM

Quote:

Originally Posted by dugan (Post 4671149)
It's your homework, not ours. We're not going to write the rest of the code for you.

If that's not what you meant then please clarify.

you misunderstand me. I mean can you help me to correct my mistake.
beause my solution to other tasks will be wrong if these codes are wrong.
I were doing my best.

dugan 05-05-2012 03:27 PM

You could start by wrapping your code in CODE tags rather than QUOTE tags, so that we can read it.

4four 05-05-2012 03:39 PM

.....

dugan 05-05-2012 03:42 PM

Looks like you got it.

4four 05-05-2012 03:44 PM

it is too long problem.I am waiting for you reply.thanks so much

4four 05-05-2012 03:45 PM

Quote:

Originally Posted by dugan (Post 4671177)
Looks like you got it.

What?!:D?

dugan 05-05-2012 03:46 PM

Uhm, yeah, it looks like you've completed the requirements that you posted.

4four 05-05-2012 03:52 PM

1- Is it correct to write the header of each class like what i did?!

2 - I think my first code is correct.Is it correct?!

3- The second code .I think it is all correct except a small problem with return b...i try to add finaly bolck to solve the
porblem .But it is also didn't work...How I can
return the b which is in try bolck ?

4four 05-05-2012 03:53 PM

I am learning java from the zero in this semester only.

4four 05-05-2012 04:38 PM

these are the files that I used to solve


Quote:

To test your program on the following data, which is to be stored in the input data file called bank.txt as follows:

1 Ali 500.0
1 Youssef 1000.0
1 Khaled 20.0
2 Alia 8000.0
2 Nasser 3000.
3 Kamal 200.0
3 Nawaf 400.0
3 Walleed 5000.0
3 Areef 390.0

The operation file:

Withdraw 50.0 1 Khaled
Debit 1000.0 2 Alia
Withdraw 500.0 3 Areef
Debit 100.0 1 Karim


---------- Post added 05-06-12 at 01:39 AM ----------

[/COLOR]I were check the other task

[QUOTE]
5. (4pts) Operations a sub-class of the super class BankName which read from a operation file some operations on different account and perform the necessary operations debitAccount or withdrawAccount.
6. (2pts) The client class should create the BankName and prints all the accounts information in all the branches.
7. (4pts) The client class should perform the operations stored in the operation file (if possible otherwise an error message should be displayed) and then prints all the branches in the screen and also in a new outputbank file. You can add any method to this class if you see it necessary.

dwhitney67 05-06-2012 04:58 AM

From code you posted earlier, the following also seems incorrect:
Code:

public class BankName extends BranchBank
A bank (BankName) consists of zero or more bank branches (BranchBank).

Code:

public class BankName
{
    ...

    ArrayList<BranchBank> branches;
}

The ordering of these statements does not seem to follow the pattern shown in the operations.txt file:
Code:

        //read next string
        operation = file.next( );
        // read next integer
        id1= file.nextInt( );
        //read next string
        name11 = file.next( );
        //read next double
        balance1 = file.nextDouble();

From the file you posted, the order is operation, transaction amount, id, and then name.

4four 05-06-2012 08:27 AM

Quote:

Originally Posted by dwhitney67 (Post 4671509)
From code you posted earlier, the following also seems incorrect:
Code:

public class BankName extends BranchBank
A bank (BankName) consists of zero or more bank branches (BranchBank).

Code:

public class BankName
{
    ...

    ArrayList<BranchBank> branches;
}

The ordering of these statements does not seem to follow the pattern shown in the operations.txt file:
Code:

        //read next string
        operation = file.next( );
        // read next integer
        id1= file.nextInt( );
        //read next string
        name11 = file.next( );
        //read next double
        balance1 = file.nextDouble();

From the file you posted, the order is operation, transaction amount, id, and then name.


comment about the first point


it is not correct by logic to say that
BankName extends BranchBank

but i think i need to do that beacuse i can't say this statement
if (a.accountName == accountName11 && a.IdBranch == IdBranch111)// found a match

inside searchAccount method.

dwhitney67 05-06-2012 08:47 AM

Quote:

Originally Posted by 4four (Post 4671590)
comment about the first point


it is not correct by logic to say that
BankName extends BranchBank

but i think i need to do that beacuse i can't say this statement
if (a.accountName == accountName11 && a.IdBranch == IdBranch111)// found a match

inside searchAccount method.

A bank has one or more branch offices; the branch office(s) in turn, has zero or more accounts.

Code:

class BankAccount
{
    protected String accountName;
    protected double accountBalance;

    ...
}

class BankBranch
{
    protected ArrayList<BankAccount> accounts;
    protected int                    branchID;

    ...
}

public class BankName
{
    protected ArrayList<BankBranch> branches;

    ...

    public BankAccount searchAccount(String accountName, int branchID)
    {
        BankAccount account = null;

        for (BankBranch branch : branches)
        {
            if (branch.getID() == branchID)
            {
                account = branch.getAccount(accountName);
                break;
            }
        }

        return account;
    }
}

Keeping an ArrayList of BankBranches, as per your requirements, will induce latencies when searching for a bank branch based on its ID. Perhaps a HashMap would be a better choice; ask your teacher if this change is permitted.

4four 05-06-2012 09:34 AM

Quote:

Originally Posted by dwhitney67 (Post 4671598)
A bank has one or more branch offices; the branch office(s) in turn, has zero or more accounts.

Code:

class BankAccount
{
    protected String accountName;
    protected double accountBalance;

    ...
}

class BankBranch
{
    protected ArrayList<BankAccount> accounts;
    protected int                    branchID;

    ...
}

public class BankName
{
    protected ArrayList<BankBranch> branches;

    ...

    public BankAccount searchAccount(String accountName, int branchID)
    {
        BankAccount account = null;

        for (BankBranch branch : branches)
        {
            if (branch.getID() == branchID)
            {
                account = branch.getAccount(accountName);
                break;
            }
        }

        return account;
    }
}

Keeping an ArrayList of BankBranches, as per your requirements, will induce latencies when searching for a bank branch based on its ID. Perhaps a HashMap would be a better choice; ask your teacher if this change is permitted.




we have to put each class in sapreate file.


All times are GMT -5. The time now is 10:02 PM.