Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Page 2 of 4 / * * TCSS 1 4 3 B - Fundamentals Of Object - Oriented Programming Theory And Application - Fall 2

Page
2
of 4
/*
* TCSS 143 B - Fundamentals Of Object-Oriented Programming Theory And Application
- Fall 2023
* Instructor: Dr. Dongfang Zhao
* Programming Assignment 1
*/
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 143 B Instruction Team
* @version 0.1
*/
public class GroceryMain {
/**
* This is the main method for the GroceryMain class.
*
* @param theArgs is used for the commandline arguments.
*/
public static void main(String[] theArgs){
System.out.println("Welcome to the Programming Assignment 1 Grocery List
Program!");
System.out.println("This 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.println("What would you like to do ?");
System.out.println("Enter 1 to Add an item to the list");
System.out.println("Enter 2 to Display the total cost of the items");
System.out.println("Enter 3 to Modify the quantity of an item in the
list");
System.out.println("Enter 4 to print the list");
System.out.println("Enter 5 to Exit the program");
// Scanner object to read the choice/input from the user for the
operation to be
// performed on the list.
Scanner input = new Scanner(System.in);
// Reading the choice from the user and performing the operation based
on the
// choice.
int choice = input.nextInt();
// If the user enters 1 then the program will add an item to the list.
if (choice ==1){
// If the number of items in the list is 10 then the program will
display the
// below message.
if (list.getNumItems()==10){
System.out.println("Number of items in the list is 10. Cannot
add more items to the list.");
System.out.println("-----------------------------------------");
}
// If the number of items in the list is less than 10 then the
program will add
// an item to the list.
else {
System.out.println("Enter the name of the item");
String name = input.next();
System.out.println("Enter the quantity of the item");
int quantity = input.nextInt();
if (quantity <1){
System.out.println(" The Quantity cannot be less than 1.
Make sure to enter atleast 1");
}
System.out.println("Enter the price of the item");
double price = input.nextDouble();
if (price <0){
System.out.println(" Price cannnot be less than 0. Make
sure to enter more than 0");
}
list.add(new GroceryItemOrder(name, quantity, price));
System.out.println("Item added to the list successfully");
System.out.println("-----------------------------------------");
}
}
// If the user enters 2 then the program will display the total cost of
the
// items in the list.
else if (choice ==2){
System.out.println("Total cost of the items in the list so far is "
+ list.getTotalCost());
System.out.println("-----------------------------------------");
}
// If the user enters 3 then the program will modify the quantity of an
item in
// the list.
else if (choice ==3){
// If the number of items in the list is 0 then the program will
display the
// below message.
if (list.getNumItems()==0){
System.out.println("No items in the list to modify");
System.out.println("-----------------------------------------");
}
// If the number of items in the list is not 0 then the program
will modify the
// quantity of an item in the list.
else {
System.out.println("-----------------------------------------");
System.out.println("The items in the list are");
System.out.println(list.toString());
System.out
.println("Please make sure to enter the index of the
item correctly(Index starts from 0)");
System.out.println("Enter the index of the item to modify the
quantity");
int index = input.nextInt();
if (index <0){
System.out.println(" The index cannot be less than 0");
}
System.out.println("Enter the new quantity of the item");
int quantity = input.nextInt();
if (quantity <1){
System.out.println(" The Quantity cannot be less than 1.
Make sure to enter atleast 1");
}
list.modifyItem(index, quantity);
System.out.println("Item Quantity modified successfully");
System.out.println("-----------------------------------------");
}
}
// If the user enters 4 then the program will print the list.
else if (choice ==4){
// If the number of items in the list is 0 then the program will
display the
// below message.
if (list.getNumItems()==0){
System.out.println("No items in the list to print");
System.out.println("-----------------------------------------");
}
// If the number of items in the list is not 0 then the program
will print the

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

9. Describe the characteristics of power.

Answered: 1 week ago

Question

3. Identify and describe nine cultural value orientations.

Answered: 1 week ago