• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Accessing clients specific account out of an arraylist of account Objects

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Apologies for the large post. I've formatted the code below.

Beginner here. I've been stuck on this for nearly 3 days now, and can't seem to figure out how to solve this problem.

I have a Bank class which has a HashMap that contains the customerID as the key and the Customer object as the value. I have an account superclass, and 3 different account subtypes. My Customer class contains an ArrayList of account objects, each customer can have as many accounts as they like. My accounts and customers are created in my Driver "ATM" initialize() function, and the customers along with their classes are added to theBank hashmap here.

In my verifyCustomer() function in the ATM class. Once the user signs in, enters their ID, it should display a list of that specific users accounts. At this point I know I have to do a for loop and print out the list of accounts.

But, how do I and where would I create a function that allows me to be able to "track" or get a specific account out of the arraylist of account objects? AND, how would I access this? The reason I ask this, is because inside both the transactDeposit() and transactWithdraw(), I can't seem to be able to access a SPECIFIC account. I get null pointer exceptions for both of these functions and can't figure out why.

I think the issue might be in my Customer class under the getAccount(). There I tried to do a for-loop to iterate through the arrayList objects, but I know the code logic is probably wrong. Let me know if there may be some other deeper rooted issue, that might not be related to the getAccount() function.

I left out the code for the 3 different sub type account classes as well as the Bank class due to post length. Let me know if you want me to post those in here as well.

THANK YOU!


 
brian leung
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marshal
Posts: 71070
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

brian leung wrote:Hello . . . how do I and where would I create a function that allows me to . . . get a specific account out of the arraylist . . .

I would use the List's stream() method and filter the Stream for a particular account number/name/something. But, why do you need to search the List? Why don't you create Maps to link all those details to the account objects? Why do you need a List at all?

Actually, I think there are good reasons for using a List. You can iterate the List and look for features like balance less than zero, etc. If you try that with a Map, you would iterate the value set. You don't need to use immutable objects as keys in a List because Lists don't have keys.
 
Marshal
Posts: 26130
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

brian leung wrote:The reason I ask this, is because inside both the transactDeposit() and transactWithdraw(), I can't seem to be able to access a SPECIFIC account. I get null pointer exceptions for both of these functions and can't figure out why.



It's because your design is kind of contradictory. There are two design statements which you have encoded in Java:

1. A Customer has an Account.

2. A Customer has a List of Accounts.

You're doing fine at setting up the list of accounts, but you're doing nothing to set up "the" customer's account. But that isn't really what you want to do anyway (I don't think). You want to select an account for the customer, and then you want to apply transactions to it. But that would be a different design.
 
brian leung
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

brian leung wrote:The reason I ask this, is because inside both the transactDeposit() and transactWithdraw(), I can't seem to be able to access a SPECIFIC account. I get null pointer exceptions for both of these functions and can't figure out why.



It's because your design is kind of contradictory. There are two design statements which you have encoded in Java:

1. A Customer has an Account.

2. A Customer has a List of Accounts.

You're doing fine at setting up the list of accounts, but you're doing nothing to set up "the" customer's account. But that isn't really what you want to do anyway (I don't think). You want to select an account for the customer, and then you want to apply transactions to it. But that would be a different design.



In this case would the getAccount(String accountNumber) function be the right thing to do?

The problem I find with this function is that it takes a string argument. However when I call this function in my transactDeposit() function, it requires that string argument as well. However, in the transact functions. It's going off of the current user's account when I run the program. So really there is no reason to have a string argument in the getAccount() function?
 
Paul Clapham
Marshal
Posts: 26130
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

brian leung wrote:In this case would the getAccount(String accountNumber) function be the right thing to do?

The problem I find with this function is that it takes a string argument. However when I call this function in my transactDeposit() function, it requires that string argument as well. However, in the transact functions. It's going off of the current user's account when I run the program. So really there is no reason to have a string argument in the getAccount() function?



I don't know. You're asking about what code to write to implement some requirements which you didn't tell us about.

Somewhere in your design there needs to be something about choosing a customer's account before applying transactions to it. I don't see that. And even in your menu options there's nothing about that.

It's important to get your design and requirements clear before you start writing code.
 
brian leung
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

brian leung wrote:In this case would the getAccount(String accountNumber) function be the right thing to do?

The problem I find with this function is that it takes a string argument. However when I call this function in my transactDeposit() function, it requires that string argument as well. However, in the transact functions. It's going off of the current user's account when I run the program. So really there is no reason to have a string argument in the getAccount() function?



I don't know. You're asking about what code to write to implement some requirements which you didn't tell us about.

Somewhere in your design there needs to be something about choosing a customer's account before applying transactions to it. I don't see that. And even in your menu options there's nothing about that.

It's important to get your design and requirements clear before you start writing code.





Here in my verifyCustomer() {}. After I Choose which Customer ID i'm trying to access. I have to enter and choose the specific account number that I want to access. Is this what you are referring to?
 
Paul Clapham
Marshal
Posts: 26130
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that's it. There's several flaws that I can see right away, though.

For example if they enter a bad customer ID, you tell them that and then continue right on by asking which account they want to access. Yeah, you haven't given them a list to choose from but you still ask. You should quit at that point.

(Preferably write separate methods for each of the parts of the verify dialog.)

Second: you're comparing the account number they enter to the customer's account number. This is the design confusion I mentioned before: does a customer have an account number or does it have a list of account numbers? You don't check against the list, you check against a number which appears to never be set. But at any rate, even though it doesn't match you still don't set customerVerified to false.

Third: you don't ask for the passcode until after you've done the account ID dialog. In a real bank that would be a horrible security failure -- you give out a list of account numbers before you know whether the user knows the customer's passcode.
 
Paul Clapham
Marshal
Posts: 26130
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh... I think I get it. That property you're calling the "customer's account number" is actually the "selected account number". So you should call it that. And you need a method to set it, i.e. the one which is commented out.

Only, it would be better if it was the "selected account" and it was a reference to an Account, rather than just an account ID.
 
Popeye has his spinach. I have this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic