Difference between revisions of "Creating a grocery list"
From CompSciWiki
(Moved pre tags out of code section) |
(Changed to utilize CodeBlock and OutputBlock templates) |
||
(One intermediate revision by one other user not shown) | |||
Line 4: | Line 4: | ||
Example Output<br> | Example Output<br> | ||
− | + | {{OutputBlock | |
+ | |Code= | ||
Grocery List | Grocery List | ||
------------ | ------------ | ||
Line 12: | Line 13: | ||
17 items | 17 items | ||
− | + | }} | |
|SideSectionTitle=More with Arrays | |SideSectionTitle=More with Arrays | ||
|SideSection= | |SideSection= | ||
− | [[Image:Wiki_method01.jpg|center]] | + | [[Image:Wiki_method01.jpg|center]]<BR> |
− | <BR> | + | |
|Solution= | |Solution= | ||
First, prompt the user for the number of items:<br> | First, prompt the user for the number of items:<br> | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
String input; //temp variable for dialog input | String input; //temp variable for dialog input | ||
int listSize = 0; //the number of items for the grocery list, default 0 | int listSize = 0; //the number of items for the grocery list, default 0 | ||
Line 27: | Line 28: | ||
input = JOptionPane.showInputDialog("Input length:"); | input = JOptionPane.showInputDialog("Input length:"); | ||
listSize = Integer.parseInt(input); | listSize = Integer.parseInt(input); | ||
− | + | }} | |
Now that we have the number of items, listSize, we need to get the items and quantities. | Now that we have the number of items, listSize, we need to get the items and quantities. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
//Create arrays for list items and quantity | //Create arrays for list items and quantity | ||
String[] groceryList = new String[listSize]; | String[] groceryList = new String[listSize]; | ||
Line 46: | Line 48: | ||
} | } | ||
− | + | }} | |
Last step is to print the list out. We will use the loop for printing to sum the quantities. | Last step is to print the list out. We will use the loop for printing to sum the quantities. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
int itemCount = 0; //sum of the quantities of the items | int itemCount = 0; //sum of the quantities of the items | ||
Line 64: | Line 67: | ||
System.out.println( "\n" + itemCount + " items."); | System.out.println( "\n" + itemCount + " items."); | ||
− | + | }} | |
All together we have: | All together we have: | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
import javax.swing.JOptionPane; | import javax.swing.JOptionPane; | ||
Line 114: | Line 118: | ||
} | } | ||
} | } | ||
− | + | }} | |
Once I have a working example I like to go a step further and simplify the code<br> | Once I have a working example I like to go a step further and simplify the code<br> |
Latest revision as of 16:33, 4 December 2011
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. Then prompt the user to enter each item and the amount of each item needed. Use parallel arrays to store the item names and quantities. Finally, print the list of items and a count of the total 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( "Grocery 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."); All together we have: import javax.swing.JOptionPane; public class grocery { 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]; //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 ); } int itemCount = 0; //sum of the quantities of the items //Print a header for the list System.out.println( "Grocery 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."); } } Once I have a working example I like to go a step further and simplify the code
| ||
Code |