Last active
December 31, 2015 07:39
-
-
Save argiopetech/7955782 to your computer and use it in GitHub Desktop.
Quick refactor of the switch statement from StackOverflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
class DivSales{ | |
private: | |
double quarter[4]; // Holds the sales for each quarter | |
double first, second, third, fourth; // Store values from the inputs | |
static double corpSales; // Shared among all instances of DivSales (each object) | |
public: | |
// Populate the array quarter | |
void setQuartersArr(double first, double second, double third, double fourth){ | |
quarter[0] = first; | |
quarter[1] = second; | |
quarter[2] = third; | |
quarter[3] = fourth; | |
} | |
void setFirst (double fir){ | |
first = fir; | |
} | |
void setSecond (double sec){ | |
second = sec; | |
} | |
void setThird (double thi){ | |
third = thi; | |
} | |
void setFourth (double fou){ | |
fourth = fou; | |
} | |
// Returns the sales of a particular quarter | |
double getSales(int quar){ | |
double quarSales; | |
quarSales = quarter[quar]; | |
return quarSales; | |
} | |
double CalculateDivTotal(){ | |
double total = 0; | |
for(int count = 0; count < 4; count++) total += quarter[count]; | |
corpSales += total; | |
return corpSales; | |
} | |
friend ostream &operator<<(ostream &out, DivSales divi6){ | |
out << divi6.CalculateDivTotal(); | |
return out; | |
} | |
}; | |
double DivSales::corpSales = 0; | |
int main() | |
{ | |
int divNum = 6; | |
int quartCount = 1; | |
double tempSales; | |
DivSales divi1, divi2, divi3, divi4, divi5, divi6; | |
DivSales divisions[6] = {divi1, divi2, divi3, | |
divi4, divi5, divi6}; | |
cout << "Welcome, please enter "; | |
for(int count = 0; count < divNum; quartCount++){ | |
cout << "Division " << count + 1 << ", Quarter " | |
<< quartCount << " sales: "; | |
cin >> tempSales; | |
switch (quartCount){ | |
case 1: divisions[count].setFirst(tempSales); | |
break; | |
case 2: divisions[count].setSecond(tempSales); | |
break; | |
case 3: divisions[count].setThird(tempSales); | |
break; | |
case 4: divisions[count].setFourth(tempSales); | |
count++; | |
quartCount = 0; | |
break; | |
} | |
tempSales = 0; | |
} | |
// Run the CalculateDivTotal method for each object except the last one | |
for(int count = 0; count < 5; count++) divisions[count].CalculateDivTotal(); | |
cout << divi6.CalculateDivTotal(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <stdexcept> | |
using namespace std; | |
class DivSales{ | |
private: | |
double quarter[4]; // Holds the sales for each quarter | |
static double corpSales; // Shared among all instances of DivSales (each object) | |
public: | |
// Populate the array quarter | |
void setQuartersArr(double first, double second, double third, double fourth){ | |
quarter[0] = first; | |
quarter[1] = second; | |
quarter[2] = third; | |
quarter[3] = fourth; | |
} | |
void setQuarter (int q, double v) { | |
if ((q > 0) && (q < 5)) { | |
quarter[q] = v; | |
} | |
else { | |
throw std::out_of_range("That isn't a valid quarter!"); | |
} | |
} | |
// Returns the sales of a particular quarter | |
double getSales(int quar){ | |
double quarSales; | |
quarSales = quarter[quar]; | |
return quarSales; | |
} | |
double CalculateDivTotal(){ | |
double total = 0; | |
for(int count = 0; count < 4; count++) total += quarter[count]; | |
corpSales += total; | |
return corpSales; | |
} | |
friend ostream &operator<<(ostream &out, DivSales divi6){ | |
out << divi6.CalculateDivTotal(); | |
return out; | |
} | |
}; | |
double DivSales::corpSales = 0; | |
int main() | |
{ | |
int divNum = 6; | |
int quartCount = 1; | |
double tempSales; | |
DivSales divi1, divi2, divi3, divi4, divi5, divi6; | |
DivSales divisions[6] = {divi1, divi2, divi3, | |
divi4, divi5, divi6}; | |
cout << "Welcome, please enter "; | |
for(int count = 0; count < divNum; quartCount++) { | |
cout << "Division " << count + 1 << ", Quarter " | |
<< quartCount << " sales: "; | |
cin >> tempSales; | |
divisions[count].setQuarter(quartCount, tempSales); | |
if (quartCount == 4) { | |
count++; | |
quartCount = 0; | |
break; | |
} | |
tempSales = 0; | |
} | |
// Run the CalculateDivTotal method for each object except the last one | |
for(int count = 0; count < 5; count++) | |
divisions[count].CalculateDivTotal(); | |
cout << divi6.CalculateDivTotal(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment