Arrays Search


Arrays - Search

SearchArray.java

Classwork

Remember that you can always test your code as you go. It is much easier to test your code a little bit at a time and fix it as you go than to wait to the end and try to test and fix everything at once.


Part A

☑In a main, create an array of 10 Strings and fill it with names (make them up) using special initialization. 

☑Also create an array of ten doubles, and use a loop to set them so that they all start at 10.   These parallel arrays represent the names and balances of bank customers, so if at index 6 in the first array there is "Constance" and at index six in the second array there is 646.20, that means Constance's account has $646.20. 

☑Ask the user their name.  (When we read in both strings and numbers, Scanner can be squirrely, so use the next() method instead of nextLine() here if you're getting weird behavior later)

We need to know if the name entered is in our list (one of our customers) and if so, we need to know at what position the name is in the array (the index). 

☑ Search through the array to find the index where that name appears.  In cases like this, we usually use the impossible index value -1 to indicate something was not found.  Call your variable for this index userI.

☑After this search, if the name did not match any in the array, print an error.  If the name was in the array, tell the customer that they are logged in, and tell them their balance, then ask them how much they would like to deposit, and add that to their balance.


Part B

Now we want multiple users to be able to sign in and change their balances, so we need to repeat the code for logging in and updating balance.

☑Create a boolean called done and put a big loop around the code you already wrote, so they can enter a name and we can search for it and they can deposit, over and over until they are done.

☑When the user is asked to enter their name, tell them they can also enter "QUIT" to end the program

If they said "QUIT" we are done, and we shouldn't even check the array for the name. If they entered anything else, we will do the code you already wrote, searching for the name.

Note that as long as they don't say "QUIT" we will loop and they will have the chance to put in another name next time, even if the name doesn't match.


Part C

☑ After the big loop is done, search through the array of balances and find the index of the highest balance.

☑ Then go back and print all the customers and their balances, putting a note next to the name of the person with the highest balance.