A menu-driven program allows users to buy birds from a bird shop called Twitter.
The program stores the type of bird in a String array and the corresponding price in an integer array!
Here is a prototype of the program that is being developed.
3 functions need to be designed. Note, you can make changes to the suggested code if you can find ways to improve it.
Eg making the Scanner variable global and initialising it in the main function would make it easily available to all functions.
This function displays the contents of two arrays. One is an array of Strings. One is an array of prices.
PRE-CONDITION: both arrays always have the same length.
It does not return anything.
This function searches flappies for flappy using a linear search algorithm.
It returns -1 if it cannot find flappy in flappies.
Otherwise it returns the index of flappy in flappies.
This function first calls the displayInfo function.
Then it asks the user to input the name of a bird eg canary
It then calls searchForBird (see above).
If the returned value from searchForBird is -1, the function returns False.
Else, the function displays the price of the bird and returns True.
Design the three functions and see if you can get the program working as expected.
A new global integer array, stock, needs to be introduced. This stores how many of each bird are in stock.
Save your code from Task 1 as version 1. This adaptation will be version 2.
Save your code as version 2. This will be version 3.
Adapt your program so that a new global variable, balance is stored and assigned a value, eg 1000.
A buyer is allowed to buy a bird as long as (s)he has enough money.
If the buyer can buy a bird, not only is the stock updated but the balance is updated, too.
If an array is a global variable in a Java program, we might not have to return it from a function after we have updated it.
This is because when we pass an array to a function as a parameter, we are not passing a copy of the data (like we do with integers and Strings and booleans etc).
We are passing a reference to the array. This means that we can directly communicate with the array and make changes to it!
In Java, an array is static. This means that after we declare it with a certain length, we can never change the length!
So, our above program will only ever be able to process up to 3 birds.
One solution to this is to create an array which has a length bigger than we need eg:
String[] birds = new String[1000];
This is not a bad idea... but in some cases it is a waste of memory.
The perfect solution would be to use a dynamic data structure whose length can change as the program runs.
In Java, an ArrayList would be a good option...
One tip is to make a list of things that your program needs to do before you start coding - you could do this as comments in Python eg
#create an empty array
#create a loop to do something 5 times
#ask the user to input a number in the loop
#append the number to the array in the loop
#calculate statistics in the loop
#display all data and statistics after the loop
Then... start coding!