Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sample input text: HW3 (Graded out of 100) Write a program that performs input validation when reading grocery product records from a file. Each grocery

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedSample input text:

image text in transcribed

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 ora 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 are encouraged to implement more functions to make your program more modular if you wish. /* Returns true if the string is a valid price, false otherwise bool isValidPrice (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 strings 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 47$ " string tokenize (string &); b) Style Make sure you follow the style requirements, especially regarding the comment header for functions, to avoid losing points. 2. Implementation Notes You are allowed to use any of the existing library functions in chapter 10 of the Gaddis textbook. a) Suggested outline of program's logic - Not all details are shown Have an outer loop to repeatedly read the file line by line. Exit the loop at the end of the file Inner loop: for each line, repeatedly call tokenize to extract the tokens, up to the last one Count the number of tokens extracted so far for the line If the number of tokens so far > 5, print the appropriate error message(s) and terminate Check if the item is invalid. If so, print the appropriate error message(s) and terminate If the number of tokens for the line 5, print the appropriate error message(s) and terminate Check if the item is invalid. If so, print the appropriate error message(s) and terminate If the number of tokens for the line

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: 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

Recommended Textbook for

Handbook Of Database Security Applications And Trends

Authors: Michael Gertz, Sushil Jajodia

1st Edition

1441943056, 978-1441943057

More Books

Students also viewed these Databases questions