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:
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 :)
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
Re: ArrayList and making a Menu
Okay after much thought and deliberation over this thing here's what I have accomplished:
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:
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