Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Page 2 of 4 / * * TCSS 1 4 3 B - Fundamentals Of Object - Oriented Programming Theory And Application - Fall 2
Page
of
TCSS B Fundamentals Of ObjectOriented Programming Theory And Application
Fall
Instructor: Dr Dongfang Zhao
Programming Assignment
import java.util.Scanner;
This is the main class for the GroceryList program. This program will perform
some operations on the list.
The operations are adding an item to the list, displaying the total cost of
the items, modifying the quantity of an item in the list and printing the
list.
@author TCSS B Instruction Team
@version
public class GroceryMain
This is the main method for the GroceryMain class.
@param theArgs is used for the commandline arguments.
public static void mainString theArgs
System.out.printlnWelcome to the Programming Assignment Grocery List
Program!";
System.out.printlnThis test program will perform some operations on the
list";
Creates an object of the GroceryList class.
GroceryList list new GroceryList;
While loop to prompt the user to enter the choice for the operation to
be
performed on the list untill the user wants to exit.
while true
System.out.printlnWhat would you like to do ;
System.out.printlnEnter to Add an item to the list";
System.out.printlnEnter to Display the total cost of the items";
System.out.printlnEnter to Modify the quantity of an item in the
list";
System.out.printlnEnter to print the list";
System.out.printlnEnter to Exit the program";
Scanner object to read the choiceinput from the user for the
operation to be
performed on the list.
Scanner input new ScannerSystemin;
Reading the choice from the user and performing the operation based
on the
choice.
int choice input.nextInt;
If the user enters then the program will add an item to the list.
if choice
If the number of items in the list is then the program will
display the
below message.
if listgetNumItems
System.out.printlnNumber of items in the list is Cannot
add more items to the list.";
System.out.println;
If the number of items in the list is less than then the
program will add
an item to the list.
else
System.out.printlnEnter the name of the item";
String name input.next;
System.out.printlnEnter the quantity of the item";
int quantity input.nextInt;
if quantity
System.out.println The Quantity cannot be less than
Make sure to enter atleast ;
System.out.printlnEnter the price of the item";
double price input.nextDouble;
if price
System.out.println Price cannnot be less than Make
sure to enter more than ;
list.addnew GroceryItemOrdername quantity, price;
System.out.printlnItem added to the list successfully";
System.out.println;
If the user enters then the program will display the total cost of
the
items in the list.
else if choice
System.out.printlnTotal cost of the items in the list so far is
list.getTotalCost;
System.out.println;
If the user enters then the program will modify the quantity of an
item in
the list.
else if choice
If the number of items in the list is then the program will
display the
below message.
if listgetNumItems
System.out.printlnNo items in the list to modify";
System.out.println;
If the number of items in the list is not then the program
will modify the
quantity of an item in the list.
else
System.out.println;
System.out.printlnThe items in the list are";
System.out.printlnlisttoString;
System.out
printlnPlease make sure to enter the index of the
item correctlyIndex starts from ;
System.out.printlnEnter the index of the item to modify the
quantity";
int index input.nextInt;
if index
System.out.println The index cannot be less than ;
System.out.printlnEnter the new quantity of the item";
int quantity input.nextInt;
if quantity
System.out.println The Quantity cannot be less than
Make sure to enter atleast ;
list.modifyItemindex quantity;
System.out.printlnItem Quantity modified successfully";
System.out.println;
If the user enters then the program will print the list.
else if choice
If the number of items in the list is then the program will
display the
below message.
if listgetNumItems
System.out.printlnNo items in the list to print";
System.out.println;
If the number of items in the list is not then the program
will print the
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