Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2.3 HW3 This homework assignment gives you the opportunity to practice strings (C++ string class) and chars. HW3 (Graded out of 100) Write a program

2.3 HW3 This homework assignment gives you the opportunity to practice strings (C++ string class) and chars. HW3 (Graded out of 100) Write a program that performs input validation when reading grocery product records from a file. Each grocery product record consists of the following 5 items: - PLU code: Product Look Up Code. Unique for each product, stored as a string. - Product Name, stored as a string - Product Sales Type: 0 = per unit, 1 = per pound - Price per Pound or price per unit, - Current Inventory Level (pounds or units). Below is an example of file content. In this example, there are four records, and each line corresponds to a record. A001 Apples 1 0.90 21 A002 Peaches 1 0.82 11 A006 Avocados 0 1.54 27 A008 Mangos 0 1.69 19 Your program should read a file and performs input validation on the items read, then print the outcome of the input validation: File has valid content if all the data is valid File has invalid content otherwise, along with the reason. To contain valid data, the file must have only valid records. A record is valid if It has exactly 5 items on the same line The first item should be a PLU. It is valid if and only if it contains only letters or digits, and is 4 characters long The second item should be product name. It is valid if and only if the first character is a letter The third item should be a sales type. It is valid if and only if it is only one character, and the character is a 1 or a 0 The fourth item should be a unit price. It is valid if and only if it contains only digits and at most one dot. If there is a dot, there are at most two digits after the dot The fifth item should be the inventory level. It is valid if and only if it contains only digits In order to validate the individual items, each line has first to be broken into tokens, where a token is a string that corresponds to an item. The tokens are separated by one or more delimiters, where a delimiter is a space, a tab, a new line or a carriage return. To take the line A001 Apples 1 0.90 21 as an example, the first token would be A001, the second token would be Apples, and so on. You may assume each line contains at most only one record. 1. Additional Requirements Make sure you meet all the requirements to avoid losing points a) Functions You are required to implement your program with these functions. You may implement more functions to make your program more modular if you wish. /* Returns true if the string is a valid PLU, false otherwise */ bool isValidPLU(string); /* Returns true if the string is a valid product name, false otherwise */ bool isValidName(string); /* Returns true if the string is a valid sales type, false otherwise */ bool isValidType(string); /* Returns true if the string is a valid price, false otherwise */ bool isValidPrice(string); /* Returns true if the string is a valid inventory level, false otherwise */ bool isValidInventory(string); /* Takes as reference parameter a string to be tokenized and returns the first token found Returns the empty string if no token is found The function deletes any leading delimiter and the first token found from the original string Tokenization is based on a delimiter, where a delimiter is the '\t' (tab) , ' ' (space), (new line) or (carriage return) character Example: if the string s is "\t abcd\t\t 345\t ^7$ ", the function returns "abcd" as the first token found, and modifies the string s to become "\t\t 345\t ^7$ " */ string tokenize(string &); b) Outline of main Prompt the user to enter a file name Read from the file and repeatedly call tokenize to extract the items Print the item and perform the appropriate validation. For example, for the first token which should be the PLU, perform PLU validation. For the second token, perform name validation, etc. Print the result of the validation. If an item is invalid, determine that the file is invalid and close the file. Check every line has the correct number of items. if the number of items is not 5, print an error message, determine that the file is invalid and close the file. If all validations are successful, the file is determined to be valid. Print a message to indicate if the file is valid or invalid. c) Style Make sure you follow the style requirements, especially regarding the comment header for functions, to avoid losing points. 2. Implementation Suggestions - You are not required to implement the suggestions You are allowed to use any of the existing library functions in chapter 10 of the Gaddis textbook. a) bool isFileValid(string file) Open file // Check that open is successful Read line by line. For each line: Repeatedly call tokenize to extract the items Print the item and perform the appropriate validation. Print the result of the validation. If an item is invalid, close the file and return false. Check the line has the correct number of items. if the number of items is not 5, print an error message, close the file and return false At end of file, close the file and return true Notes: To read line by line, you can use getline(inFile, stringRead) Detect end of file when getline returns an empty string b) string tokenize(string & s) Loop over the characters of s as long as they are a delimiter and the end of s is not reached // Remove any leading delimiter(s) Loop over the remaining characters as long as they are not a delimiter and the end of s is not reached // Extract the first token found store the characters into token Delete the first n characters from s // n is the number of characters looped over in the two above loops Return token 2. Grading criteria Source code inspection (grader) *Style: 5 points (refer to the Homework Notes for the style requirements) Code compilation and execution (zylabs) *Output test-1 Valid file Output exactly matches output-1: 9 points *Output test-2 File with invalid PLU Output exactly matches output-2: 9 points *Output test-3 File with invalid product name Output exactly matches output-3: 9 points *Output test-4 File with invalid sales type Output exactly matches output-4: 9 points *Output test-5 File with invalid price Output exactly matches output-5: 9 points *Output test-6 File with invalid inventory Output exactly matches output-6: 9 points *Output test-7 File with excessive number of items Output exactly matches output-7: 9 points *Output test-8 File with insufficient number of items Output exactly matches output-8: 9 points *Output test-9 File with invalid price Output exactly matches output-9: 9 points *Unit test-1 tokenize: 9 points *Unit test-2 isValidPrice: 5 points 3. Screen outputs Enter input file:in.txt Checking in.txt --------------- Token #1 is A001, PLU is valid Token #2 is Apples, Product name is valid Token #3 is 1, Sales type is valid Token #4 is 0.90, Price is valid Token #5 is 21, Inventory is valid Token #1 is A002, PLU is valid Token #2 is Peaches, Product name is valid Token #3 is 1, Sales type is valid Token #4 is 0.82, Price is valid Token #5 is 11, Inventory is valid Token #1 is A009, PLU is valid Token #2 is Strawberries_Case, Product name is valid Token #3 is 0, Sales type is valid Token #4 is 12.50, Price is valid Token #5 is 8, Inventory is valid Token #1 is 4261, PLU is valid Token #2 is Rice_1_LB_Bag, Product name is valid Token #3 is 0, Sales type is valid Token #4 is 0.49, Price is valid Token #5 is 107, Inventory is valid ######## in.txt has valid content ######## Output-1 Enter input file:in0.txt Checking in0.txt ---------------- Token #1 is A001, PLU is valid Token #2 is Apples, Product name is valid Token #3 is 1, Sales type is valid Token #4 is 0.90, Price is valid Token #5 is 21, Inventory is valid Token #1 is A00%, PLU is invalid ######## in0.txt has invalid content ######## Output-2 Enter input file:in1.txt Checking in1.txt ---------------- Token #1 is A001, PLU is valid Token #2 is Apples, Product name is valid Token #3 is 1, Sales type is valid Token #4 is 0.90, Price is valid Token #5 is 21, Inventory is valid Token #1 is A002, PLU is valid Token #2 is [eaches, Product name is invalid ######## in1.txt has invalid content ######## Output-3 Enter input file:in2.txt Checking in2.txt ---------------- Token #1 is A001, PLU is valid Token #2 is Apples, Product name is valid Token #3 is 1, Sales type is valid Token #4 is 0.90, Price is valid Token #5 is 21, Inventory is valid Token #1 is A002, PLU is valid Token #2 is Peaches, Product name is valid Token #3 is 11, Sales type is invalid ######## in2.txt has invalid content ######## Output-4 Enter input file:in3.txt Checking in3.txt ---------------- Token #1 is A001, PLU is valid Token #2 is Apples, Product name is valid Token #3 is 1, Sales type is valid Token #4 is -0.90, Price is invalid ######## in3.txt has invalid content ######## Output-5 Enter input file:in4.txt Checking in4.txt ---------------- Token #1 is A001, PLU is valid Token #2 is Apples, Product name is valid Token #3 is 1, Sales type is valid Token #4 is 0.90, Price is valid Token #5 is 21, Inventory is valid Token #1 is A002, PLU is valid Token #2 is Peaches, Product name is valid Token #3 is 1, Sales type is valid Token #4 is 0.82, Price is valid Token #5 is 11., Inventory is invalid ######## in4.txt has invalid content ######## Output-6 Enter input file:in5.txt Checking in5.txt ---------------- Token #1 is A001, PLU is valid Token #2 is Apples, Product name is valid Token #3 is 1, Sales type is valid Token #4 is 0.90, Price is valid Token #5 is 21, Inventory is valid Token #1 is A002, PLU is valid Token #2 is Peaches, Product name is valid Token #3 is 1, Sales type is valid Token #4 is 0.82, Price is valid Token #5 is 11, Inventory is valid Token #6 is &^, Too many items in record ######## in5.txt has invalid content ######## Output-7 Enter input file:in6.txt Checking in6.txt ---------------- Token #1 is A001, PLU is valid Token #2 is Apples, Product name is valid Token #3 is 1, Sales type is valid Token #4 is 0.90, Price is valid Inventory is invalid, record has missing items ######## in6.txt has invalid content ######## Output-8 Enter input file:in7.txt Checking in7.txt ---------------- Token #1 is A001, PLU is valid Token #2 is Apples, Product name is valid Token #3 is 1, Sales type is valid Token #4 is 0.907, Price is invalid ######## in7.txt has invalid content ########

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

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