Answer to Question #188457 in C++ for Drake

Question #188457

You've to develop software for bookshop.Program should be able to calculate total sales,salaries of salesman,types of book sold,record each

customer data.You've to enter data of three salesperson and at least 10 books (try to enter data of

three different types).Consider following structures, you

can always add additional data member if you feel it will result in better/efficient design:


You have to write following functions:

1. totalSales(): Total sales done

2. totalSalesBySalesPerson(): Total sales done by a specific sales person

3. topSalesPerson(): List of sorted sales person along with the total sales done by them

4. commissionSalesPerson(): Calculate commission of a sales person

5. totalSalary(): Calculate the amount that the owner has to pay to all sales person

6. booksInventory(): remaining books in inventory

7. booksInventoryByType(): remaining books by type


1
Expert's answer
2021-05-05T03:56:42-0400
#include <iostream>
#include <string>
#include <iomanip>


using namespace std;


struct Salary{
	double fixedSalary; 
	double comission; //2% of sale price of all the books sold
};
struct Address{
	char* street;
	char* city;
	char* state;
	char* zip;
};
struct Book{
	int id;
	string type;
	char* bookName;
	char* authorName;
	double price;
};
struct Customer{
	int cust_No;
	char* cust_Name; //duration of project 
	struct Address cust_Address; //structure itself 
	struct Book bk; //
};


struct Sales_Person{//to store data of each sales person
	int emp_ID;
	char* emp_Name; //duration of project
	double Sales;
	struct Customer emp_Customer_Array[10]; //array of customers that salesperson sold books to
	int number_sold_books;
	Salary emp_sal; //salary of the employee
};


double totalSales(struct Sales_Person sales_Person[],int totalSalesPersons);
double totalSalesBySalesPerson(struct Sales_Person sale_Person);
void topSalesPerson(struct Sales_Person sales_Person[],int totalSalesPersons);
double commissionSalesPerson(struct Sales_Person sale_Person);
double totalSalary(struct Sales_Person sales_Person[],int totalSalesPersons);
void booksInventory(struct Book books[],int totalBook);
void booksInventoryByType(struct Book books[],int totalBooks,string type);


int main(){
	int STRING_SIZE=30;
	// three salesperson
	struct Sales_Person sales_Person[3];
	int totalSalesPersons=0;
	//10 books
	struct Book books[10];
	int totalBooks=0;


	int choice=0;
	while(choice!=8){
		cout<<"1. Add a new book\n";
		cout<<"2. Add a new sales person\n";
		cout<<"3. Sell the book to the customer\n";
		cout<<"4. Display total sales\n";
		cout<<"5. Display total sales by sales person\n";
		cout<<"6. Display top sales person\n";
		cout<<"7. Display the remaining books by type\n";
		cout<<"8. Exit\n";
		cout<<"Your choice: ";
		cin>>choice;
		switch (choice)
		{
		case 1:
			{
				cout<<"Enter the book id: ";
				cin>>books[totalBooks].id;
				cin.ignore();
				cout<<"Enter the book type: ";
				getline(cin,books[totalBooks].type);
				cout<<"Enter the book name: ";
				books[totalBooks].bookName = (char*)malloc(STRING_SIZE*sizeof(char));
				gets(books[totalBooks].bookName);
				cout<<"Enter the book author name: ";
				books[totalBooks].authorName = (char*)malloc(STRING_SIZE*sizeof(char));
				gets(books[totalBooks].authorName);
				cout<<"Enter the book price: ";
				cin>>books[totalBooks].price;
				totalBooks++;
				cout<<"\nA new book has been added.\n";
			}
			break;
		case 2:
			{
				cout<<"Enter the sales person id: ";
				cin>>sales_Person[totalSalesPersons].emp_ID;
				cin.ignore();
				cout<<"Enter the sales person name: ";
				sales_Person[totalSalesPersons].emp_Name=(char*)malloc(STRING_SIZE*sizeof(char));
				gets(sales_Person[totalSalesPersons].emp_Name);
				cout<<"Enter the sales person sales: ";
				cin>>sales_Person[totalSalesPersons].Sales;
				sales_Person[totalSalesPersons].number_sold_books=0;
				cout<<"Enter the sales person fixed salary: ";
				cin>>sales_Person[totalSalesPersons].emp_sal.fixedSalary;
				cin.ignore();
				totalSalesPersons++;
			}
			break;
		case 3:


			if(totalBooks>0){
				if(totalSalesPersons>0){
					int selectedSalesPersons=-1;
					while(selectedSalesPersons<1 || selectedSalesPersons>totalSalesPersons){
						cout<<"Select the sales person [1-"<<totalSalesPersons<<"]: ";
						cin>>selectedSalesPersons;
					}
					selectedSalesPersons--;
					booksInventory(books,totalBooks);
					int selectedBook=-1;
					while(selectedBook<1 || selectedBook>totalBooks){
						cout<<"Select the book [1-"<<totalBooks<<"]: ";
						cin>>selectedBook;
					}
					selectedBook--;


					struct Customer customer;
					cout<<"Enter the customer number: ";
					cin>>customer.cust_No;
					cin.ignore();
					cout<<"Enter the customer name: ";
					customer.cust_Name=(char*)malloc(STRING_SIZE*sizeof(char));
					gets(customer.cust_Name);
					cout<<"Enter the customer street: ";
					customer.cust_Address.street=(char*)malloc(STRING_SIZE*sizeof(char));
					gets(customer.cust_Address.street);
					cout<<"Enter the customer city: ";
					customer.cust_Address.city=(char*)malloc(STRING_SIZE*sizeof(char));
					gets(customer.cust_Address.city);
					cout<<"Enter the customer state: ";
					customer.cust_Address.state=(char*)malloc(STRING_SIZE*sizeof(char));
					gets(customer.cust_Address.state);
					cout<<"Enter the customer zip: ";
					customer.cust_Address.zip=(char*)malloc(STRING_SIZE*sizeof(char));
					gets(customer.cust_Address.zip);
					customer.bk=books[selectedBook];
					sales_Person[selectedSalesPersons].emp_Customer_Array[sales_Person[selectedSalesPersons].number_sold_books]=customer;
					sales_Person[selectedSalesPersons].number_sold_books++;
					sales_Person[selectedSalesPersons].emp_sal.comission=commissionSalesPerson(sales_Person[selectedSalesPersons]);
					cout<<"\nThe book has been sold\n";
				}else{
					cout<<"\nAdd sales persons.\n";
				}
			}else{
				cout<<"\nAdd books.\n";
			}
			break;
		case 4:
			{
				if(totalSalesPersons>0){
					cout<<"The total sales: "<<totalSales(sales_Person, totalSalesPersons)<<"\n";
				}else{
					cout<<"\nAdd sales persons.\n";
				}
			}
			break;
		case 5:
			{
				if(totalSalesPersons>0){
					int selectedSalesPersons=-1;
					while(selectedSalesPersons<1 || selectedSalesPersons>totalSalesPersons){
						cout<<"Select the sales person [1-"<<totalSalesPersons<<"]: ";
						cin>>selectedSalesPersons;
					}
					selectedSalesPersons--;
					cout<<"The total sales for this sale person: "<<totalSalesBySalesPerson(sales_Person[selectedSalesPersons])<<"\n";


				}else{
					cout<<"\nAdd sales persons.\n";
				}
			}
			break;
		case 6:
			{
				if(totalSalesPersons>0){
					topSalesPerson(sales_Person, totalSalesPersons);
				}else{
					cout<<"\nAdd sales persons.\n";
				}
			}
			break;
		case 7:
			{
				if(totalBooks>0){
					cin.ignore();
					string bookType;
					cout<<"Enter the book type: ";
					getline(cin,bookType);
					booksInventoryByType(books,totalBooks,bookType);   
				}else{
					cout<<"\nAdd book.\n";
				}
			}
		case 8:
			//exit
			break;
		default:
			break;
		}	
	}


	return 0;
}


//1. totalSales(): Total sales done
double totalSales(struct Sales_Person sales_Person[],int totalSalesPersons){
	double totalsales=0;
	for (int s = 0; s <totalSalesPersons; ++s) {
		for(int i=0;i<sales_Person[s].number_sold_books;i++){
			totalsales+=sales_Person[s].emp_Customer_Array[i].bk.price;
		}
	}
	return totalsales;
}
//2. totalSalesBySalesPerson(): Total sales done by a specific sales person
double totalSalesBySalesPerson(struct Sales_Person sale_Person){
	double totalsales=0;
	for(int i=0;i<sale_Person.number_sold_books;i++){
		totalsales+=sale_Person.emp_Customer_Array[i].bk.price;
	}
	return totalsales;
}
//3. topSalesPerson(): List of sorted sales person along with the total sales done by them
void topSalesPerson(struct Sales_Person sales_Person[],int totalSalesPersons){
	// loop to access each array element
	for (int s = 0; s < (totalSalesPersons-1); ++s) {
		// loop to compare array sales Person by sales
		for (int i = 0; i < totalSalesPersons - (s-1); ++i) {
			if (sales_Person[i].Sales > sales_Person[i + 1].Sales) {
				struct Sales_Person temp = sales_Person[i];
				sales_Person[i] = sales_Person[i + 1];
				sales_Person[i + 1] = temp;
			}
		}
	}
	for (int s = 0; s <totalSalesPersons; ++s) {
		cout<<"Sales person ID: "<<sales_Person[s].emp_ID<<"\n";
		cout<<"Sales person name: "<<sales_Person[s].emp_Name<<"\n";
		cout<<"Sales person sales: "<<sales_Person[s].Sales<<"\n";
		cout<<"Sales person number sold books: "<<sales_Person[s].number_sold_books<<"\n";
		cout<<"Sales person number fixed salary: "<<sales_Person[s].emp_sal.fixedSalary<<"\n";
		cout<<"Sales person number comission: "<<sales_Person[s].emp_sal.comission<<"\n\n";
	}
}
//4. commissionSalesPerson(): Calculate commission of a sales person
double commissionSalesPerson(struct Sales_Person sale_Person){
	double priceSum=0;
	for(int i=0;i<sale_Person.number_sold_books;i++){
		priceSum+=sale_Person.emp_Customer_Array[i].bk.price;
	}
	return priceSum*0.02;
}
//5. totalSalary(): Calculate the amount that the owner has to pay to all sales person
double totalSalary(struct Sales_Person sales_Person[],int totalSalesPersons){
	double totalsalary=0;
	for(int i=0;i<totalSalesPersons;i++){
		totalsalary+=sales_Person[i].emp_sal.fixedSalary+sales_Person[i].emp_sal.comission;
	}
	return totalsalary;
}
//6. booksInventory(): remaining books in inventory
void booksInventory(struct Book books[],int totalBooks){
	cout <<fixed <<left<< "Book ID" << setw(25) << "Book type"<< setw(25) << "Book name"<< setw(25) << "Book author name"<< setw(25) << "Book price\n";
	for(int i=0;i<totalBooks;i++){
		cout <<left<<setw(25)<< books[i].id << setw(25) << books[i].type<< setw(25) <<books[i].bookName<< setw(25) <<books[i].authorName<< setw(25)<<books[i].price << "\n";	
	}
}
//7. booksInventoryByType(): remaining books by type
void booksInventoryByType(struct Book books[],int totalBooks,string type){
	cout <<fixed <<left << setw(25)<< "\t      Book ID" << setw(25) << "Book type"<< setw(25) << "Book name"<< setw(25) << "Book author name"<< setw(25) << "Book price\n";
	for(int i=0;i<totalBooks;i++){
		if(type.compare(books[i].type)==0){
			cout <<left<<setw(25)<< books[i].id << setw(25) << books[i].type<< setw(25) <<books[i].bookName<< setw(25) <<books[i].authorName<< setw(25)<<books[i].price << "\n";	
		}
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS