Problem Statement
You are in charge of writing some software to generate an itemized invoice. You have a raw list of items that were purchased, but the raw list is not very user friendly. Your code should determine how many of each item were purchased, and return a list of items and their quantity. You are given a String[] items, each element representing a single item. You must return a String[] listing, in alphabetical order, each item and its quantity. Each element of the return should be formatted as "item - quantity" (quotes added for clarity), where the quantity has no leading zeroes.
Definition
- Class:
- ItemizedList
- Method:
- generateList
- Parameters:
- String[]
- Returns:
- String[]
- Method signature:
- String[] generateList(String[] items)
- (be sure your method is public)
Constraints
- items will contain between 1 and 50 elements, inclusive.
- Each element of items will contain between 1 and 50 lowercase letters ('a'-'z'), inclusive.
Examples
{"apple", "orange", "apple", "banana", "apple", "orange"}Returns: {"apple - 3", "banana - 1", "orange - 2" }A grocery list of someone getting ready to make a fruit salad.{"apple", "apple", "apple", "apple"}Returns: {"apple - 4" }The itemized format is a lot more concise.{"apple", "orange", "banana"}Returns: {"apple - 1", "banana - 1", "orange - 1" }Each item only appears once.{"apple", "orange", "apple", "banana", "apple", "apple", "orange", "apple", "banana", "apple", "apple", "orange", "apple", "banana", "apple"}Returns: {"apple - 9", "banana - 3", "orange - 3" }Lots of items.{"apple"}Returns: {"apple - 1" }A very short list.