Black box

This task is a simplification of the data that represents the transactions taking place in a shop.

Source files

There are 3 data files: “items”, “customers” and “invoices”, a header file import.h and an object file import.o.

  • The file “items” lists the description of the goods available.
  • The file “customers” lists the information about the customers.
  • The file “invoices” lists the purchases of goods by customers, essentially tying the 3 data sets together.
  • The file “import.h” contains descriptions of functions and constants that you need to use for this task. The implementation of the functions described here are in the import.o file.
  • The file “import.o” contains the assembly of the implementation of those functions described in import.h.

Task Description

Create the appropriate structure using functions. There should not be any loops or data processing as such in main().

Points are divided into 5 subtasks:

  1. Read the data from all 3 files. Use the supplied function “GetFileContents()”.  Do not attempt to write this function! The description of the function and its parameters is in the file “import.h”.
    After reading the data, print the contents of these files. For this, implement the function PrintData(). Look below for explanations.
  2. Find the sum total of all sales in the database.
  3. Find the following statistic:
    • the most expensive item that has been purchased
    • the least expensive item that has been purchased
  4. Print the invoices, include the customer data available.
  5. Update the program to include information about the items and total sum for each invoice.

For each of these tasks the file “import.h” has the constants and function descriptions that you will need.
All functions need to be able to work with files of length “n”.

Example data in files

“invoices”

invoiceID customerID time date
0 3 15:32:04 28.11.2013
1 1 22:13:03 28.11.2013

“items”

invoiceID name count price
0 Item1 1 12.99
0 Item2 1 22.99
1 Item3 2 1299

“customers”

customerID firstName lastName phone e-mail
0 John Smith +37251123123 john.smith@gmail.com
1 Jane Smith +37178787878 jane.smith@gmail.com
2 Jude Smith +37256666666 jude@outlook.com
3 Jeremy Smith +37065247 jeremy@live.com

 

Data relations between the tables

mustkast_tabelid

Using custom header files

Header files usually contain the following:

  • Macros (this includes, but is not limited to #define statements)
  • Structure declarations (Programming 2)
  • Type definitions (Programming 2)
  • Function prototypes (instead of writing the function prototypes before main(), you could write them in the included header).

To add header files, you need to refer to them in the code file using an include statement.Header files are the foundation to code reuse between different programs and code files (also for group work). This is the way we have included C standard libraries, but we can also use user-created libraries. It also helps in terms of readability.

To guard against duplicate includes from multiple .c source files, we can use conditional macros. These kind of conditional checks are also used to detect system defined macros (e.g. if system is Linux or Windows based) etc:

  • #ifndef IMPORT_H   An if statement macro. Checks if the constant IMPORT_H is not defined.
  • #define IMPORT_H  defines the constant IMPORT_H
  • #endif  Ends the if statement. Usually it’s customary to also comment which if statement it was.

Now we need to include the file. In this case we are using a relative path, meaning that the source code file has to be in the same folder as the header file. Notice that we are also using quotes instead of < and > symbols. The latter is used for libraries installed to the system.

NB! Don’t write code in your header files! It’s for prototypes and preprocessor macros mostly (e.g. #define). Don’t change the given header without a compelling reason.

Data type conversion

The stdlib library contains functions that can convert text into integers or floating point numbers. To use these you must include <stdlib.h>

For example: num = atof(stringArray[i]);

First steps (compiling it)

Command lineMakefileCode::Blocks
  1. Save all files into one folder and create your own code file (for example main.c) where you have the main() function and the rest of your code.
  2. Write a line to include the supplied header file (import.h) to main.c
  3. To test if everything works add the following line to your main() function:

printf(“%s”, CheckStatus());

To compile use the following commands:

If the compiled program prints “Working like a charm” then the preparations are complete and you can start solving the tasks.

The next step could be checking the GetFileContents() function and figuring out what variables you need to pass as parameters to this function.

  1. Save all files into one folder and create your own code file (for example main.c) where you have the main() function and the rest of your code.
  2. Write a line to include the supplied header file (import.h) to main.c
  3. To test if everything works add the following line to your main() function:

printf(“%s”, CheckStatus());

Now you need to compile your program. To make life simpler, we can use a Makefile described below. Save the contents of this to your project folder (name must be Makefile) and run “make” from the command line. You can also do this from geany by running make all. If everything went correctly, you can run the program after this and the following text should appear: „Working like a charm“. If it does, then everything went correctly and you can continue writing your program.

The next step could be checking the GetFileContents() function and figuring out what variables you need to pass as parameters to this function. GetFileContents() has already been created for you, however you will not be able to read it as it is already compiled in import.o.

  1. Create new project
    1. Project type is “Console application”.
    2. Pick the C language
    3. Type the name and location of the project
    4. Don’t change any compiler configuration!
  2. Use only the compile button to compile the program! If there weren’t any errors nor warnings then you can run the project.
    If you use the compile and run button then you won’t be able to see the warning messages and this might cause problems when the program is running.
  3. The files of the project are listed in a tab on the right in the form of a folder tree. In the beginning only the c file should be visible in the Sources folder.
    1. Sources – code files
    2. Headers – header files
    3. Other – data files and other files
  4. Copy the following files to project folder:
    1. import.h
    2. import.o
    3. customers
    4. items
    5. invoices
  5. Make an additional copy of the import.o file to the folder “obj -> Debug”
  6. Add the files to the project by right clicking on the project name and selecting “Add files..”
    NB! If asked where the files should belong then both “Debug” and “Release” should be checked.
  7. Include the supplied library (import.h) to your main.c
  8. To test if everything works add the following line to your main() function:

printf(“%s”, CheckStatus());

If the compiled program prints “Working like a charm” then the preparations are complete and you can start solving the tasks.

The next step could be checking the GetFileContents() function and figuring out what variables you need to pass as parameters to this function.

First function PrintData()

To be able to see the data that was read by the function GetFileContents(), you should create a printing function. This should be done after you have got at least one of the calls to GetFileContents() working. The declaration of the function has been done for you alongside with the comment. Your job is to implement this function – create the necessary loops and variables, use the printf() function in the correct way. Once done, you just call this function 3 times with different parameters.

Example of expected output: