Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Controller {
- private Scanner scanner = new Scanner(System.in);
- private Items items;
- public Controller(){
- this.items = new Items();
- }
- private void showAction(){
- this.items.showItem();
- }
- private void addItem(){
- System.out.print("Add Item: ");
- String item = scanner.nextLine();
- this.items.addItem(item);
- }
- private void editItem(){
- System.out.println("Enter current name: ");
- String item = scanner.nextLine();
- System.out.print("Enter new Item name: ");
- String newitem = scanner.nextLine();
- this.items.editItem(item,newitem);
- }
- private void removeItem(){
- System.out.print("Remove Item: ");
- String item = scanner.nextLine();
- this.items.removeItem(item);
- }
- private void searchItem(){
- System.out.print("Enter item name: ");
- String item = scanner.nextLine();
- this.items.searchItem(item);
- }
- public void print(){
- System.out.println("t 0. Show Command");
- System.out.println("t 1. Add Item");
- System.out.println("t 2. Edit Item");
- System.out.println("t 3. Remove Item");
- System.out.println("t 4. Search Item");
- System.out.println("t 5. Show Item");
- System.out.println("t 6. Exit Application");
- }
- public void Execute(){
- boolean quit = false;
- int choice = 0;
- while(!quit){
- System.out.print("Enter command: ");
- choice = scanner.nextInt();
- scanner.nextLine();
- switch(choice){
- case 0:
- print();
- break;
- case 1:
- addItem();
- break;
- case 2:
- editItem();
- break;
- case 3:
- removeItem();
- break;
- case 4:
- searchItem();
- break;
- case 5:
- showAction();
- break;
- case 6:
- quit = true;
- break;
- }
- }
- }
- }
- public class Items {
- private ArrayList<String> list = new ArrayList<>();
- public void addItem(String item){
- list.add(item);
- }
- public void showItem(){
- System.out.println("you have " + list.size() + " in you grocery list");
- for (int i =0;i<list.size();i++){
- System.out.println((i + 1) + ". " + list.get(i));
- }
- }
- public void editItem(String currentItem,String item){
- int position = searchItem(item);
- if(position>=0){
- editItem(position,item);
- System.out.println("Item: " + currentItem + " has been modified to: " + item);
- }
- }
- private void editItem(int position, String item){
- list.set(position,item);
- }
- public void removeItem(String item){
- int position = searchItem(item);
- if(position>=0){
- removeItem(position);
- }
- }
- private void removeItem(int position){
- //String item = list.get(position);
- list.remove(position);
- }
- public int searchItem(String item){
- return list.indexOf(item);
- }
- }
- public static void main(String[] args) {
- Controller controller = new Controller();
- controller.print();
- controller.Execute();
- }
- if(position>=0){
- removeItem(position);
- }
- if(position >= 0) removeItem(position);
- do {
- System.out.print("Enter command within range (inclusive) 0-6: ");
- while(!scanner.hasNextInt()) {//continue iterating while the value held in scanner is not an integer
- System.out.print("Your input must be an integer, please re-enter: ");
- scanner.next();//take another input
- }
- choice = scanner.nextInt();//sets 'choice' to the integer just entered into the scanner
- }while(choice < 0 || choice > 6);
Add Comment
Please, Sign In to add comment