design a C program to control the stock of books of a small bookstore.
Part 1: for the cout, it should be printf(" "); should write like that
When a client orders a book, the system should check if the bookstore has the book in stock. If the bookstore keeps the title and there is enough stock for the order, the system should inform the price per copy to the client, as well as the total price of the purchase. The client orders a book using its code.
If the client confirms the purchase, the system should ask the client’s name and address. (This information will be sent to the shipping system.)
If the bookstore keeps the title, but there is no book in stock, the system should inform the client and register in a file the code of the book and the date that the low stock was detected, so the manager can request to the publisher.
Part 2:
By the manager request, the system should check how many copies of each book are in stock. If a book has only one copy or none, the system should register in a file the code of the book and the date that the low stock was detected, so the manager can request to the publisher. The system will read the title of a book from a file (books.txt), which structure is: bookCode BookTitle. (Your program will open the file to read. It reads the code, checks if it is the code of the book with no stock. If it is, read the title of the book and display it.)
Use: (parallel) arrays, loops, and files. Use of functions will be counted as extra credit.
Suggestion: Create an array with about 6 codes of books to test your program. The parallel array will be needed to keep the quantity in stock and the price of the book.
Add comments to your program to explain your decisions.
Evaluation:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define MAX 50 // Maximum record in file
// Function to read file contents and stores it in respective array
// Returns record length with &len call by reference
void readFile(char title[][MAX], int code[], int qty[], float price[], int *len)
{
// Creates a file pointer to open the file BookOrder.txt in read mode
FILE *readF = fopen("BookOrder.txt", "r");
// To store '\n' character
char newLine[3];
// Initializes both the counter to 0
*len = 0;
// Checks if file is unable to open then display error message
if (readF == NULL)
{
// Display error message
puts("Error: Could not open files");
exit(0);
}// End of if
// Extracts data from file and stores it
while (!feof(readF))
{
// Reads a number from file and stores it at len index position
fscanf(readF, "%[^\n]s", title[*len]);
// Reads a number from file and stores it at len index position
fscanf(readF, "%d", &code[*len]);
// Reads a number from file and stores it at len index position
fscanf(readF, "%d", &qty[*len]);
// Reads a number from file and stores it at len index position
fscanf(readF, "%f", &price[*len]);
// Reads '\n' character
fscanf(readF, "%c", &newLine);
// Increase the record counter by one
*len = *len + 1;
}// End of while loop
// Close the file
fclose(readF);
}// End of function
// Function to display all book information
void showBooks(char title[][MAX], int code[], int qty[], float price[], int len)
{
printf("\n ******************* Books Information *******************");
int x;
// Loops till number of records
for(x = 0; x < len; x++)
// Displays each book information
printf("\n\n Title: %s \t Code: %d \n Quantity: %d \t Price: %f",
title[x], code[x], qty[x], price[x]);
}// End of function
// Function to search a book and return found index position otherwise returns -1
int searchBook(char title[][MAX], char name[], int len)
{
int x;
// Loops till number of records
for(x = 0; x < len; x++)
// Checks if current book title is equals to parameter name
if(stricmp(name, title[x]) == 0)
// Returns loop variable value as found index position
return x;
// End of the loop returns -1 for not found
return -1;
}// End of function
// Function to perform transaction
// Writes the data for purchase if stock no available
// Writes the shipping information is book purchased
void transaction(char title[][MAX], int code[], int qty[], float price[], int len)
{
// Creates a file pointer to open the file Shipping.txt in read write
FILE *shippingWrite = fopen("Shipping.txt", "w");
// Creates a file pointer to open the file Purchase.txt in read write
FILE *purchaseWrite = fopen("Purchase.txt", "w");
// Checks if file is unable to open then display error message
if (shippingWrite == NULL || purchaseWrite == NULL)
{
// Display error message
puts("\n ERROR: Unable to open the file for writing.");
exit(0);
}// End of if
// Declaring argument for time()
time_t currentDate;
// Applying time
time (¤tDate);
// To store user choice to continue
char ch;
// To store name and address of client
char name[MAX], address[MAX];
// To store book title to search for purchase
char bookTitle[MAX];
// To store required number of books to purchase
int requiredQty;
// To store found index position of book title
int position;
// Loops till user choice is 'Y' or 'y'
do
{
// Accepts book title
printf("\n\n Enter the title of the book to purchase: ");
gets(bookTitle);
// Calls the function to search book title availability
// Stores the found status
position = searchBook(title, bookTitle, len);
// if found position is not -1 book found
if(position != -1)
{
// Accepts the quantity to purchase
printf("\n Enter the quantity: ");
scanf("%d", &requiredQty);
// Clears console
fflush(stdin);
// Checks if quantity found at index position of book title
// is greater than or equals to required quantity
if(qty[position] >= requiredQty)
{
// Displays the price of found book title
printf("\n Price per copy: $%.2f", price[position]);
// Calculates and displays amount
printf("\n Total price of the purchase: $ %.2f", (qty[position] * price[position]));
// Accepts client conformation for purchase
printf("\n Conform your purchase (y/n)? ");
scanf("%c", &ch);
// Checks if conformation is 'Y' or 'y'
if(ch == 'Y' || ch == 'y')
{
// Accepts the name and address of the client
printf("\n Enter your name: ");
scanf("%s", name);
// Clears console
fflush(stdin);
printf("\n Enter your address: ");
gets(address);
// Writes data to shipping file
fprintf(shippingWrite, "\n Client Name: %s", name);
fprintf(shippingWrite, "\n Address: %s", address);
fprintf(shippingWrite, "\n Book Title: %s", bookTitle);
fprintf(shippingWrite, "\n Quantity: %d", requiredQty);
fprintf(shippingWrite, "\n Unit Price: $%.2f", price[position]);
fprintf(shippingWrite, "\n Amount: $%.2f", (qty[position] * price[position]));
// Subtracts the required quantity from the found book title quantity
qty[position] -= requiredQty;
// Checks if after purchase quantity is 0
if(qty[position] == 0)
{
// Writes data to purchase file
fprintf(purchaseWrite, "\n Book Code: $%d", code[position]);
fprintf(purchaseWrite, "\n Low stock date: %s", ctime(¤tDate));
}
}// End of if condition
}// End of if condition
// Otherwise insufficient stock
else
{
printf("\n Insufficient quantity to purchase. \n Available quantity: %d", qty[position]);
// Writes data to purchase file
fprintf(purchaseWrite, "\n Book Code: $%d", code[position]);
fprintf(purchaseWrite, "\n Low stock date: %s", ctime(¤tDate));
}// End of else
}// End of if condition
// Otherwise book title not available display error message
else
printf("\n Entered book title %s not available in store.", bookTitle);
// Accepts user choice to continue purchase
printf("\n Would you like to purchase another book (y/n)? ");
scanf("%c", &ch);
// Clears console
fflush(stdin);
}while(ch == 'y' || ch == 'y'); // End of do - while loop
}// End of function*/
// main function definition
int main()
{
// Declares a string array of size MAX to store book title
char title[MAX][MAX];
// Declares a integer array of size MAX to store book code
int code[MAX];
// Declares a integer array of size MAX to store quantity
int qty[MAX];
// Declares a double array of size MAX to store price
float price[MAX];
// To store number of records
int len = 0;
// Calls the function to read file
readFile(title, code, qty, price, &len);
// Calls the function to display book information
showBooks(title, code, qty, price, len);
// Calls the function to perform transaction
transaction(title, code, qty, price, len);
return 0;
}// End of main function
Sample Output:
******************* Books Information *******************
Title: C Programming Code: 1111
Quantity: 45 Price: 450.230011
Title: Java Programming Code: 1223
Quantity: 78 Price: 983.369995
Title: Data Structure Code: 3256
Quantity: 78 Price: 345.869995
Title: Operating System Code: 4578
Quantity: 10 Price: 312.450012
Title: Database Management System Code: 2233
Quantity: 12 Price: 123.559998
Title: C++ Code: 2123
Quantity: 8 Price: 421.549988
Enter the title of the book to purchase: DS
Entered book title DS not available in store.
Would you like to purchase another book (y/n)? y
Enter the title of the book to purchase: C++
Enter the quantity: 12
Insufficient quantity to purchase.
Available quantity: 8
Would you like to purchase another book (y/n)? y
Enter the title of the book to purchase: C++
Enter the quantity: 3
Price per copy: $421.55
Total price of the purchase: $ 3372.40
Conform your purchase (y/n)? y
Enter your name: Pyari
Enter your address: BAM
Would you like to purchase another book (y/n)? y
Enter the title of the book to purchase: Operating System
Enter the quantity: 12
Insufficient quantity to purchase.
Available quantity: 10
Would you like to purchase another book (y/n)? y
Enter the title of the book to purchase: Operating System
Enter the quantity: 8
Price per copy: $312.45
Total price of the purchase: $ 3124.50
Conform your purchase (y/n)? n
Would you like to purchase another book (y/n)?n
BookOrder.txt file contents
C Programming
1111
45
450.23
Java Programming
1223
78
983.37
Data Structure
3256
78
345.87
Operating System
4578
10
312.45
Database Management System
2233
12
123.56
C++
2123
8
421.55
Purchase.txt file contents
Book Code: $2123
Low stock date: Tue Apr 23 14:21:27 2019
Book Code: $4578
Low stock date: Tue Apr 23 14:21:27 2019
Shipping.txt file contents
Client Name: Pyari
Address: BAM
Book Title: C++
Quantity: 3
Unit Price: $421.55
Amount: $3372.40
design a C program to control the stock of books of a small bookstore. Part 1:...
Write a program for the management of a bookstore in java, WITHOUT THE USE OF BOOK CLASS, just arrays, strings, loops, etc. Overview; First menu 1. List all books 2. Search all books 3. Purchase books 4. Exit If option 1 is chosen: List all books by Title, Author, Price, Copies, Category within a group of arrays If option 2 is chosen: Search all books by Title, Author, Price, Copies, Category If option 3 is chosen: List books, List shopping...
Write a program for the management of a bookstore in Java Programming Language, WITHOUT THE USE OF STUFF RELATING TO OBJECTS AND WHAT NOT, just arrays, strings, loops, etc. I'm in an intro to programming class so I haven't learned those techniques and am not allowed to use them. The books we just create Overview; Opening menu 1. List all books 2. Search all books 3. Purchase books 4. Exit If option 1 is chosen: List all books by Title,...
Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read and process a file containing customer purchase data for books. The books available for purchase will be read from a separate data file. Process the customer sales and produce a report of the sales and the remaining book inventory. You are to read a data file (customerList.txt, provided) containing customer book purchasing data. Create a structure to contain the information. The structure will contain...
1) User wants to keep track Books. Each Book has Author (String), Title (String), Year (4 digit String) and Price (Double). Write a Java Book class with constructors and set/get methods. 2) Write a Java program that asks user to enter the Book data on the Console. User can enter any number of books until terminated on the Console. Write the book data to a text file called "book.txt" in the current project directory. 3) Write a Java program to...
High School Assignment: please keep simple, use for loops but preferably no arrays In python, Instructions Write a program to input ten book titles and author's names then save them to a file named books.txt. For each book, the user will first input the title, then input the first name, then the last name of the author, all on separate lines. In addition to saving the books in the file, please output, using the print method, the information you've gathered...
please use C++ write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. each line in the file has tile, ..... write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. Each line in...
Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors. Create Functions, include headers and other files, Loops(while, for), conditional(if, switch), datatypes, etc. Assignment You work at the computer science library, and your boss just bought a bunch of new books for the library! All of the new books need to be cataloged and sorted back on the shelf. You don’t need to keep track of which customer has the book or not, just whether...
please write this program in C++(Linux). Also write all the
explanation of codes, the comment for your code.
Description You are an avid reader and have decided that you would like to keep track of the books that you read. You will define a structure called book_list that will hold 1. a number associated with each book (int) 2. book title (string), 3. author (string), 4. a description of the book (string), 5. a date when the book was read...
CSC 126 Arrays, Functions, & File 'O Part I Your program should prompt the user to calculate the total bill of a transaction at a Book store After the calculations have been performed, the program should output the result to the screen and save the output in a text file which can be later retrieved The program should work as follows: XYZ Book Store Sales Register This program calculates your total bill and generates a receipt for you. Please enter...
Part 1 The purpose of this part of the assignment is to give you practice in creating a class. You will develop a program that creates a class for a book. The main program will simply test this class. The class will have the following data members: A string for the name of the author A string for the book title A long integer for the ISBN The class will have the following member functions (details about each one are...