Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: ArrayList and making a Menu

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    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

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList and making a Menu

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

    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:

    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

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Need help making program
    By ixjaybeexi in forum Collections and Generics
    Replies: 5
    Last Post: December 6th, 2009, 11:36 PM
  3. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. Digital map application with Java GUI
    By donjuan in forum AWT / Java Swing
    Replies: 3
    Last Post: May 15th, 2009, 03:32 AM