You are on page 1of 4

//This is the Menu for my project, It will just gather input from the user and will

call out the functions I created on the other side


//It will also have the Main loop which will control the Menu

//I am importing Java Utility for me to use all of the commands there specially the
Scanner.
package barilproject;
import java.util.*;

/*I created 2 Private class which is can only access what's


inside of my class and using static for me to access using class in other methods
later*/
public class BarilProject {
//First Private class is a scanner. This will input something from the user and
private class to access methods much safer and to avoid messing up the other codes.
//I also declared a new variable in it to access and modify its value.
private static Scanner scanner = new Scanner (System.in);
//Second one is Private class Itemlist which I used to import my functions and
methods on the other class I made
private static ItemList itemList = new ItemList();

public static void main(String[] args) {


//This boolean variable is to stop the loop
boolean quit = false;
//This one is for the loop to know where to stop
//It is called incrementing
int choice = 0;
//This line is for me to call out the code
showChoices();

//This is the loop for my Menu


//Using ! so if the variable quit is change to true then the menu will
close
while (! quit){
//A simple line that will show at the start
System.out.println("What do you want to do?");
/*A Line that will determine what function will execute,
As you can see at the top I created an Int variable which is 0 that
will hold no value but still output a set of line later*/
choice = scanner.nextInt();
//This is to get the input of the user
scanner.nextLine();
//The loop I created for my Menu to have function
/*I used switch caused it is easier to build and when creating a Menu
like this which is a user will choose one and that
selected function will do its work then stop cause of the break code*/
switch (choice){
//This is what I am talking earlier the choice variable is equals
to 0, so it will automatically show this without gathering any input from the user
case 0:
showChoices();
break;
//I will explain these functions below, for now I created this to
call the functions and a break line to stop the loop and focus to the function
case 1:
itemList.printItemList();
break;
case 2:
addItem();
break;
case 3:
changeitem();
break;
case 4:
removeItem();
break;
case 5:
searchItem();
break;
//The loop will not stop until the user choose this case which will
change the value of the variable quit which is false to true then the loop will
stop
case 6:
quit = true;
break;

}
}
}
/*
This for the case 0, This is the Menu that will automatically shows up cause
like I said earlier the value of choice from the start without
the user input is 0 that means it will show or output this line of words there
*/
public static void showChoices(){
System.out.println("\n Enter a Number:");
System.out.println("\t Choice options:");
System.out.println("\t 1.Show Items");
System.out.println("\t 2.Add Item");
System.out.println("\t 3.Change Item");
System.out.println("\t 4.Remove Item");
System.out.println("\t 5.Search Item");
System.out.println("\t 6.Quit ");
/*Case 1 doesn't need any input from the user cause it will just show the Items
that the user putted using case 2 to the arraylist I created on the other class*/
/*This is the case 2 thats a function that will add an Item to your ItemArray,
This code will not explain this cause
Id just called a function that will gather input from the user, I will
explain how it works on the other class I created*/
}
public static void addItem(){
System.out.println("Add Item: ");
itemList.additem(scanner.nextLine());
/*This is the case 3 which will change the Item you want, for the case 2 It
will not also explain how to function works but I will explain it later,
This lines of codes will only gather input from the user for the postion of
the item he wants to change and what Item is it
*/
}
public static void changeitem(){
System.out.println("Item Number: ");
int ItemPos = scanner.nextInt();
scanner.nextLine();
System.out.println("New Item: ");
String NewItem = scanner.nextLine();
itemList.changeItem (ItemPos - 1, NewItem);
}

public static void removeItem(){


System.out.println("Item Number: ");
int ItemPos = scanner.nextInt();
scanner.nextLine();
itemList.removeItem(ItemPos -1);
}
public static void searchItem(){
System.out.println("Item you want to find: ");

String searchItem = scanner.nextLine();

if(itemList.findItem(searchItem) != null){
System.out.println("Found " + searchItem + " in your List");
}
else{
System.out.println(searchItem + " is not your List");
}

}
}

package barilproject;
import java.util.*;

public class ItemList {


private ArrayList<String> ItemList = new ArrayList<>();

public void additem(String item){


ItemList.add(item);
}

public void printItemList(){


System.out.println("You have " + ItemList.size() + " items in your list");
for (int i = 0; i < ItemList.size(); i ++ ){
System.out.println((i+1)+ "." + ItemList.get(i));
}
}

public void changeItem(int position, String newItem){


ItemList.set(position, newItem);
System.out.println("Item" + (position + 1) + " has been changed");
}

public void removeItem(int position){


String Item = ItemList.get(position);
ItemList.remove(position);
}

public String findItem(String searchItem){


int position = ItemList.indexOf(searchItem);
if (position >= 0){
return ItemList.get(position);

}
return null;
}
}

You might also like