Hi! I am needing some help with this project. I do not know where to begin. It i
ID: 3729574 • Letter: H
Question
Hi! I am needing some help with this project. I do not know where to begin. It is in Java.
(1) Prompt the user to input a positive number - the number of gift items - must be positive or print error and re-enter.
sample output:
Input number of gift items: -2
ERROR: number of items must be greater than zero.
Input number of gift items: 0
ERROR: number of items must be greater than zero.
Input number of gift items: 3
(2) Create an array of gift item names and gift item quantity.
(3) Ask the user to input the name of each gift item and store into an array.
sample output:
Input name of gift item 1: ham
Input name of gift item 2: egg
Input name of gift item 3: cheese
(4) Display a menu as in the following:
sample output:
(5) Prompt and read a command from the user, and perform the action.
sample output:
(6) Show basket will list all gift item names and its quantity - see example below
sample output:
(7) Add a gift item will add 1 to the quantity of a particular gift item. It will ask the user to input gift item name and add 1 to the corresponding gift item quantity; if not found, print error. Tell user that item added if success.
sample output:
(8) Delete a gift item will ask the user to input gift item name and delete 1 to the corresponding gift item quantity; if less than zero, set to zero; if not found, print error. Tell user that item deleted if success.
sample output:
(9) Empty basket will set all gift item quantities to zero. Tell use that basket is emptied.
sample output:
(10) Quit will tell the user that the program is terminated.
sample output:
(11) If command is not valid, print error.
sample output:
Here is what I have so far. It is extremely rough and needs a lot of help.
import java.util.Scanner;
public class Project2 {
public static void main( String [] args ) {
Scanner sc = new Scanner(System.in);
int numGifts = sc.nextInt();
while (true){
System.out.print("Input number of gift items: ");
numGifts = sc.nextInt();
if(numGifts <= 0){
System.out.println("ERROR: number of items must be greater than zero.");
}
else{
break;
}
}
String giftItems[] = new String[numGifts];
giftItems[0] = sc.nextLine();
for(int i = 0;i<numGifts;i++){
System.out.print("Input name of gift item "+(i+1)+": ");
giftItems[i] = sc.nextLine();
}
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(“---------------------”);
}
String cmd;
Cmd = sc.next();
Char cmdChar;
cmdChar = cmd.charAt(0);
String model;
Int j:
Swith (cmdChar) {
Case ‘a’:
Case ‘A’:
Explanation / Answer
import java.util.Scanner;
public class Project2 {
public static void main( String [] args ) {
Scanner sc = new Scanner(System.in);
int numGifts;
while (true)
{
System.out.print("Input number of gift items: ");
numGifts = sc.nextInt();
if(numGifts <= 0)
{
System.out.println("ERROR: number of items must be greater than zero.");
}
else
{
break;
}
}
String giftItems[] = new String[numGifts];
int giftQuantity[]=new int[numGifts];
giftItems[0] = sc.nextLine();
for(int i = 0;i<numGifts;i++)
{
System.out.print("Input name of gift item "+(i+1)+": ");
giftItems[i] = sc.nextLine();
}
char cmdChar;
String model;
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("---------------------");
String cmd;
cmd = sc.next();
cmdChar = cmd.charAt(0);
switch (cmdChar)
{
case 'a':
case 'A':
{
int count=0;
System.out.println("Input gift item name: ");
model=sc.next();
for(int i=0;i<numGifts;i++)
{
if(giftItems[i].equals(model))
{
giftQuantity[i]++;
count=1;
break;
}
}
if(count==1)
{
System.out.println("Item Added");
}
else
{
System.out.println("ERROR: "+model+" not found");
}
break;
}
case 'd':
case 'D':
{
System.out.println("Input gift item name: ");
model=sc.next();
for(int i=0;i<numGifts;i++)
{
if(giftItems[i].equals(model))
{
if(giftQuantity[i]<=0)
{
System.out.println("ERROR: "+model+" not found");
}
else{
giftQuantity[i]--;
System.out.println("Item Deleted");
}
break;
}
}
break;
}
case 'e':
case 'E':
{
for(int i=0;i<numGifts;i++)
{
giftQuantity[i]=0;
}
System.out.println("Basket emptied");
break;
}
case 's':
case 'S':
{
for(int i=0;i<numGifts;i++)
{
System.out.println((i+1)+" "+giftItems[i]+": "+giftQuantity[i]);
}
break;
}
case 'q':
case 'Q':
{
System.out.println("Program terminated");
break;
}
default: {
System.out.println("ERROR: Invalid input");
break;
}
}
}while(cmdChar!='q'|| cmdChar!='Q');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.