This was a project to learn about C programming. The following concepts were implemented:
- Pointers to Functions + Callback Functions
- External Variables
- Header Files
- Linking Multiple C Files
- Macro + Conditional Substitution
- Multidimensional Arrays
- Pointers + Pointers to Arrays
- Structs + Arrays of Structs
- Sorting + Quicksort
- Recursion
- File Input/Output (formatted)
- User Input/Output (formatted)
- Random Generation
- Static Memory Allocation
This project randomly generates a credit card record with amounts, dates, locations and stores it in an array of structures (each struct represents a transaction). The user is given menu options that allows them to print their record to the screen, or to a Record.txt file, sort by date (in both old-->new and new-->old directions), sort by amount (in both low-->high and high-->low directions), add a transaction, or leave the program.
- Make a folder to hold the repository
- Clone the project:
% git clone https://github.com/RupinMittal/Credit-Card-System.git - Compile the code:
% gcc RecordKeeper.c RecordGenerator.c RecordSorter.c RecordPrinter.c - Run the executable:
% ./a.out
The Record is an externally declared array since almost every function in the program must access it. It would be cumbersome to pass a pointed in every function call to every function.
The Actual Record
Each transaction is a struct containing an amount, a date, and a location. An array called record stores all the transactions
Generating the Record
The record is randomly generated by using the rand function and by setting a new seed. The int amount is chosen from between $0-2000. Since the Location is a string (char[]), a random location is chosen from a multidimensional array containing many locations. For the date string, random (based on the calendar) numbers are generated for mmddyy and '/' is placed in the appropriate positions
Sorting the Record
Quicksort was written from scratch to sort the record. Since there are 4 ways to sort the record: (date --> old/new and new/old, and amount --> high/low and low/high), callback functions and function pointers were used to avoid writing duplicate sorting code. A single sort function was written, and 2 different callbacks (compareDate and compareAmount). When the sort function was called, it was passed a pointer to the appropriate callback function for comparing. To avoid writing duplciate code for the top/down vs down/top direction, each of these callback functions had a int direction parameter. If the reverse order was requested, then the result of the comparision (1 or -1) would be switched. This way the sort function didn't have to account for the sorting type and direction, it just used the function pointer passed to it.
Printing the Record
Two options were provided to the user for printing the record. They could print to the system screen or to a file. Both printing options printed formated outputs. If the to file option was chosen, a file called "Record.txt" is created with the formatted record.
Managing the User's Options
A menu is printed to the user and the user may enter the corresponding number for their choice. A switch case is used to call the necessary functions for that request.
CreditCardSystem.h - Header files that defines the Transaction struct, and declares the extern record array.
RecordHeader.c - Header files that declares the function signatures in the various files used to manage the record
RecordKeeper.c - Calls generator to create intitial record. Runs menu option for user and calls the appropriate functions to fullfill user's request
RecordSorter.c - Has the sorting (and its assisting) functions. Also has the callback function definitions for sorting
RecordGenerator.c - Randomly creates the initial record and stores it in the record array
RecordPrinter.c - Contains the two functions to print the formatted record to the location the user requests
The initial menu that the user sees is this:
(In no necessary order): The user can then choose to sort the record (in whichever way they like). They could also print to the screen or to the Record.txt file. They could also add a transaction
Adding a transaction would look like this:
Printing to the system would look like this:
Printing to the Record.txt file would look like this:



