Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is in java eclipse. The program should recognize when you have inputed more

ID: 3746745 • Letter: T

Question

This is in java eclipse.

The program should recognize when you have inputed more than 1 of an item and automatically add to the quantity without asking.

Since I cannot upload a txt file, the txt file looks like this.

Directions Write a program that prints the total amount owed for a cart full of groceries to a text file, and saves the file to a folder on your computer's desktop named "Receipts". The program must satisfy the following requirements: 1. Asks the user to enter the name he/she wishes to give the .txt file. 2. Continuously asks the user to enter the name and size/weight of the 3. The program searches the file "PriceList.txt" for the price(s) of 4. The program formats the output so that it lists each item on the left, and 5. If the quantity of an item is greater than one, then the program... product, until the user enters "Done the item(s) entered. subtotal for that item aligned some space to the right. (i) .prints the quantity followed by O" and the price of one item in parentheses (i) ists the price for 1 item to the left of the subtotal (i).the subtotal reflecets the mumber of items. (iv) ...the total is printed at the end of list, separated by two newlines

Explanation / Answer

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#define MAX 50
using namespace std;

// Defines a structure to store item information
struct Items
{
// Member to store data
string name;
float price;
int qty;
};// End of structure

// Function to read file contents and stores it in Items array of object
int readData(Items item[])
{
// To store file names for product and price
char fileNamePro[20];
char fileNamePri[20];

// Counter for number of records. Initializes to zero
int counter = 0;

// Declare ifstream objects for Product and Price file
ifstream inFileProd, inFilePri;

// Accepts the product file name
cout<<" Enter the product file name to read: ";
cin>>fileNamePro;

// Open product file for reading
inFileProd.open(fileNamePro);

// Checks whether file can be opened or not
if(inFileProd.fail())
{
cout << " ERROR: Unable to open the file "<<fileNamePro<<" for reading." << endl;
exit(0);
}// End of if condition

// Accepts the price file name
cout<<" Enter the price file name to read: ";
cin>>fileNamePri;

// Open price file for reading
inFilePri.open(fileNamePri);

// Checks whether file can be opened or not
if(inFilePri.fail())
{
cout << " ERROR: Unable to open the file "<<fileNamePri<<" for reading." << endl;
exit(0);
}// End of if condition

// Loops till not end of either file
while (!inFileProd.eof() || !inFilePri.eof())
{
// Reads product name
inFileProd>>item[counter].name;
// Reads product price
inFilePri>>item[counter].price;
// Increase the record counter by one
counter++;
}// End of while loop

// Returns number of records
return counter;
}// End of function

// Function to accept transaction details from the user and returns number of transactions
int accept(Items item[], Items transaction[], int len)
{
string name;
float qty;
int c = 0;
int f = 0;

// Loops till product name is not "END"
do
{
// Sets the found status to 0
f = 0;
// Accepts the product name from the user
cout<<" Enter the product name to purchase (END to stop): ";
cin>>name;
// Checks if the product name is "END" then stop
if(name.compare("END") == 0)
break;

// Loops till number of products
for(int x = 0; x < len; x++)
{
// Checks current product name with the product name entered by the user
if(item[x].name.compare(name) == 0)
{
// If matches
// Loops till quantity greater than zero
do
{
// Accepts the quantity from the user
cout<<" Enter the quantity to purchase of "<<name<<": ";
cin>>qty;
// Checks if quantity is greater than zero
if(qty > 0)
{
// Stores the item name at x index position in transaction array c index position
transaction[c].name = item[x].name;
// Stores the price at x index position in transaction array c index position
transaction[c].price = item[x].price;
// Stores the quantity in transaction array c index position
transaction[c].qty = qty;
// Increases the transaction counter by one
c++;
// Come out of the loop
break;
}// End of if condition

// Otherwise displays error message
else
cout<<" ERROR: Invalid quantity. Please try again.";
}while(1);// End of do - while loop
// Sets the found status to one
f = 1;
// Come out of the loop
break;
}// End of if condition
}// End of for loop

// Checks if f value is zero then item not found
if(f == 0)
cout<<" Item "<<name<<" not found.";
}while(1); // End of do - while loop
// Returns transaction counter
return c;
}// End of function

// Function to sort the car ascending order of price
void mySort(Items rc[], int len)
{
// Temporary object created for swapping
Items temp;
// Loops number of records minus one times
for (int r = 0; r < len - 1; r++)
{
// Loops number of records minus outer loop value minus one times
for (int c = 0; c < len - r - 1; c++)
{
// Checks if the current price is greater than the next price then swap
if (rc[c].price > rc[c + 1].price)
{
// Swapping process
temp = rc[c];
rc[c] = rc[c + 1];
rc[c + 1] = temp;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function

// Function to display the items and transaction details
void display(Items it[], int len, Items tr[], int trLen)
{
// To store total
float total = 0;
// Calls the function to sort the transaction based on price
mySort(tr, trLen);

// Loops till number of items
for(int x = 0; x < len; x++)
// Displays each item
cout<<it[x].name<<" "<<it[x].price<<endl;


cout<<" Item Subtotal: ";
// Loops till number of transactions
for(int x = 0; x < trLen; x++)
{
// Checks if quantity is 1
if(tr[x].qty == 1)
// Displays current transaction details
cout<<tr[x].name<<" $"<<tr[x].price<<endl;
// Otherwise quantity is greater than one
else
// Displays current transaction details with amount
cout<<tr[x].name<<" "<<tr[x].qty<<"(@"<<tr[x].price<<") $"<<(tr[x].qty * tr[x].price)<<endl;
// Calculates total
total += (tr[x].qty * tr[x].price);
}// End of for loop
// Displays total
cout<<" Total $"<<total;
}// End of function

// main function definition
int main()
{
// Array to store item information
Items item[MAX];
// Array to store transaction information
Items transaction[MAX];
// Calls the function to read file and store the item information
int len = readData(item);
// Calls the function to accept transaction information from the user
int trLen = accept(item, transaction, len);
// Calls the function to display item and transaction information
display(item, len, transaction, trLen);
}// End of main function

Sample Output:

Enter the product file name to read: Product.txt

Enter the price file name to read: Price.txt

Enter the product name to purchase (END to stop): Lux

Enter the quantity to purchase of Lux: -2

ERROR: Invalid quantity. Please try again.
Enter the quantity to purchase of Lux: 1

Enter the product name to purchase (END to stop): Doo

Item Doo not found.
Enter the product name to purchase (END to stop): Dove

Enter the quantity to purchase of Dove: 3

Enter the product name to purchase (END to stop): Cinthol

Enter the quantity to purchase of Cinthol: 8

Enter the product name to purchase (END to stop): END
Lux 10.23
Cinthol 20.12
Dove 14.22
Liril 23.45
Pepsodent 45.51
-2.00017

Item Subtotal:
Lux $10.23
Dove 3(@14.22) $42.66
Cinthol 8(@20.12) $160.96

Total $213.85

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote