Source Code

December 26, 2016 | Author: yongjin95 | Category: N/A
Share Embed Donate


Short Description

code...

Description

#include #include #include #include #include void void void void char

// added TIME //for text coloring. No pseudocode or flowchart needed.

firstpage (); menu (); card_type (int card_type); receipt (int i, int num_order, int list[], int quantity[], float total_price[50], *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 --> Proceed to checkout."); printf ("\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 --> 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 printf ("\n

Please key in the item number you wish to Remove."); --> 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; 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;

// Added rounding adjustment.

++i; ++no_order; } else if ( round 3 && round 5 && round 8 && round = 1 ){ SetConsoleTextAttribute (h, FOREGROUND_RED | FOREGROUND_INTENSITY);

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); printf ("

// TIME 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: credit card numbers and security number. loop_counter = 0;

//Added error checking for

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| - MasterCard |\n"); printf ("\t\t\t| - 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 ( creditno19999 || creditno29999 || creditno39999 || creditno49999 || securityno999 ); 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 |\n");

|

All input are correct?

printf ("\t\t

// CHANGED | - Yes, Proceed to next step .

printf ("\t\t

// CHANGED | - No, Retype input .

|\n"); |\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); 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 ( creditno19999 || creditno29999 || creditno39999 || creditno49999 || securityno999 ); } system ("cls"); printf ("\n\t\t\t\t Receipt\n"); receipt (i, no_order, list, quantity, total_price, name, current_total); time (&now); printf ("

// TIME Transaction completed on %s", ctime (&now));

card_type (type_card); printf ("\n Credit card number creditno1, creditno2, creditno3, creditno4); printf ("\n

Total amount paid

: %d - %d - %d - %d", // CHANGED \t: RM %.2f\n\n\n",

current_total); printf (" break;

Thank You! Have a Nice day!!!\n\n\n");

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 (" printf (" printf ("

Jaya

Tel : 03-123 4444 Mr.Sherman Tan Tel : 03-456 4444 Mr.KK No 1 , Jalan Petaling , Petaling

\n"); \n");

\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 (" | |\tCall of Duty 10 Ghosts\t\t | RM 179.99 |\n"); printf (" | |\tAsssasin Creed IV Black Flag\t | RM 169.99 |\n"); printf (" | |\tFootball Manager 2014\t\t | RM 149.99 |\n"); printf (" | |\tNBA 2K14\t\t\t | RM 119.99 |\n"); printf (" | |\tARMA III\t\t\t | RM 89.99 |\n"); printf (" | |\tBATMAN ARKHAM ORIGINS\t\t | RM 69.99 |\n"); printf (" | |\tChivalry Medieval Warfare\t | RM 59.99 |\n"); printf (" ***************************************************************************\n"); printf (" --> 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; CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

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); } }

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF