Skip to content

Instantly share code, notes, and snippets.

@andraantariksa
Created April 20, 2019 07:22
Show Gist options
  • Save andraantariksa/316319678eeff2ecdde19023059d1a04 to your computer and use it in GitHub Desktop.
Save andraantariksa/316319678eeff2ecdde19023059d1a04 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iomanip>
#include <cstring>
class Sales{
private:
long int sales[4][5];
public:
Sales(){
memset(sales, 0, sizeof sales);
}
void print(){
long int total;
std::cout << std::left << std::fixed;
std::cout << std::setw(8) << "Sales" << std::setw(40) << "Products" << std::setw(8) << "Total" << '\n';
for (int i = 0; i < 4; i++) {
std::cout << std::setw(8) << i + 1;
total = 0;
for (int j = 0; j < 5; j++) {
std::cout << std::setw(8) << sales[i][j];
total += sales[i][j];
}
std::cout << std::setw(8) << total << '\n';
}
}
void set(int sales_person, int product, long int total_sales){
sales[sales_person][product] += total_sales;
}
bool valid(int sales_person, int product){
if(!(sales_person >= 0 && sales_person <= 4) || !(product >= 0 && product <= 5)){
return false;
}else{
return true;
}
}
};
int main(){
Sales sales;
char cont;
int sales_person, product, week = 1;
long int total_sales;
do{
while(true){
std::cout << "Enter the salesperson, product, and total sales (Enter -1 for the salesperon to end input):\n";
std::cin >> sales_person;
if (sales_person != -1){
std::cin >> product >> total_sales;
if (sales.valid(sales_person - 1, product - 1)){
sales.set(sales_person - 1, product - 1, total_sales);
}else{
std::cout << "Invalid salesperson or/and product!\n";
}
}else{
break;
}
}
std::cout << "Week " << week << '\n';
sales.print();
std::cout << "Continue (Y/n)? ";
std::cin >> cont;
week++;
}while(cont == 'Y' || cont == 'y');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment