Go Back   Java Programming Forums > Java Standard Edition Programming Help > Collections and Generics

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 28-01-2010, 12:01 AM
Junior Member
 

Join Date: Jan 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Silver is on a distinguished road

I'm feeling Confused
Default ArrayList and making a Menu

Hello everyone, I'm currently making a program that is going to be designed to keep track of inventory for a near by church pantry. I'm using ArrayList to keep track of the ingredients and the type of unit they are stored in. For example: " One gallon-the unit of milk-the ingredient" And I'm making a user interface for the person behind the counter so she can use it easily, so I've made a method to contain that. Here's what I have so far in main:
Java Code
import java.util.*;
public class Driver {

	Scanner keyboard = new Scanner(System.in);
	public static void main(String[] args) {
		
		
		
	}
		public void menu()
	{
		ArrayList ingredients = new ArrayList();
	   int menuChoice;
		System.out.println("Please select an option");
		System.out.println("1)Add inventory");
		System.out.println("2)Stock inventory");
		System.out.println("3)Use ingredients");
		System.out.println("4)Display ingredients");
		System.out.println("5)Exit program");
		
		menuChoice = keyboard.nextInt();
		if(menuChoice < 1 || menuChoice > 5)
			System.out.println("Invalid please try again");
			menuChoice = keyboard.nextInt();
			if(menuChoice==1)

				//S.O.P "What type of ingredient would you like to add"
				//S.O.P "What type of unit is "ingredient" stored in"
				//S.O.P "How many "units" of "ingredient" do you have"
			else if(menuChoice==2)

				//S.O.P "What type of ingredient would you like to add?"
				//S.O.P "How many "units" of "ingredient" would you like to add"?
			else if (menuChoice==3)

				//S.O.P "What type of ingredient are you wanting to use?"
				//S.O.P "How many "units" of "ingredient" are you wanting to use?
			else if (menuChoice==4)

					System.out.println(ingredients);
			else
				System.out.println("Thank you for using the Ingredient Manager");
Now I've forgotten to mention that I'm new to the ArrayList idea... But I know this is the most effecient way to do it. I'm working on this bit by bit, and right now what I need to do is work on option one; which is to add inventory. This means I have to be able to make a method to store what the user imputs into the array ingredients and store the type of unit and be able to repeat it back to the user for friendliness reasons. Any help or additional comments or if you see any errors in my code thus far for the menu would be greatly appreciated



Reply With Quote Share this thread on Facebook
Sponsored Links
  #2 (permalink)  
Old 28-01-2010, 03:17 AM
Member
 

Join Date: Oct 2009
Posts: 311
Thanks: 4
Thanked 89 Times in 83 Posts
copeg will become famous soon enough

I'm feeling Mellow
Default Re: ArrayList and making a Menu

An ArrayList acts very similar to an array, but it is a Class and access to its element is through its instance methods. To add what you wish to the list, you may wish to create a Class (say Ingredient) which specifies what you wish to specify for each Ingredient (name/quantity/etc...), then add these to your ArrayList (using the add(Object o) method). You can then access these Objects through the List, getting/setting their data as needed. I hope this helps
Reply With Quote
  #3 (permalink)  
Old 28-01-2010, 08:29 AM
Junior Member
 

Join Date: Jan 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Silver is on a distinguished road

I'm feeling Confused
Default Re: ArrayList and making a Menu

Okay after much thought and deliberation over this thing here's what I have accomplished:

Java Code
import java.util.*;
public class Driver {

	 
	public static void main(String[] args) 
	{
		Scanner keyboard = new Scanner(System.in);
		   ArrayList <Inventory>ingredients = new ArrayList<Inventory>(); 
		   String name;
		   String measurement;
		   int units;
		   int menuChoice=menu();
		   int i;
		   int addOn;
		
		   while(menuChoice!=5)
		   {
			if(menuChoice==1)
			{
				System.out.println("What type of ingredient would you like to add?");
				name=keyboard.nextLine();
				System.out.println("What is "+name+" measured in?");
				measurement=keyboard.nextLine();
				System.out.println("");
				System.out.println("How many  "+measurement+" of  "+name+" do you have?");
				units=keyboard.nextInt();
				keyboard.nextLine();
				System.out.println("");
				Inventory foodItem = new Inventory(name, measurement, units);
				ingredients.add(foodItem);
				
				
			}
				
			else if(menuChoice==2)
			{
				for(i=0; i<ingredients.size(); i++)
				{
					System.out.println(i +") "+ingredients.get(i).toString());
				}
				System.out.println("What would you like to add to?");
				int x = keyboard.nextInt();
				addOn=ingredients.get(x).getUnits();
				System.out.println("How many "+ingredients.get(x).getMeasurment()+" would you like to stock?");
				addOn+=keyboard.nextInt();
			    ingredients.get(x).setUnits(addOn);

			    				 
			}
			else if (menuChoice==3)
			{
				for(i=0; i<ingredients.size(); i++)
				{
					System.out.println(i +") "+ingredients.get(i).toString());
				}
				System.out.println("Which item would you like to use?");
				int x = keyboard.nextInt();
				addOn=ingredients.get(x).getUnits();
				System.out.println("How many "+ingredients.get(x).getMeasurment()+" would you like to use?");
				addOn-=keyboard.nextInt();
			    ingredients.get(x).setUnits(addOn);

			}
			else if (menuChoice==4)
			{
			for(i=0; i<ingredients.size(); i++)
				System.out.println(ingredients.get(i).toString());
			}
			menuChoice=menu();
		   }
			
	
	}
	public static int menu()
    {
		Scanner keyboard = new Scanner(System.in);
		int menuChoice;

		
		System.out.println("Please select an option");
		System.out.println("1)Add inventory");
		System.out.println("2)Stock inventory");
		System.out.println("3)Use ingredients");
		System.out.println("4)Display ingredients");
		System.out.println("5)Exit program");
		menuChoice = keyboard.nextInt();
		
		while(menuChoice < 1 || menuChoice > 5)
		{
			System.out.println("Invalid please try again");
			menuChoice = keyboard.nextInt();
		}
		return menuChoice;
		
		
				
		
		

	}

}
And for my inventory class:

Java Code
public class Inventory 
	{
	int units;
	String name;
	String measurment;
	public Inventory(String nameIn,String measurmentIn, int unitsIn)
	{
		name=nameIn;
		measurment=measurmentIn;
		units=unitsIn;
		
	}
	
	
	public int getUnits() {
		return units;
	}


	public void setUnits(int units) {
		this.units = units;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getMeasurment() {
		return measurment;
	}


	public void setMeasurment(String measurment) {
		this.measurment = measurment;
	}


	public String toString()
	{
	String foodInfo="you have "+units+" "+measurment+" of "+name;
	return foodInfo;
	}
}
Now I have a couple more questions, I need to set parameter limits on using inventory. As in, if you use more than whats in stock print out an error message to the user. And for the other question, How would I make it to when an item gets to 0 It will delete it's self from the ArraylList?

Also once again, any comments, suggestions to improve, and any catch in an error would be very much appreciated <3. Thanks again
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help making program ixjaybeexi Collections and Generics 5 07-12-2009 04:36 AM
Arraylist or Arraylist Object? igniteflow Collections and Generics 2 11-09-2009 07:08 AM
[SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int?? igniteflow Collections and Generics 2 16-08-2009 06:11 PM
How to use an ArrayList - java.util.ArrayList JavaPF Java Code Snippets and Tutorials 1 17-05-2009 06:12 PM
[ASK]making a map donjuan AWT / Java Swing 3 15-05-2009 08:32 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java ipod touch java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java jtextarea font color programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 02:47 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.