This program is a take on the classic programming assignment of creating a grocery list. Your task is to take the store, the food or drink item, and the amount for each item as input and create a grocery list string that can be displayed to the user. For example, the parts in bold were entered by the user.
Once read, the user input will be modified to fit the following rules:
- Store name: no leading or trailing whitespace; all characters should be upper case; the only String that may contain a space character (can be multiple words).
- All food and drink: no leading or trailing whitespace; all characters should be lower case.
Note to remove any leading/trailing whitespace you may want to consider using the .trim() method for strings!
Below is an example of input, in bold, for the grocery list program. Each amount/weight in the input are followed by their corresponding food/drink (amount1 corresponds to food1, amount3 corresponds to drink, etc).
Store: the Farmer's Market
amount1: 5
food1: Apples
amount2: 12
food2: EggS
amount3: 3
drink: milK
weight1: 1.5
food3: CHEESE
weight2: 3
food4: potatoes
Note the store/amount/food/drink labels will not be included in the input. Thus the input alone will be seen as follows:
the Farmer's Market
5
Apples
12
EggS
3
milK
1.5
CHEESE
3
potatoes
The resulting grocery list is:
Grocery List for Bucky Badger
I am going to THE FARMER'S MARKET to get:
5 apples
12 eggs
3 gallons of milk
1.5 pounds of cheese
3.0 pounds of potatoes
Note: there should always be 5 items in the grocery list, with the third item in units of gallons, and the fourth and fifth items in units of pounds.
Requirements:
- Your main method should contain all input code (e.g., Scanner instance) and have 11 user prompts inputting values with nextInt(), nextDouble(), next(), and nextLine().
- All output code (e.g., print statements) must be in the main method as well. The
makeGroceryList
method takes the arguments passed in and prepares the entire grocery list, using all the 11 parameters and returning one long String. You will need to use string concatenation to create this one long string combining all the variables. - You do not need to use the above grocery list. You can try out your own input values.
- Your grocery list must contain your name as you entered it in the file header comments. It should be a string literal and not passed in as a parameter. For example, at the start of your grocery list "Grocery List for Bucky Badger…" where Bucky Badger is your name.
- The method stub for
makeGroceryList
is:
/**
* Returns a single String that is formatted as a grocery list that contains all
* 11 parameters. You must include your name in the list as a string literal
* (don't pass in your name as a parameter). Your name must match the name within
* the File Header Comment.
*
* There are no input or output statements in this method. All input should already be done in
* main (when the user is prompted), and all output should be done in main after this method
* returns.
*
* DO NOT MODIFY THE ORDER OF PARAMETERS IN THE METHOD HEADER. If you do,
* you may not pass the automatic tests that we use to grade your submission.
*
* @param store
* @param amount1
* @param amount2
* @param amount3
* @param weight1
* @param weight2
* @param food1
* @param food2
* @param food3
* @param food4
* @param drink
* @return A grocery list that contains all 11 parameters and your name.
*/
public static String makeGroceryList(String store, int amount1, int amount2,
int amount3, double weight1, double weight2, String food1, String food2,
String food3, String food4, String drink) {
//FILL IN BODY
}
Note that you may change the makeGroceryList
parameter names to be more meaningful, but be sure to keep the order and data types the same. It is good practice to test different combinations of input with your method, but note that we will test your method using specific input for this lab. When calling makeGroceryList
, the arguments passed are by position (the order in which they show up as parameters in the method header), so the parameter names do not matter outside the method. Also note that the grocery list should NOT end with a newline character.
Note:
(1) A common error found when running this program is an InputMismatchException error. This occurs when a scanner is looking for one type of object but finds another (for example scnr.nextInt() is looking for an integer when the next value may be a string). Be sure the order in which you read in inputs matches the expected type. Hint: Read 3 L | Tutorial: Using Scanner to make sure you understand the differences among nextInt(), nextDouble(), next(), and nextLine() on reading the input. (Especially how nextLine() is different from others.)