#include <stdio.h> #include <stdlib.h> #include <iostream>
#include <time.h> // added TIME
#include <windows.h> //for text coloring. No pseudocode or flowchart needed. void firstpage ();
void menu ();
void card_type (int card_type);
void receipt (int i, int num_order, int list[], int quantity[], float total_price[50], char *name[], float current_total);
int main (void) {
float money_paid = 0, money_return, total_price[50] = {}, current_total = 0, price[50] = {}, rounding;
int type_card = -5, i = 0, quantity[50] = {}, list[50] = {}, chance = 3, no_order =
0, code = -5, type_payment = -5, id, pw; //
CHANGED
int creditno1, creditno2, creditno3, creditno4, securityno;
// CHANGED
char selection, *name1[50] = {}, *name[50] = {}, type = 'a', type2 = 'a'; int reorder = 0, pass = 1, round;
HANDLE h = GetStdHandle (STD_OUTPUT_HANDLE); // These 3 lines define the color changing mechanism.
WORD wOldColorAttrs;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; firstpage ();
system ("color 3"); // Change entire program font color to aqua.
GetConsoleScreenBufferInfo (h, &csbiInfo); // These 2 lines precapture original font and background color.
wOldColorAttrs = csbiInfo.wAttributes;
while ( chance > 0 ) // Changed the while loop to countdown instead of count up. Removed access 'If' statements.
{
if ( chance == 1 ){
printf ("\t\tPlease enter your ID (%d try remaining) : ", chance); scanf ("%d", &id);
fflush (stdin); }
else {
printf ("\t\tPlease enter your ID (%d tries remaining) : ", chance); scanf ("%d", &id);
fflush (stdin); }
if ( id == 1302523 || id == 1302467 || id == 1302375 || id == 1302514 || id
== 1303111 || id == 1302509 ) // Reorganised 'If' statement.
// ADDED break;
else {
--chance;
SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY); // Change to color.
printf ("\n\n\n\t\t\t\t=============\n"); printf ("\t\t\t\tInvalid ID!!!");
printf ("\n\t\t\t\t=============");
SetConsoleTextAttribute (h, wOldColorAttrs); // Change back to original.
printf ("\n\n\n\t\t\t");
system ("pause"); // Used 'pause' instead of getchar() for turely 'Press any key to continue'.
} system ("cls"); firstpage (); } if ( chance == 0 ) { system ("cls");
SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY); printf ("\n\n\n\n\n\n\t\t\t\t LOG IN
FAILED\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
SetConsoleTextAttribute (h, wOldColorAttrs); exit (0);
}
chance = 3; // Added this line to reset the 'chance' to 3 in events where 1 chance is used in ID section but not reset in password section.
while ( chance > 0 ) {
system ("cls"); firstpage ();
SetConsoleTextAttribute (h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf ("\t\tID : %d\n\n", id);
SetConsoleTextAttribute (h, wOldColorAttrs); if ( chance == 1 ){
printf ("\t\tPlease enter your password (%d try remaining) : ", chance);
scanf ("%d", &pw); //password = 123 fflush (stdin);
} else{
printf ("\t\tPlease enter your password (%d tries remaining) : ", chance);
scanf ("%d", &pw); //password = 123 fflush (stdin);
}
if ( pw == 123 ) // Reorganized 'If' statements. break;
else{
--chance;
SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY); printf ("\n\n\t\t\t\t===================\n");
printf ("\t\t\t\tInvalid password!!!"); printf ("\n\t\t\t\t==================="); SetConsoleTextAttribute (h, wOldColorAttrs); printf ("\n\n\t\t\t"); system ("pause"); } system ("cls"); firstpage ();
printf ("\tID\t: %d\n", id); }
if ( chance == 0 ) {
system ("cls");
SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY); printf ("\n\n\n\n\n\n\t\t\t\t LOG IN FAILED\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); SetConsoleTextAttribute (h, wOldColorAttrs); exit (0); } system ("cls");
SetConsoleTextAttribute (h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf ("\n\n\n\n\n\n\t\t\t\t LOG IN SUCCESSFUL\n\n\n");
SetConsoleTextAttribute (h, wOldColorAttrs); switch ( id ){
case 1302514: printf ("\t\t\tGreetings, Mr Arthur Chang\n");
//Changed all 'Good Morning' to 'Greetings', more appropriate if time is included. break;
case 1302375: printf ("\t\t\tGreetings, Mr YYN\n"); break;
case 1302467: printf ("\t\t\tGreetings, Mr Ken Yap\n"); break;
case 1302523: printf ("\t\t\tGreetings, Mr Sherman Tan\n"); break;
case 1303111: printf ("\t\t\tGreetings, Mr Wen Shen\n"); break;
case 1302509: printf ("\t\t\tGreetings, Mr Ang Jia Jian\n"); break;
}
printf ("\t\t\tYou can start to order now ^_^\n"); time_t now;
// TIME time (&now);
// TIME
printf ("\t\t\tLogin time : %s", ctime (&now)); // TIME printf ("\n\n\n\n\n\n\n\n\n"); system ("pause"); i = 0; list[i] = 0; no_order = 0; current_total = 0; system ("cls"); menu ();
printf ("\n Please enter the Item Code of items you wish to buy : "); // Reinvented the while loop.
scanf ("%d", &code); fflush (stdin); while ( code != 0 ) {
do // Added looping for invalid Item Code. { switch ( code ){ case 0: pass = 1; break; case 1:
name[i] = "Call of Duty 10 Ghosts\t"; name1[i] = "Call of Duty 10 Ghosts"; price[i] = 179.99;
pass = 1; break; case 2:
name[i] = "Asssasin Creed IV Black Flag"; name1[i] = "Asssasin Creed IV Black Flag"; price[i] = 169.99;
pass = 1; break; case 3:
name[i] = "Football Manager 2014\t"; name1[i] = "Football Manager 2014"; price[i] = 149.99; pass = 1; break; case 4: name[i] = "NBA 2K14\t\t"; name1[i] = "NBA 2K14"; price[i] = 119.99; pass = 1; break; case 5:
name[i] = "ARMA III\t\t"; name1[i] = "ARMA III"; price[i] = 89.99; pass = 1;
break; case 6:
name[i] = "BATMAN ARKHAM ORIGINS\t"; name1[i] = "BATMAN ARKHAM ORIGINS"; price[i] = 69.99;
pass = 1; break; case 7:
name[i] = "Chivalry Medieval Warfare"; name1[i] = "Chivalry Medieval Warfare"; price[i] = 59.99; pass = 1; break; default: system ("cls"); menu (); SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY);
printf ("\n Invalid Item Code!!!\n"); SetConsoleTextAttribute (h, wOldColorAttrs); pass = 0;
printf ("\n Please enter valid Item Code of items you wish to buy : ");
scanf ("%d", &code); fflush (stdin); break;
} while ( pass == 0 );
if ( code == 0 ) // Added second layer exit code checking in events where first layer is bypassed by entering an invalid code other than 0.
break; else{ system ("cls"); menu (); SetConsoleTextAttribute (h, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf ("\n Item code : (%d)\n", code); SetConsoleTextAttribute (h, wOldColorAttrs); printf ("\n Please enter quantity : "); scanf ("%d", &quantity[i]);
fflush (stdin);
while ( quantity[i] == 0 ){ // Added looping for 0 quantity. system ("cls");
menu ();
SetConsoleTextAttribute (h, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf ("\n Item code : (%d)\n", code); SetConsoleTextAttribute (h, wOldColorAttrs); SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY);
printf ("\n Invalid quantity!!!\n"); SetConsoleTextAttribute (h, wOldColorAttrs); printf ("\n Please enter valid quantity : "); scanf ("%d", &quantity[i]);
fflush (stdin); }
system ("cls"); menu ();
SetConsoleTextAttribute (h, FOREGROUND_BLUE | FOREGROUND_INTENSITY); if ( quantity[i] == 1 )
printf ("\n %d copy of %s has been added to the shopping cart.\n\n", quantity[i], name1[i]);
else
printf ("\n %d copies of %s has been added to the shopping cart.\n\n", quantity[i], name1[i]);
SetConsoleTextAttribute (h, wOldColorAttrs); total_price[i] = price[i] * quantity[i]; current_total += price[i] * quantity[i]; system ("pause"); system ("cls"); menu (); list[i] = i; ++i; ++no_order; }
printf ("\n Please enter the Item Code of items you wish to buy : "); scanf ("%d", &code); fflush (stdin); } int a; a = i; int loop_counter = 0;
do{
system ("cls");
printf ("\n\t\t\t\t SHOPPING CART\n");
receipt (i, no_order, list, quantity, total_price, name, current_total); printf ("\n <y> --> Proceed to checkout.");
printf ("\n <n> --> Remove item(s).\n\n"); if ( loop_counter >= 1 ){
SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY); printf (" ================\n");
printf (" Invalid Input!!!\n"); printf (" ================\n\n"); SetConsoleTextAttribute (h, wOldColorAttrs); } printf (" > "); scanf ("%c", &type); fflush (stdin); loop_counter++;
} while ( type != 'y' && type != 'n' ); if ( type == 'n' ){
system ("cls");
printf ("\n\t\t\t\t SHOPPING CART\n");
receipt (i, no_order, list, quantity, total_price, name, current_total); printf ("\n Please key in the item number you wish to Remove.");
//Type changed to key
printf ("\n <-1> --> proceed to checkout.\n"); printf ("\n Please key in the number : "); scanf ("%d", &i);
fflush (stdin); while ( i != -1 ) {
current_total -= price[i] * quantity[i]; name[i] = "Removed.\t\t";
quantity[i] = 0; total_price[i] = 0; system ("cls");
printf ("\n\t\t\t\t SHOPPING CART\n");
receipt (i, no_order, list, quantity, total_price, name, current_total);
printf ("\n Please key in the item number you wish to Remove."); printf ("\n <-1> --> proceed to checkout.\n");
// Type changed to Key
printf ("\n Selected item has been removed.\n"); printf ("\n Please key in the number : "); scanf ("%d", &i); fflush (stdin);
} }
i = a;
rounding = current_total * 100; // Added rounding adjustment.
round = (int)rounding % 10; if ( round == 0 ){
name[i] = "Rounding Adjustment\t"; name1[i] = "Rounding Adjustment"; quantity[i] = 1;
total_price[i] = 0.00; list[i] = i;
++i; ++no_order; }
else if ( round <= 3 ){
float minus = (float)round / 100; current_total -= minus;
name[i] = "Rounding Adjustment\t"; name1[i] = "Rounding Adjustment"; quantity[i] = 1; total_price[i] = -minus; list[i] = i; ++i; ++no_order; }
else if ( round > 3 && round <= 5 ){ int minus = 5 - round;
float plus = (float)minus / 100; current_total += plus;
name[i] = "Rounding Adjustment\t"; name1[i] = "Rounding Adjustment"; quantity[i] = 1; total_price[i] = +plus; list[i] = i; ++i; ++no_order; }
else if ( round > 5 && round <= 8 ){ int plus = round - 5;
float minus = (float)plus / 100; current_total -= minus;
name[i] = "Rounding Adjustment\t"; name1[i] = "Rounding Adjustment"; quantity[i] = 1; total_price[i] = -minus; list[i] = i; ++i; ++no_order; }
else if ( round > 8 && round <= 9 ){ int minus = 10 - round;
float plus = (float)minus / 100; current_total += plus;
name[i] = "Rounding Adjustment\t"; name1[i] = "Rounding Adjustment"; quantity[i] = 1; total_price[i] = +plus; list[i] = i; ++i; ++no_order; } loop_counter = 0; do { // Did some
changes in this part, especially error checking. system ("cls");
printf ("\n\t\t\t\t SHOPPING CART\n");
receipt (i, no_order, list, quantity, total_price, name, current_total); printf ("\n Select your payment method: \n");
printf (" <1> --> Cash Payment "); printf ("\n <2> --> Credit Card Payment"); printf ("\n <3> --> Exit Program.\n\n"); if ( loop_counter >= 1 ){
printf (" ================\n"); printf (" Invalid Input!!!\n"); printf (" ================\n\n"); SetConsoleTextAttribute (h, wOldColorAttrs); } printf (" > "); scanf ("%d", &type_payment); fflush (stdin); loop_counter++;
} while ( type_payment != 1 && type_payment != 2 && type_payment != 3 );
switch ( type_payment ){ //
Converted to case control. case 1:
loop_counter = 0; do {
system ("cls"); fflush (stdin);
printf ("\n\t\t\t\t SHOPPING CART\n");
receipt (i, no_order, list, quantity, total_price, name, current_total);
SetConsoleTextAttribute (h, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf ("\n\t\t\t\t******************\n"); printf ("\t\t\t\t| Cash payment |\n"); printf ("\t\t\t\t******************\n"); SetConsoleTextAttribute (h, wOldColorAttrs); if ( loop_counter >= 1 ){
SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY);
printf ("\n\n Sorry!! You do not have enough money to pay !! =( \n");
SetConsoleTextAttribute (h, wOldColorAttrs); }
printf ("\n Please enter total amount paid : RM "); scanf ("%f", &money_paid);
fflush (stdin); loop_counter++;
} while ( money_paid < current_total ); system ("cls");
printf ("\n\t\t\t\t Receipt\n");
receipt (i, no_order, list, quantity, total_price, name, current_total);
time_t now;
// TIME time (&now);
// TIME
printf (" Transaction completed on %s", ctime (&now)); money_return = money_paid - current_total;
printf ("\n\n Total amount paid : RM %.2f", money_paid); printf ("\n Total price : RM %.2f", current_total); printf ("\n Change : RM %.2f\n\n\n", money_return); printf (" Thank You! Have a Nice day!!!\n\n\n");
break;
case 2: //Added error checking for
credit card numbers and security number. loop_counter = 0;
do {
system ("cls");
printf ("\n\t\t\t\t SHOPPING CART\n");
receipt (i, no_order, list, quantity, total_price, name, current_total); printf ("\n\t\t\t***************************\n"); printf ("\t\t\t| <1> - MasterCard |\n"); printf ("\t\t\t| <2> - Visa |\n"); printf ("\t\t\t***************************\n"); if ( loop_counter >= 1 ){ SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY); printf ("\n ================\n"); printf (" Invalid Input!!!\n"); printf (" ================\n\n");
SetConsoleTextAttribute (h, wOldColorAttrs); }
printf ("\n Please select type of credit card : "); scanf ("%d", &type_card);
fflush (stdin); loop_counter++;
} while ( type_card != 1 && type_card != 2 ); system ("cls");
printf ("\n\t\t\t\t SHOPPING CART\n");
receipt (i, no_order, list, quantity, total_price, name, current_total); card_type (type_card); loop_counter = 0; do { system ("cls"); fflush (stdin);
printf ("\n\t\t\t\t SHOPPING CART\n");
receipt (i, no_order, list, quantity, total_price, name, current_total); card_type (type_card); if ( loop_counter >= 1 ){ SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY); printf ("\n ================================================\n");
printf (" Invalid Credit Card Number or Security Number!!!\n");
printf (" ================================================\n\n");
SetConsoleTextAttribute (h, wOldColorAttrs); }
printf ("\n Please key in your 1st 4 digits : "); // CHANGED
scanf ("%d", &creditno1);
// CHANGED
fflush (stdin);
printf (" Please key in your 2nd 4 digits : "); // CHANGED
scanf ("%d", &creditno2);
// CHANGED
fflush (stdin);
printf (" Please key in your 3rd 4 digits : "); // CHANGED
scanf ("%d", &creditno3);
// CHANGED
fflush (stdin);
printf (" Please key in your 4th 4 digits : "); // CHANGED
scanf ("%d", &creditno4);
// CHANGED
fflush (stdin);
printf ("\n Please key in security number : "); scanf ("%d", &securityno);
fflush (stdin); loop_counter++;
} while ( creditno1<1000 || creditno1>9999 || creditno2<1000 || creditno2>9999 || creditno3<1000 || creditno3>9999 || creditno4<1000 || creditno4>9999 || securityno<100 || securityno>999 );
loop_counter = 0; do
{
system ("cls");
printf ("\n\t\t\t\t SHOPPING CART\n");
receipt (i, no_order, list, quantity, total_price, name, current_total);
card_type (type_card);
printf ("\n Credit card number : %d - %d - %d - %d", creditno1, creditno2, creditno3, creditno4);
// CHANGED
printf ("\n Credit card security number : %d\n ", securityno);
printf ("\n\t\t ****************************************\n");
printf ("\t\t | All input are correct? |\n");
// CHANGED
printf ("\t\t | <y> - Yes, Proceed to next step . |\n");
// CHANGED
printf ("\t\t | <n> - No, Retype input . |\n"); // CHANGED printf ("\t\t ****************************************\n"); if ( loop_counter >= 1 ){ SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY);
printf ("\n ================\n"); printf (" Invalid Input!!!\n"); printf (" ================\n\n");
SetConsoleTextAttribute (h, wOldColorAttrs); }
printf ("\n Please select your choice : "); scanf ("%c", &type2);
fflush (stdin); loop_counter++;
} while ( type2 != 'y' && type2 != 'n' ); while ( type2 == 'n' ) { loop_counter = 0; do { system ("cls"); fflush (stdin);
printf ("\n\t\t\t\t SHOPPING CART\n");
receipt (i, no_order, list, quantity, total_price, name, current_total); card_type (type_card); if ( loop_counter >= 1 ){ SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY); printf ("\n ================================================\n");
printf (" Invalid Credit Card Number or Security Number!!!\n");
printf (" ================================================\n\n");
SetConsoleTextAttribute (h, wOldColorAttrs); }
printf ("\n Please key in your 1st 4 digits : "); // CHANGED
scanf ("%d", &creditno1);
// CHANGED
fflush (stdin);
printf (" Please key in your 2nd 4 digits : "); // CHANGED
scanf ("%d", &creditno2);
// CHANGED
fflush (stdin);
printf (" Please key in your 3rd 4 digits : "); // CHANGED
scanf ("%d", &creditno3);
// CHANGED
fflush (stdin);
// CHANGED scanf ("%d", &creditno4);
// CHANGED
fflush (stdin);
printf ("\n Please key in security number : "); scanf ("%d", &securityno);
fflush (stdin); loop_counter++;
} while ( creditno1<1000 || creditno1>9999 || creditno2<1000 || creditno2>9999 || creditno3<1000 || creditno3>9999 || creditno4<1000 || creditno4>9999 || securityno<100 || securityno>999 );
}
system ("cls");
printf ("\n\t\t\t\t Receipt\n");
receipt (i, no_order, list, quantity, total_price, name, current_total);
time (&now);
// TIME
printf (" Transaction completed on %s", ctime (&now)); card_type (type_card);
printf ("\n Credit card number : %d - %d - %d - %d",
creditno1, creditno2, creditno3, creditno4); // CHANGED
printf ("\n Total amount paid \t: RM %.2f\n\n\n", current_total);
printf (" Thank You! Have a Nice day!!!\n\n\n"); break;
case 3:
system ("cls");
printf ("\n You decided to exit the program. \n"); printf ("\n See you soon!!
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); exit (0); break; } return 0; } void firstpage () { printf ("********************************************************************************\n"); printf (" \n"); printf (" Welcome to World Latest PC game
store \n");
printf (" A lot of latest PC games present in out store \n");
printf
(" \n");
printf (" Tel : 03-123 4444 Mr.Sherman Tan \n"); printf (" Tel : 03-456 4444 Mr.KK \n"); printf (" No 1 , Jalan Petaling , Petaling
Jaya \n");
printf (" 46000 Petaling Jaya, Selangor. \n\n"); printf (" \n"); printf ("********************************************************************************\n"); } void menu () { printf ("\n\t\t\t\t MENU\n"); printf (" ***************************************************************************\n"); printf (" | Item Code |\t\tGAME LIST\t\t | Price |\n"); printf ("
***************************************************************************\n");
printf (" | <1> |\tCall of Duty 10 Ghosts\t\t | RM 179.99 |\n"); printf (" | <2> |\tAsssasin Creed IV Black Flag\t | RM 169.99 |\n");
printf (" | <3> |\tFootball Manager 2014\t\t | RM 149.99 |\n"); printf (" | <4> |\tNBA 2K14\t\t\t | RM 119.99 |\n");
printf (" | <5> |\tARMA III\t\t\t | RM 89.99 |\n");
printf (" | <6> |\tBATMAN ARKHAM ORIGINS\t\t | RM 69.99 |\n"); printf (" | <7> |\tChivalry Medieval Warfare\t | RM 59.99 |\n"); printf ("
***************************************************************************\n"); printf (" <0> --> Proceed to checkout.\n");
}
void receipt (int i, int no_order, int list[], int quantity[], float total_price[50], char *name[], float current_total)
{
printf ("
************************************************************************\n");
printf (" | No | Item name | Quantity | Total price |\n");
printf ("
************************************************************************\n"); for ( i = 0; i < no_order; ++i )
printf (" | %d |\t%s\t| %5d | RM %9.2f |\n", list[i], name[i], quantity[i], total_price[i]); //Changed the number list to start from 1
printf (" | | | | |\n");
printf ("
************************************************************************\n");
printf (" | TOTAL AMOUNT | RM %9.2f |\n", current_total); // CHANGED printf (" ************************************************************************\n"); // CHANGED }
void card_type (int type_card) {
HANDLE h = GetStdHandle (STD_OUTPUT_HANDLE); WORD wOldColorAttrs;
GetConsoleScreenBufferInfo (h, &csbiInfo); wOldColorAttrs = csbiInfo.wAttributes; if ( type_card == 1 )
{
SetConsoleTextAttribute (h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf ("\n\t\t\t***************************\n"); printf ("\t\t\t| MasterCard |\n"); printf ("\t\t\t***************************\n"); SetConsoleTextAttribute (h, wOldColorAttrs); } if ( type_card == 2 ) {
SetConsoleTextAttribute (h, FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf ("\n\t\t\t***************************\n"); printf ("\t\t\t| Visa |\n"); printf ("\t\t\t***************************\n"); SetConsoleTextAttribute (h, wOldColorAttrs); } }