When submitting this lab, submit a .java file called ShoppingList2, and create the following structure in Eclipse:
Package Name:week9
Class Name:ShoppingList2
For this lab you will be updating your last program to make the shopping list more interesting.This is about string methods.Instead of simply entering items into a shopping list, your users will now add items and the corresponding amounts desired into the shopping list as a single string using the format ‘:’. Your program will need to split the string to get the proper formatting as described below and shown in the example run.String formatting must appear exactly as shown in the example run.
Make the following updates to your last program
addItems()
- Require the user to use a ‘:’ when entering new items into the shopping list with the following format: :
- BEFORE adding the user entry into your shoppingList ArrayList, do the following:
- Check to be sure that each new item includes a ‘:’ If the item does not contain a ‘:’, display a helpful message indicating the required format as shown in the example run.
- Remove any leading or trailing spaces before and after the AND before and after the . Do this before adding the item to the shopping list. Example: If the user enters the string ‘ apples : 6 ‘, then ‘apples:6’ should be added to the shopping list.
showItems()
- Display your Shopping List to look like the following:
--------------------------
Shopping List
----------------------------
Apples 6
Oranges 10
--------------------------
- To get your shopping list to display as shown above, you will need to:
- Print the title for your Shopping List
- Split each item into its itemName and itemAmount
- Use System.out.printf() to print the itemName and itemAmount with width specified
deleteItems()
- There are no changes to deleteItems().
sort_Items()
- There are no changes to sortItems().
Hint:
- When splitting a string into two substrings based on a given character delimiter, a combination ofindexOf()andsubstring()can be used.
Example:
To separate “Hello,World” into two strings, “Hello” and “World” do the following:
originalWord = “Hello,World”; //the original user input
commaIndex = originalWord.indexOf(“,”); //gets the index number for where the “,” is located
firstWord = originalWord.substring(0, commaIndex); //gets everything before the comma
secondWord = originalWord.substring(commaIndex + 1); //gets everything after the comma
Results:
- firstWord now equals "Hello"
- secondWord now equals "World"
|