Difference between revisions of "Creating a grocery list"

From CompSciWiki
Jump to: navigation, search
(Changed to utilize CodeBlock and OutputBlock templates)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{1010PrAD|ProblemName=Create a Grocery List
 
{{1010PrAD|ProblemName=Create a Grocery List
  
|Problem=Create a grocery list. Prompt the user for the number of items and the quantity. Then print the list of items.<br><br>
+
|Problem=Write 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<br><br>
  
 
Example Output<br>
 
Example Output<br>
<pre>
+
{{OutputBlock
Enter the number of items for the list: 3
+
|Code=
Next item:bacon
+
Item Quantity:1
+
Next item:eggs
+
Item Quantity:12
+
Next item:apples
+
Item Quantity:4
+
 
+
 
+
 
Grocery List
 
Grocery List
 
------------
 
------------
Line 19: Line 11:
 
12 eggs
 
12 eggs
 
4 apples
 
4 apples
</pre>
+
 
 +
17 items
 +
}}
 
|SideSectionTitle=More with Arrays
 
|SideSectionTitle=More with Arrays
 
|SideSection=
 
|SideSection=
[[Image:Wiki_method01.jpg|center]]
+
[[Image:Wiki_method01.jpg|center]]<BR>
  
 
|Solution=
 
|Solution=
  
Prompt the user for the number of items:<br>
+
First, prompt the user for the number of items:<br>
<pre>
+
{{CodeBlock
//setup a BufferedReader to read from the console
+
|Code=
BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in) );
+
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
+
input = JOptionPane.showInputDialog("Input length:");
 +
listSize = Integer.parseInt(input);
 +
}}
  
//read in a value and try casting it to an integer
+
 
//Java requires you catch any exceptions that may arise
+
Now that we have the number of items, listSize, we need to get the items and quantities.
System.out.print ("Enter the number of items for the list: ");
+
{{CodeBlock
try
+
|Code=
{
+
//Create arrays for list items and quantity
String tempListSize = stdin.readLine();
+
listSize = Integer.parseInt( tempListSize );
+
}
+
catch(Exception ex)
+
{
+
System.out.println("Input error");
+
System.exit(1);
+
}
+
</pre>
+
Next, loop up to the inputted value prompting the user for grocery list items
+
<pre>
+
//Create and array of strings, size listSize
+
 
String[] groceryList    = new String[listSize];
 
String[] groceryList    = new String[listSize];
 
int[]    groceryQuantity = new int[listSize];
 
int[]    groceryQuantity = new int[listSize];
  
//loop from zero to the size of the array
+
//prompt for input listSize times
 
for( int i = 0; i < groceryList.length; i++ )
 
for( int i = 0; i < groceryList.length; i++ )
 
{
 
{
//Java requires you to catch any exceptions
+
//get the name of the item and
try
+
groceryList[ i ] = JOptionPane.showInputDialog("Item name");
{
+
 
System.out.print( "Next item:" );
+
input = JOptionPane.showInputDialog("How many " + groceryList[ i ] + "?" );
groceryList[ i ] = stdin.readLine();
+
groceryQuantity[ i ] = Integer.parseInt( input );
System.out.print( "Item Quantity:" );
+
groceryQuantity[ i ] = Integer.parseInt( stdin.readLine() );
+
}
+
catch(Exception ex)
+
{
+
System.out.println("Input error");
+
System.exit(1);
+
}
+
 
}
 
}
</pre>
+
}}
 +
 
 +
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
  
Last step is to print the list out.
 
<pre>
 
 
//Print a header for the list
 
//Print a header for the list
System.out.println( "\n\nGrocery List" );
+
System.out.println( "Grocery List" );
 
System.out.println( "------------" );
 
System.out.println( "------------" );
  
Line 82: Line 63:
 
{
 
{
 
System.out.println( groceryQuantity[ i ] + " " + groceryList[ i ] );
 
System.out.println( groceryQuantity[ i ] + " " + groceryList[ i ] );
 +
itemCount = itemCount + groceryQuantity[ i ];
 
}
 
}
</pre>
 
  
 +
System.out.println( "\n" + itemCount + " items.");
 +
}}
  
|SolutionCode=<pre>public class HelloWorld
+
All together we have:
{
+
 
}</pre>
+
{{CodeBlock
 +
|Code=
 +
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<br>
 +
<ol>
 +
<li>The loop for prompting the user and the loop for printing the list can be combined.</li>
 +
<li>By passing the String returned by JOptionPane to Integer.parseInt you can remove the input variable.</li>
 +
</ol>
 +
 
 +
|SolutionCode=
 +
import javax.swing.JOptionPane;
 +
 
 +
public class grocery2 {
 +
public static void main( String[] args )
 +
{
 +
 
 +
int  listSize  = 0; //the number of items for the grocery list, default 0
 +
int  itemCount = 0; //sum of the quantities of the items
 +
 +
listSize = Integer.parseInt( JOptionPane.showInputDialog("Input length:") );
 +
 
 +
//Create arrays for list items and quantity
 +
String[] groceryList    = new String[listSize];
 +
int[]    groceryQuantity = new int[listSize];
 +
 +
 +
//Print a header for the list
 +
System.out.println( "Grocery List\n------------" );
 +
 +
for( int i = 0; i < listSize; i++ )
 +
{
 +
//get the name of the item and
 +
groceryList[ i ]    = JOptionPane.showInputDialog("Item name");
 +
groceryQuantity[ i ] = Integer.parseInt( JOptionPane.showInputDialog("How many " + groceryList[ i ] + "?" ) );
 +
 +
//print the item on quantity to console
 +
System.out.println( groceryQuantity[ i ] + " " + groceryList[ i ] );
 +
 +
//add the most recent quantity to the item count
 +
itemCount = itemCount + groceryQuantity[ i ];
 +
}
 +
 +
System.out.println( "\n" + itemCount + " items.");
 +
 +
}
 +
}
  
 
}}
 
}}

Latest revision as of 16:33, 4 December 2011

Back to the Program-A-Day homepage

Problem

Write 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

Wiki method01.jpg

Solution

First, 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); 


Now that we have the number of items, listSize, we need to get the items and quantities.

 //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

  1. The loop for prompting the user and the loop for printing the list can be combined.
  2. By passing the String returned by JOptionPane to Integer.parseInt you can remove the input variable.

Code

Solution Code

Back to the Program-A-Day homepage