11. Cafeteria Management System Code - Part 2


Hello folks!
Welcome to the blog series.In the previous blog we have seen about the libraries that we used in our program as well as the declaration of class, methods and variables. So in this blog we will continue with the remaining part code which includes global variables as well as main() function.

So as you can see we have used the standard namespace for using standard input and output. Next is the flag which is a global variable which is used so that the first part of the program which gives the user output whether the program has started runs only once. Another variable is of type ofstream which is used for writing every bill printed into a log file.

Another global variable is a pointer which stores the address of objects of class Cafe. In the main function we have declared two variable cmd which stores the option number for the item which the customer has ordered and table_number which stores table number. As explained earlier the use of flag variable the user gets to know the program has started. Also we are opening the Cafe_log.txt file to write the new bills. Next we are taking the input for table number and corresponding option for the item that the customer has ordered.
According to the option that the user has entered the program asks for the quantity for that item that the customer wants to order or print the bill or exit from the program. If the user opts for the option for the bill we also simultaneously write the bill the log file and if the user wants to exit the program then we close the log file which was opened earlier.

If the user enters in inappropriate command then an message is displayed for the same. So this is all about the code.The link of the code is attached below. In the next blog we will discuss about theoretical background of the project which is based on socket programming.


By :
Ashutosh Bardapurkar    (k-05)
Hrishikesh Deshpande    (k-16)
Archit Hiwrekar        (k-23)
Chinmay Kapkar    (k-33)

10. Cafeteria Management System Code - Part 1


Hello folks!
Welcome back to this blog series. In the previous blog we have studied advance ways of file handling .
Now In this blog we will see the code for Cafeteria Management System which is an application of object oriented programming studied in prior blogs. So lets start by knowing the libraries used.

Theee libraries that we required in order the implement the code are mentioned above. I will explain you some of the important libraries we needed. So the first one is the iostream library which contains the input output streams which are needed to make the user experience more better.
Next is the fstream library which contains necessary functions for file handling. The stdlib.h library is  used for dynamic memory allocation so as to make the space complexity low. Another important library is string.h which is required to perform string operations. We have also used a macro ‘max_value’ which is used to store the maximum number of tables available in the cafe.
Next task is to create a class and then create objects of this class which denote food serving tables of cafe.

So as you can see we have declared a class named Cafe which has private members price which is an array containing prices of each item present in the menu card which we will be displaying in the main() method. Quantity variable contains quantity of each item that the customer has ordered and bill contains the total amount that the customer needs to pay. In the constructor of the class we are setting values of arrays price and quantity to zero assuming that no customer is there on the table and cafe is just started. Next task is to set which items needed to be displayed in the menu. For example, we have taken four items as shown in the above picture. Also we have set the bill value to zero.
Now we need to create a method which would actually do the processing of data obtained from customer which includes getting the order, changing the appropriate values of quantity array and to calculate the total bill.


As you can see in the above image we have created a function named option which takes item number of the item ordered as input and changes values of quantity array and variable bill.


Next function is display which takes a fstream pointer as input. In this function we are basically display the items that we ordered and their respective quantities along with price and total amount. Simultaneously, we are also storing this bill as text in a file using the file pointer that we took as input.
Finally, in the destructor we are assigning bill variable to zero assuming that the previous customer has left the cafe and new customer has arrived. Also, we are assigning zero value to all elements of price and quantity array respectively.
So this is the end of class declaration and in the next blog we’ll explore the main() function and try to understand how the user will interact with the program. We will also examine the need for some global variables that we have used in the program. 

By :
Ashutosh Bardapurkar    (k-05)
Hrishikesh Deshpande    (k-16)
Archit Hiwrekar        (k-23)
Chinmay Kapkar    (k-33)

9. Advance way of handling files in C++




Hello folks!
Welcome to this blog series. In the previous blog we have studied basic file handling in C++. In this blog we will see some more advance ways of file handling in C++, so let’s get started.

Advance way of handling files in C++

There can be different purpose of opening a file. For example to write on the file, to read from the file, etc. These are the different modes in which we can open a file in C++.


Mode
Description
ios::app
To open a text file for appending purpose.
ios::ate
To open a file for output purpose and move the read/write control to the end of the file.
ios::in
To open a text file for reading purpose.
ios::out
To open a text file for writing purpose.
ios::trunc
To truncate the file content before opening a file, if file already exists in our system.


















Let’s understand this with an example code.

Code:
Output:
Files before running the code:
Files after running the code: 

So we now know how to play with files in C++. So we will meet you in our next blog where we will discuss, how we implemented the system in C++ with its code explained.

By-
Ashutosh Bardapurkar    (k-05)
Hrishikesh Deshpande    (k-16)
Archit Hiwrekar        (k-23)
Chinmay Kapkar    (k-33)

8. Basics of File Handling in C++


Hello folks!
Welcome to this blog series. In the previous blog we have studied see how Dynamic memory is allocated in C++.
Now in this blog we will study how to handle files in C++.  So why do we need file handling knowledge while designing our system? We require File handling because know when our system is fully build and given to the user to operate it, the user of the system needs to know how much transaction had occurred since past check for various reasons such as to calculate the profit the Cafeteria is making. Number of items sold for the quantity of goods to be purchased etc. So let’s get started with this blog session.

Basics of File Handling in C++

The Basic file handing library in C++ is fstream. The fstream library contains
1.     ofstream object/data type  which Creates and writes to files,
2.     ifstream object/data type  which Reads from files
3.    fsrteam class which contains file handling functions.
Now if we want to create a file and write into it, we basically use ofstream or fstream object and specify the name of file to be created.
We use insertion operator (<<) to write in the file. Let’s take an example to see how it is done.

Code:
Output:
Text File:

Similarly if we want to just read the file, we use ifstream or fstream object and specify the name of file to be read.
We use extraction operator (>>) to read from the file. Let’s take an example to see how it is done.
We will read from the above created file for understanding purose.

Code:
Output:

Some of us will get a doubt that why did I close the file, because in C++ by default all the files opened are closed and all related data is released, the answer to this question is “as a good programming practice we should write file closing syntax is our code”. Not all the programming languages out there have such functionality.
So now we can do basic file handling in C++.
Some more advance ways of file handling in C++ will be discussed in our next blog.

By-
Ashutosh Bardapurkar    (k-05)
Hrishikesh Deshpande    (k-16)
Archit Hiwrekar        (k-23)
Chinmay Kapkar    (k-33)



7. Dynamic Memory Allocation in C++.



Hello folks!
Welcome to this blog series. In the previous blog we have studied different type of memory allocation. Now in this blog we will see how Dynamic memory is allocated in C++
Dynamic memory allocation in C++ :
In C++ all the functions related to dynamic memory allocation are present in the “cstdlib” library. Also, C++ provides additional dynamic memory allocation operators like ‘new’ and ‘delete’ operators in the “iostream” library. The following is syntax of new and delete operators for allocating memory for variable and array respectively.
new data-type;
new data-type[size];
delete variable-name;
delete[] variable-name;
The ‘new’ operator basically returns and address to memory location which needs to be stored in a pointer type of variable of the same data type. Other functions present in the “cstdlib” library are malloc(), calloc(), realloc(), free.

malloc() function:
‘malloc’ stands for memory allocation. The malloc() function reserves a block of memory of the specified number of bytes and it returns a pointer of void which can be casted into pointers of any form. The syntax for malloc() is
(castType*)malloc(size)

calloc() function:
The name ‘calloc’ stands for contiguous allocation. The malloc() function allocates memory and leaves the memory uninitialized. Whereas, the calloc() function allocates memory and initializes all bits to zero. The syntax for calloc() is
(castType*)calloc(n, size)

realloc() function:
‘realloc’ or ‘re-allocation’ function is used to dynamically change the memory allocation of a previously allocated memory. In other words, if the memory previously allocated with the help of malloc() or calloc() is insufficient, realloc() can be used to dynamically re-allocate memory. The syntax for realloc() is
realloc(ptr, newSize)

free() function:
‘free’ method is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() is not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it. The syntax for free() is
free(ptr)
Below are few screenshots of the code for our system in which we have used these dynamic memory allocation concepts.
       
       

     So this is all about dynamic memory allocation in C++. In the next blog we will discuss about the basics of file handling in C++.

References :

By-
Ashutosh Bardapurkar    (k-05)
Hrishikesh Deshpande    (k-16)
Archit Hiwrekar        (k-23)
Chinmay Kapkar    (k-33)