Anda di halaman 1dari 4

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

//sales_calculator.cpp #include #include #include #include #include #include <iostream> <iomanip> <cstdlib> <cmath> <sstream> <stdexcept>

// constants defination // all global constant are qualified static to have internal linkage in this translation unit static double const GOVERNMENT_TAX_RATE = 0.06; static double const SERVCE_TAX_RATE = 0.10; static int const static int const static int const static int const static int const static int const static int const COLUMN_2_WIDTH + static int const COLUMN_2_WIDTH; static int const static static static static static static double double double double double double LMARGIN_WIDTH = 3; RMARGIN_WIDTH = 5; COLUMN_1_WIDTH = 16; COLUMN_1_RIGHT_MARGIN_WIDTH = 2; COLUMN_2_WIDTH = 8; COLUMN_3_WIDTH = 15; CONTENT_WIDTH = COLUMN_1_WIDTH + COLUMN_1_RIGHT_MARGIN_WIDTH + RMARGIN_WIDTH + COLUMN_3_WIDTH; SPAN_2_COLUMN_WIDTH = COLUMN_1_WIDTH + COLUMN_1_RIGHT_MARGIN_WIDTH + PAGE_WIDTH = LMARGIN_WIDTH + CONTENT_WIDTH + RMARGIN_WIDTH; MIN_COMBO_SET_COUNT = 1; MAX_COMBO_SET_COUNT = 9999; MAX_COMBO_SET_PRICE = 9999.9; MAX_CASH = 999999.9; MAX_DISCOUNT_VOUCHER = 9999.9; MIN_CHANGE_DUE = 0;

const const const const const const

void print_receipt(double combo_set_count, double combo_set_price ,double sales_total, double government_tax ,double service_tax, double sub_total ,double discount_voucher, double gross_amount_payable ,double net_amount_payable ,double cash, double change_due);

void calculate_sales(double combo_set_count, double combo_set_price, double discount_voucher, double cash) { // calculation logic double const sales_total = (combo_set_price * combo_set_count); double const government_tax = (sales_total * GOVERNMENT_TAX_RATE); double const service_tax = (sales_total * SERVCE_TAX_RATE); double const sub_total = (sales_total + government_tax + service_tax); double const gross_amount_payable = std::max(sub_total - discount_voucher, 0.0); double const net_amount_payable = (floor((gross_amount_payable * 10.0 ) + 0.5) / 10.0); double const change_due = (cash - net_amount_payable);

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

std::string err_msg; // user input validation if(MIN_COMBO_SET_COUNT > combo_set_count) { err_msg += "Error: Minimum order of combo set is one.\n"; } if(static_cast<int>(combo_set_count) < combo_set_count) { err_msg += "Error: Combo set must be whole number.\n"; } if(MAX_COMBO_SET_COUNT < combo_set_count) { err_msg += "Error: Combo set range too large.\n"; } if(MAX_COMBO_SET_PRICE < combo_set_price) { err_msg += "Error: Combo price range too large.\n"; } if(MAX_CASH < cash ) { err_msg += "Error: Cash range too large.\n"; } if(MAX_DISCOUNT_VOUCHER < discount_voucher) { err_msg += "Error: Discount voucher range too large.\n"; } if(MIN_CHANGE_DUE > change_due) { err_msg += "Error: Insufficient cash entered.\n"; } if(false == err_msg.empty()) { throw std::out_of_range(err_msg); } // clear console screen system("cls"); // print receipt print_receipt(combo_set_count, combo_set_price , sales_total, government_tax , service_tax, sub_total , discount_voucher, gross_amount_payable , net_amount_payable , cash, change_due); } void print_receipt(double combo_set_count, double combo_set_price ,double sales_total, double government_tax ,double service_tax, double sub_total ,double discount_voucher, double gross_amount_payable ,double net_amount_payable ,double cash, double change_due) { std::stringstream memory_stream; memory_stream << combo_set_count;

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

std::string combo_set_count_number; memory_stream >> combo_set_count_number; using using using using std::left; std::right; std::setw; std::setfill;

std::cout << std::fixed << std::setprecision(2) << "ooooooooooooo .o. ooooooooo. .oooooo. "8 888 8 .888. 888 Y88 d8P Y8b " 888 .8 888. 888 .d88 888 " 888 .8 888. 888ooo88P 888 " 888 .88ooo8888. 888 88b. 888 " 888 .8 888. 888 88b. 88b ooo " o888o o88o o8888o o888o o888o Y8bood8P "\n" \n" \n" \n" \n" \n" \n" \n"

<< right << setw(PAGE_WIDTH / 2 + sizeof((char[]){"R E C E I P T"}) / 2) << "R E C E I P T" << '\n' << '\n' << right << setw(PAGE_WIDTH) << setfill('-') << ' ' << setfill(' ') << '\n' << left << setw(LMARGIN_WIDTH) << '|' << left << setw(COLUMN_1_WIDTH) << " Description" << right << setw(COLUMN_1_RIGHT_MARGIN_WIDTH) << '|' << right << setw(COLUMN_2_WIDTH) << "Rate" << right << setw(RMARGIN_WIDTH) << '|' << right << setw(COLUMN_3_WIDTH) << "Amount (RM)" << right << setw(RMARGIN_WIDTH) << '|' << '\n' << right << setw(PAGE_WIDTH) << setfill('-') << ' ' << setfill(' ') << '\n'

<< left << setw(LMARGIN_WIDTH) << '|' << left << setw(COLUMN_1_WIDTH) << ("Combo Set * " + combo_set_count_number) << right << setw(COLUMN_1_RIGHT_MARGIN_WIDTH) << '|' << right << setw(COLUMN_2_WIDTH) << combo_set_price << right << setw(RMARGIN_WIDTH) << '|' << right << setw(COLUMN_3_WIDTH) << sales_total << right << setw(RMARGIN_WIDTH) << '|' << '\n' << left << setw(LMARGIN_WIDTH) << '|' << left << setw(COLUMN_1_WIDTH) << "Government Tax %" << right << setw(COLUMN_1_RIGHT_MARGIN_WIDTH) << '|' << right << setw(COLUMN_2_WIDTH) << int(GOVERNMENT_TAX_RATE * 100) << right << setw(RMARGIN_WIDTH) << '|' << right << setw(COLUMN_3_WIDTH) << government_tax << right << setw(RMARGIN_WIDTH) << '|' << '\n' << left << setw(LMARGIN_WIDTH) << '|' << left << setw(COLUMN_1_WIDTH) << "Service Tax %" << right << setw(COLUMN_1_RIGHT_MARGIN_WIDTH) << '|' << right << setw(COLUMN_2_WIDTH) << int(SERVCE_TAX_RATE * 100) << right << setw(RMARGIN_WIDTH) << '|' << right << setw(COLUMN_3_WIDTH) << service_tax << right << setw(RMARGIN_WIDTH) << '|' << '\n'

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

<< right << setw(PAGE_WIDTH) << setfill('-') << left << B T O T A << right << << right <<

<< ' ' << setfill(' ') << '\n'

setw(LMARGIN_WIDTH) << '|' << left << setw(SPAN_2_COLUMN_WIDTH) << "S U L" setw(RMARGIN_WIDTH) << '|' << right << setw(COLUMN_3_WIDTH) << sub_total setw(RMARGIN_WIDTH) << '|' << '\n' << ' ' << setfill(' ') << '\n'

<< right << setw(PAGE_WIDTH) << setfill('=')

<< left << setw(LMARGIN_WIDTH) << '|' << left << setw(SPAN_2_COLUMN_WIDTH) << "Discount voucher" << right << setw(RMARGIN_WIDTH) << ':' << right << setw(COLUMN_3_WIDTH) << discount_voucher << right << setw(RMARGIN_WIDTH) << '|' << '\n' << left << setw(LMARGIN_WIDTH) << '|' << right << setw(CONTENT_WIDTH) << "------" << right << setw(RMARGIN_WIDTH) << '|' << '\n' << left << setw(LMARGIN_WIDTH) << '|' << left << setw(SPAN_2_COLUMN_WIDTH) << "Gross Amount Payable" << right << setw(RMARGIN_WIDTH) << ':' << right << setw(COLUMN_3_WIDTH) << gross_amount_payable << right << setw(RMARGIN_WIDTH) << '|' << '\n' << left << setw(LMARGIN_WIDTH) << '|' << right << setw(CONTENT_WIDTH) << ' ' << right << setw(RMARGIN_WIDTH) << '|' << '\n' << left << setw(LMARGIN_WIDTH) << '|' << left Payable" << right << setw(RMARGIN_WIDTH) << '|' << '\n' << setw(CONTENT_WIDTH) << "Net Amount

<< left << setw(LMARGIN_WIDTH) << '|' << left << setw(SPAN_2_COLUMN_WIDTH) << " (After adjustment)" << right << setw(RMARGIN_WIDTH) << ':' << right << setw(COLUMN_3_WIDTH) << setfill('*' ) << net_amount_payable << setfill(' ') << right << setw(RMARGIN_WIDTH) << '|' << '\n' << left << setw(LMARGIN_WIDTH) << '|' << right << setw(CONTENT_WIDTH) << "======" << right << setw(RMARGIN_WIDTH) << '|' << '\n' << right << setw(PAGE_WIDTH) << setfill('-') << '\n' << ' ' << setfill(' ') << '\n'

<< left << setw(LMARGIN_WIDTH) << ' ' << left << setw(10) << "Cash (RM)" << right << setw(LMARGIN_WIDTH) << ':' << right << setw(13) << cash << '\n' << left << setw(LMARGIN_WIDTH) << ' ' << left << setw(10) << "Change due (RM)" << right << setw(LMARGIN_WIDTH) << ':' << right << setw(13) << change_due << '\n' << '\n' << right << setw(PAGE_WIDTH) << setfill('-') }

<< ' ' << setfill(' ') << std::endl;

Anda mungkin juga menyukai