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 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 07:48 PM.