This assignment should use the concepts, Java implementations, and applications
ID: 3554404 • Letter: T
Question
This assignment should use the concepts, Java implementations, and applications of the following:
Class inheritance
Polymorphism
File I/O and arrays
Exceptions
Design a Java application to emulate operations at a grocery store that sells 3 kinds of Items/Products: Dairy, Vegi and Meat. The user may log in as a Manager or a Client.
A Manager has access to the Inventory which must be an Array of at least 10 Items and can list, add, delete or modify Items at the store. A client shops at the store by editing his Shopping Cart which can also be an Array of Items. A client can choose to open his old Shopping Cart and continue shopping or start a new one. After a client checks out, his Shopping Cart can be cleared and a receipt must be printed, which shows details of all the purchased Items. Both Inventory and a Client's Shopping Cart have to be also kept updated in files.
Each Item/Product at the store has the following data:
name
itemCode: first part indicates the Item's category; second part indicates the brand
unitPrice
totalQuantity
Each Dairy item also includes data for amount of fat; Vegi also includes amount of fiber; Meat also includes amount of protein.
The program must include the following classes:
Item
Dairy
Vegi
Meat
Inventory
ShoppingCart
Dairy, Vegi and Meat must inherit Item.
Any error in the program must throw an exception.
Explanation / Answer
import java.util.Scanner; import java.util.InputMismatchException; /** * Implements a Grocery List * Version 2: Has tested & revised code for main() and displayMenu() methods */ public class GroceryListProgram2 { /** * The main() Method Starts The Program. * @param commandlineArguments 1st argument is INPUT File, * 2nd argument is OUTPUT File */ public static void main(String[] commandlineArguments) { //Error Checking For 2 Command Line Arguments.. if(commandlineArguments.length != 2){ System.out.println("Please enter the INPUT file name as " + "the 1st commandline argument."); System.out.println("Please enter the OUTPUT file name as " + "the 2nd commandline argument."); System.out.println("Please enter exactly " + "two (2) commandline arguments."); }//end of if //if no commandline argument errors, continue program else{ //read grocery items from file & store in array for grocery list String groceryList[] = GroceryListProgram2.readFromFile(commandlineArguments[0]); //size of grocery list Integer size = groceryList.length; //user's choice Integer choice = new Integer(0); //choice for ending program final Integer QUIT = new Integer(4); //if the user does not want to QUIT, keep looping while(!choice.equals(QUIT)){ //get the user's choice choice = GroceryListProgram2.displayMenu(); //add grocery item if(choice.equals(1)){ size = GroceryListProgram2.add(groceryList, size); } //delete grocery item else if(choice.equals(2)){ size = GroceryListProgram2.delete(groceryList, size); } //display grocery item else if(choice.equals(3)){ GroceryListProgram2.display(groceryList, size); } //error message else if(!choice.equals(QUIT)){ System.out.println("ERROR: Please enter an integer in the range from 1 to 4"); } }//end of "while" //write to from grocery list array to OUTPUT file GroceryListProgram2.writeToFile(commandlineArguments[1], groceryList, size); }//end of "else" }//end of main() method /** * Displays the menu for the program and returns user's choice * @return the choice of the user (if error, returns 0) */ public static Integer displayMenu(){ //display menu System.out.println(" GROCERY LIST MENU"); System.out.println(" Enter 1 to Add"); System.out.println(" Enter 2 to Delete"); System.out.println(" Enter 3 to Display"); System.out.println(" Enter 4 to Quit"); System.out.print(" Enter your choice: "); //get input from user Scanner keyboardInput = new Scanner(System.in); Integer choiceOfUser = new Integer(0); try{ //non-integer input will throw InputMismatchException choiceOfUser = keyboardInput.nextInt(); } catch(InputMismatchException exception){ //Already have error message in main() method, //as choiceOfUser = 0 } return choiceOfUser; } /** * Reads grocery items from a file and stores items in an array * @param inputFile is the INPUT File * @return an array of grocery items */ public static String[] readFromFile(String inputFile){ System.out.println("readFromFile method"); //return empty array return new String[0]; } /** * Adds a grocery item to an array * @param list is the grocery list * @param listSize is the size of the grocery list * @return new size of the grocery list */ public static Integer add(String [] list, Integer listSize){ System.out.println("add method"); //we add one to the size (one item to end of list) return listSize + 1; } /** * Deletes a grocery item from an array * @param list is the grocery list * @param listSize is the size of the grocery list * @return new size of the grocery list */ public static Integer delete(String [] list, Integer listSize){ System.out.println("delete method"); //we subtract one from the size (one item deleted from list) return listSize - 1; } /** * Displays a grocery list * @param list is the grocery list * @param listSize is the size of the grocery list */ public static void display(String [] list, Integer listSize){ System.out.println("display method"); } /** * Write grocery list array to file * @param list is the grocery list * @param listSize is the size of the grocery list */ public static void writeToFile(String outputFile, String [] list, Integer listSize){ System.out.println("writeToFile method"); } }//end of class