Difference between revisions of "Creating a grocery list"
From CompSciWiki
Line 1: | Line 1: | ||
{{1010PrAD|ProblemName=Create a Grocery List | {{1010PrAD|ProblemName=Create a Grocery List | ||
− | |Problem= | + | |Problem=Write a program create a simple grocery list. Prompt the user for the number of items in the list. Use parallel arrays to store the item names and quantities. Then print the list of items and a count of the number of items<br><br> |
Example Output<br> | Example Output<br> | ||
<pre> | <pre> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Grocery List | Grocery List | ||
------------ | ------------ | ||
Line 19: | Line 10: | ||
12 eggs | 12 eggs | ||
4 apples | 4 apples | ||
+ | |||
+ | 17 items | ||
</pre> | </pre> | ||
|SideSectionTitle=More with Arrays | |SideSectionTitle=More with Arrays | ||
Line 26: | Line 19: | ||
|Solution= | |Solution= | ||
− | + | First, prompt the user for the number of items:<br> | |
<pre> | <pre> | ||
− | // | + | String input; //temp variable for dialog input |
− | + | int listSize = 0; //the number of items for the grocery list, default 0 | |
− | + | input = JOptionPane.showInputDialog("Input length:"); | |
− | + | listSize = Integer.parseInt(input); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</pre> | </pre> | ||
− | + | ||
+ | |||
+ | Now that we have the number of items, listSize, we need to get the items and quantities. | ||
<pre> | <pre> | ||
− | //Create and | + | //Create arrays for list items and quantity |
String[] groceryList = new String[listSize]; | String[] groceryList = new String[listSize]; | ||
int[] groceryQuantity = new int[listSize]; | int[] groceryQuantity = new int[listSize]; | ||
− | // | + | //prompt for input listSize times |
for( int i = 0; i < groceryList.length; i++ ) | for( int i = 0; i < groceryList.length; i++ ) | ||
{ | { | ||
− | // | + | //get the name of the item and |
− | + | groceryList[ i ] = JOptionPane.showInputDialog("Item name"); | |
− | + | ||
− | + | input = JOptionPane.showInputDialog("How many " + groceryList[ i ] + "?" ); | |
− | + | groceryQuantity[ i ] = Integer.parseInt( input ); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
</pre> | </pre> | ||
− | Last step is to print the list out. | + | Last step is to print the list out. We will use the loop for printing to sum the quantities. |
<pre> | <pre> | ||
+ | int itemCount = 0; //sum of the quantities of the items | ||
+ | |||
//Print a header for the list | //Print a header for the list | ||
System.out.println( "\n\nGrocery List" ); | System.out.println( "\n\nGrocery List" ); | ||
Line 82: | Line 59: | ||
{ | { | ||
System.out.println( groceryQuantity[ i ] + " " + groceryList[ i ] ); | System.out.println( groceryQuantity[ i ] + " " + groceryList[ i ] ); | ||
+ | itemCount = itemCount + groceryQuantity[ i ]; | ||
} | } | ||
+ | |||
+ | System.out.println( "\n" + itemCount + " items."); | ||
</pre> | </pre> | ||
− | |SolutionCode=<pre>public class | + | |SolutionCode= |
− | { | + | <pre> |
− | }</pre> | + | import javax.swing.JOptionPane; |
+ | |||
+ | public class grovery { | ||
+ | public static void main( String[] args ) | ||
+ | { | ||
+ | |||
+ | String input; //temp variable for dialog input | ||
+ | int listSize = 0; //the number of items for the grocery list, default 0 | ||
+ | |||
+ | input = JOptionPane.showInputDialog("Input length:"); | ||
+ | listSize = Integer.parseInt(input); | ||
+ | |||
+ | |||
+ | //Create arrays for list items and quantity | ||
+ | String[] groceryList = new String[listSize]; | ||
+ | int[] groceryQuantity = new int[listSize]; | ||
+ | |||
+ | // | ||
+ | for( int i = 0; i < groceryList.length; i++ ) | ||
+ | { | ||
+ | //get the name of the item and | ||
+ | groceryList[ i ] = JOptionPane.showInputDialog("Item name"); | ||
+ | |||
+ | input = JOptionPane.showInputDialog("How many " + groceryList[ i ] + "?" ); | ||
+ | groceryQuantity[ i ] = Integer.parseInt( input ); | ||
+ | |||
+ | } | ||
+ | |||
+ | int itemCount = 0; //sum of the quantities of the items | ||
+ | |||
+ | //Print a header for the list | ||
+ | System.out.println( "\n\nGrocery List" ); | ||
+ | System.out.println( "------------" ); | ||
+ | |||
+ | //loop through and print out the list items | ||
+ | for( int i = 0; i < groceryList.length; i++ ) | ||
+ | { | ||
+ | System.out.println( groceryQuantity[ i ] + " " + groceryList[ i ] ); | ||
+ | itemCount = itemCount + groceryQuantity[ i ]; | ||
+ | } | ||
+ | |||
+ | System.out.println( "\n" + itemCount + " items."); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </pre> | ||
}} | }} |
Revision as of 02:06, 7 April 2010
Back to the Program-A-Day homepage
ProblemWrite a program create a simple grocery list. Prompt the user for the number of items in the list. Use parallel arrays to store the item names and quantities. Then print the list of items and a count of the number of items Example Output Grocery List ------------ 1 bacon 12 eggs 4 apples 17 items |
More with Arrays | |
---|---|---|
SolutionFirst, prompt the user for the number of items: String input; //temp variable for dialog input int listSize = 0; //the number of items for the grocery list, default 0 input = JOptionPane.showInputDialog("Input length:"); listSize = Integer.parseInt(input);
//Create arrays for list items and quantity String[] groceryList = new String[listSize]; int[] groceryQuantity = new int[listSize]; //prompt for input listSize times for( int i = 0; i < groceryList.length; i++ ) { //get the name of the item and groceryList[ i ] = JOptionPane.showInputDialog("Item name"); input = JOptionPane.showInputDialog("How many " + groceryList[ i ] + "?" ); groceryQuantity[ i ] = Integer.parseInt( input ); } Last step is to print the list out. We will use the loop for printing to sum the quantities. int itemCount = 0; //sum of the quantities of the items //Print a header for the list System.out.println( "\n\nGrocery List" ); System.out.println( "------------" ); //loop through and print out the list items for( int i = 0; i < groceryList.length; i++ ) { System.out.println( groceryQuantity[ i ] + " " + groceryList[ i ] ); itemCount = itemCount + groceryQuantity[ i ]; } System.out.println( "\n" + itemCount + " items."); | ||
Code |