Guest User

Untitled

a guest
Mar 13th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. public class Controller {
  2.  
  3. private Scanner scanner = new Scanner(System.in);
  4. private Items items;
  5. public Controller(){
  6. this.items = new Items();
  7. }
  8.  
  9. private void showAction(){
  10. this.items.showItem();
  11. }
  12.  
  13. private void addItem(){
  14. System.out.print("Add Item: ");
  15. String item = scanner.nextLine();
  16. this.items.addItem(item);
  17. }
  18.  
  19. private void editItem(){
  20. System.out.println("Enter current name: ");
  21. String item = scanner.nextLine();
  22. System.out.print("Enter new Item name: ");
  23. String newitem = scanner.nextLine();
  24. this.items.editItem(item,newitem);
  25. }
  26.  
  27. private void removeItem(){
  28. System.out.print("Remove Item: ");
  29. String item = scanner.nextLine();
  30.  
  31. this.items.removeItem(item);
  32. }
  33.  
  34. private void searchItem(){
  35. System.out.print("Enter item name: ");
  36. String item = scanner.nextLine();
  37. this.items.searchItem(item);
  38. }
  39.  
  40. public void print(){
  41. System.out.println("t 0. Show Command");
  42. System.out.println("t 1. Add Item");
  43. System.out.println("t 2. Edit Item");
  44. System.out.println("t 3. Remove Item");
  45. System.out.println("t 4. Search Item");
  46. System.out.println("t 5. Show Item");
  47. System.out.println("t 6. Exit Application");
  48. }
  49.  
  50. public void Execute(){
  51. boolean quit = false;
  52. int choice = 0;
  53.  
  54. while(!quit){
  55. System.out.print("Enter command: ");
  56. choice = scanner.nextInt();
  57. scanner.nextLine();
  58. switch(choice){
  59. case 0:
  60. print();
  61. break;
  62. case 1:
  63. addItem();
  64. break;
  65. case 2:
  66. editItem();
  67. break;
  68. case 3:
  69. removeItem();
  70. break;
  71. case 4:
  72. searchItem();
  73. break;
  74. case 5:
  75. showAction();
  76. break;
  77. case 6:
  78. quit = true;
  79. break;
  80. }
  81. }
  82. }
  83. }
  84.  
  85. public class Items {
  86. private ArrayList<String> list = new ArrayList<>();
  87.  
  88. public void addItem(String item){
  89. list.add(item);
  90. }
  91.  
  92. public void showItem(){
  93. System.out.println("you have " + list.size() + " in you grocery list");
  94. for (int i =0;i<list.size();i++){
  95. System.out.println((i + 1) + ". " + list.get(i));
  96. }
  97. }
  98.  
  99. public void editItem(String currentItem,String item){
  100. int position = searchItem(item);
  101. if(position>=0){
  102. editItem(position,item);
  103. System.out.println("Item: " + currentItem + " has been modified to: " + item);
  104. }
  105. }
  106.  
  107. private void editItem(int position, String item){
  108. list.set(position,item);
  109. }
  110.  
  111. public void removeItem(String item){
  112. int position = searchItem(item);
  113. if(position>=0){
  114. removeItem(position);
  115. }
  116. }
  117.  
  118. private void removeItem(int position){
  119. //String item = list.get(position);
  120. list.remove(position);
  121. }
  122.  
  123. public int searchItem(String item){
  124. return list.indexOf(item);
  125. }
  126. }
  127.  
  128. public static void main(String[] args) {
  129. Controller controller = new Controller();
  130. controller.print();
  131. controller.Execute();
  132. }
  133.  
  134. if(position>=0){
  135. removeItem(position);
  136. }
  137.  
  138. if(position >= 0) removeItem(position);
  139.  
  140. do {
  141. System.out.print("Enter command within range (inclusive) 0-6: ");
  142. while(!scanner.hasNextInt()) {//continue iterating while the value held in scanner is not an integer
  143. System.out.print("Your input must be an integer, please re-enter: ");
  144. scanner.next();//take another input
  145. }
  146. choice = scanner.nextInt();//sets 'choice' to the integer just entered into the scanner
  147. }while(choice < 0 || choice > 6);
Add Comment
Please, Sign In to add comment