Answered step by step
Verified Expert Solution
Question
1 Approved Answer
IN JAVA PLEASE HELP: Convert the project 2 code as posted and break into methods: enterGifts(); printMenu(); addGifts(); deleteGifts(); emptyBasket(); showBasket(); below is the code
IN JAVA PLEASE HELP: Convert the project 2 code as posted and break into methods: enterGifts(); printMenu(); addGifts(); deleteGifts(); emptyBasket(); showBasket(); below is the code for project 2:
import java.util.Scanner; // public class CS220_Project2 { public static void main(String[] args) { // Scanner sc = new Scanner(System.in); // int i; // int numItems; // do { // System.out.println("Input number of gift items: "); numItems = sc.nextInt(); // if (numItems < 1) { System.out.println("ERROR: number of items must be greater than zero."); } // } while (numItems < 1); // // String[] itemNames = new String[numItems]; int[] itemQty = new int[numItems]; // for (i = 0; i < numItems; ++i) { // System.out.println("Input name of gift item "+(i+1)+": "); itemNames[i] = sc.next(); // itemQty[i] = 0; } // char cmd; // do { // System.out.println("---------------------"); System.out.println("MENU"); System.out.println("---------------------"); System.out.println("(A)dd a gift item"); System.out.println("(D)elete a gift item"); System.out.println("(E)mpty basket"); System.out.println("(S)how basket"); System.out.println("(Q)uit"); System.out.println("---------------------"); System.out.println("Please enter command: "); // String input = sc.next(); // cmd = input.charAt(0); // switch (cmd) { // case 'a': case 'A': // System.out.println("Input gift item name: "); String name = sc.next(); //***YOU CAN DO IT THIS WAY // for (i = 0; i < itemNames.length; ++i) { // if (name.toLowerCase().equals(itemNames[i].toLowerCase())) { // break; } } // if (i < itemNames.length) { // ++itemQty[i]; System.out.println("Item added." ); } else { // System.out.println("ERROR: "+name+" not found." ); } break; // case 'd': case 'D': // System.out.println("Input gift item name: "); name = sc.next(); //****OR YOU CAN DO IT THIS WAY // boolean found = false; // // for (i = 0; i < itemNames.length; ++i) { // if (name.toLowerCase().equals(itemNames[i].toLowerCase())) { // itemQty[i] = itemQty[i] - 1; // if (itemQty[i] < 0) itemQty[i] = 0; // found = true; } } // if (found) { System.out.println("Item deleted." ); } else { System.out.println("ERROR: "+name+" not found." ); } break; // case 'e': case 'E': // for (i = 0; i < itemQty.length; ++i) { // itemQty[i] = 0; } System.out.println("Basket emptied."); break; // case 's': case 'S': // for (i = 0; i < itemNames.length; ++i) { // System.out.println((i+1) + ") " + itemNames[i] + ": " + itemQty[i]); } break; // case 'q': case 'Q': break; // default: System.out.println("ERROR: Invalid command."); } // } while (!(cmd == 'q' || cmd == 'Q')); // System.out.println("Program terminated."); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started