brian leung

Greenhorn
+ Follow
since Nov 14, 2020
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by brian leung

Paul Clapham wrote:

brian leung wrote:Yeah. This seems to be the case that I only have my Customer with "789" currently in my map.



My guess is that you're accidentally creating one Map per Customer in the other code, and then when you go to use the Map it's the one with the data for the last Customer.



Ah I fixed it! I redesigned my main class and it appears to now be displaying all the account numbers in my class!

This brings me to my next question. If this function to display all codes works. How would I design it so that it only displays inactive accounts?
I've already created some inactive accounts by creating the accounts in my main class and then calling the setActive() functions and setting them to false.

Currently, when I run this code



All accounts including inactive ones get displayed.

I know I can use probably reuse this. But would I write this?

6 days ago

Campbell Ritchie wrote:That tells me that Paul was correct to suggest you only have one “K” in your Map.
Why did you put the @Override annotation on that method? What are you overriding? Why? That method looks like something that shouldn't be overriding anything.



The class that this function is in is implementing two other interfaces. So I'm essentially overriding the methods in the two other interfaces.


6 days ago

Paul Clapham wrote:

brian leung wrote:I ONLY get SA-789.  It doesn't print out SA-123, and SA-789. Same thing happens when I type in CH for the prefix. I ONLY get CH-789.

So I'm just wondering, am I iterating incorrectly? Is it going through the loop and then only printing out the most recent number, but not actually adding all the numbers and printing ALL of them out??



You seem to be convinced that you have, what, six entries in that map? But the evidence says otherwise. The Map class has a size() method which tells you how many key-value pairs are in the Map, you could use that to see what it says.



Yeah. This seems to be the case that I only have my Customer with "789" currently in my map.

I tried running my program, and the code appears to work fine with that specific customer. I can deposit/withdraw and show transactions with that customer. However when I try to do it with the other 2 customers, "123" and "456". When I try to run these methods, all I get are NullPointerExceptions.
6 days ago

Paul Clapham wrote:

This code will go through all of the String objects from the Iterator, yes. Is that different from what actually happened?



It's still only printing out 1 account number.

In my main class. I have 3 customers created. I'll label them Tom, Bob, Jim.

All 3 of them have a Chequings and Savings account. The prefix for Chequings is "CH" and for Savings is "SA". Each customer will have a number followed by the account prefix to differentiate each customer account. For example.

Tom's chequing Account will be CH-123, and SA-123 for Savings.
Bob's chequing Account will be CH-456, and SA-456 for Savings.
Jim's chequing Account will be CH-789, and SA-789 for Savings.


When I call this method in my main class, and I put in "SA" as the prefix for the argument.

I ONLY get SA-789.  It doesn't print out SA-123, and SA-789. Same thing happens when I type in CH for the prefix. I ONLY get CH-789.

So I'm just wondering, am I iterating incorrectly? Is it going through the loop and then only printing out the most recent number, but not actually adding all the numbers and printing ALL of them out??
1 week ago
Sorry. Maybe I should've made this clearer.

Yes, the account number is the string which is the key to the HashMap. I am trying to print out ALL the accounts that START with the prefix, and yes in an case-insensitive way. What I meant by "overwritten" is. Inside the while loop, once the loop iterates through once, it prints out the "number" which I have set as i.next(). While that loop is true, i'm assuming, the next number in line is called and thus, that number is printed out, so like only printing out the last number and not ALL of the account numbers.

I hope that clears up any confusion?
1 week ago
When I call this method and run my program. It's only printing out one account number, when i'm trying to print out ALL the account numbers in my hashmap. Any tips?
Should I be using a for loop instead of a while? Essentially I'm trying to prevent the number from being overriden everytime the while loop iterates. I believe that is the issue

1 week ago

Junilu Lacar wrote:

brian leung wrote:


When you say errors, do you mean compile time errors or runtime errors? What's going on with line 7 in the above code? What are you trying to do there?



I was primarily getting compile time errors for the .add() and .remove() methods.

In terms of line 7. I have updated my code to this





However, i'm still able to add different data types into my arraylist "newList" in the main method. Basically i'm trying to make it so the overloaded constructor parameter "public MyArrayList (Object type) will be used to set the type of reference that will be added to my "arraylist".
1 week ago
I'm trying to validate a specific type and make my list"type-safe". I'm trying to ensure that only one type of reference can be added. For example if I want this list to ONLY be able to take String objects, it can ONLY take string objects.

The array is already set to an Object type, and everything inherits from Object, i've tried to substitute/subtype. However i'm getting errors when trying to call my add/display/remove functions. Not sure if i'm even validating my types correctly? Unfortunately, I can't use generics for this as my professor has not yet covered that topic in class and doesn't expect us to know how to use it.


2 weeks ago

Carey Brown wrote:Brian, please do not edit old posts once people have responded to them. It makes their responses sound stupid. I have reverted your edit. Please re-post the new code again at the end of this thread.



Ah sorry, didn't want to spam and re-post.
2 weeks ago

Mike Simmons wrote:First, element 1 and element 2 are not the first and second element, because element 0 is the first, and element 1 is the second.  Confusing, but that's how most arrays and lists are numbered, in Java and most other programming.

But also, what Piet was saying is that remove(1) and remove(2) will remove element 1 (the second element) and the new element 2 (new third) , which was originally element 3 (fourth).  The first remove(1) has an effect on the numbers in the list afterwards.

So if you have an array consisting of

["A", "B", "C", "D", "E"]

and you call remove(1), you get

["A", "C", "D", "E"]

and if you now call remove(2), you get

["A", "C", "E"]

The missing B and D were originally elements 1 and 3, second and fourth, in the original array.



Ok thank you Mike, this makes sense being able to visualize it. I'm still trying to sweat this out. So this must mean that I'm updating the original list to the new list in the wrong place?
2 weeks ago

Piet Souris wrote:in the 'remove' method, you forget to set theList to temp in the end.

As a sidenode: if you do 'remove(1)' followed by 'remove(2)', you are in fact deleting elements 1 and 3 of the original theList. But maybe that's the intention.



Sorry i'm confused on what you mean why you say i'm deleting elements 1 and 3 from the original theList. Is remove(1) and remove(2) deleting the first and second element?
2 weeks ago
I'm trying to remove an element from an array. I am running into an issue where when I run my program and remove a certain index. I get nulls in the place where the element/index should be removed entirely.

My output is when I execute list.display() in the main method is

Tom, Bob, Richard, John

However after I execute list.remove() and run the program. I get

Tom, null, null, Richard.




2 weeks ago

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?
2 weeks ago

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?
2 weeks ago
2 weeks ago