# CS201 Assignment 3 Solution and Discussion

• ``````Assignment No.  3
``````

Semester: Fall 2020
CS201 – Introduction to Programming Total Marks: 20
Due Date:
29-01-2021

Instructions
It should be clear that your assignment will not get any credit if:

o Assignment is submitted after due date.
o Submitted assignment does not open or file is corrupt.
o Assignment is copied (From internet/students).
o Assignment is not in .cpp format.

Software allowed to develop Assignment

• Dev C++

Objectives:
In this assignment, the students will learn:
• Use of class in programming
• How to deal with a file in programming
• How to implement switch statement for Class based functions.

ABC bakery is using a console based inventory management system for the receipt generation purposes. Console application will help the cashier in calculating total price of each item with respect to its price. Menu list will provide Add an item option to take data about an inventory item, include Item Id, Item name, price and quantity. Relevant data of all items will be displayed on screen with the help of menu option. Quantity amount of items will changeable if user wants to add quantity of an item.

Problem Statement
Write a C++ program to manage the inventory item using your knowledge about file handling and classes. User will manage details of an Inventory item using menu list that will provide three options:

ENTER CHOICE

2. DISPLAY FILE DATA
3. INCREASE QUANTITY

Prompt will show to the user for continue the program after dealing with each option, until user will press a key other than ‘y’.

Instructions to write C++ program:

You will use class “Inventory” to declare inventory data and info
Make “Inventory.txt” file to save inventory item record
“Inventory.txt” file will delete each time when the program will run
“ERROE IN OPENING FILE” will be shown if user not press ‘1’ when program will execute first time.
You will use switch statement to handle different conditions and to perform different actions based on the different actions, that is, choice 1, 2, and 3.

Code structure [ Demonstration]:
You will be using the following class and other functions to develop the assignment:

``````class Inventory
{
private:
int itemID;
char itemName[20];
float itemPrice;
float quantity;
float totalPrice;

public:
void displayItem();
int getItemID() ;
float getPrice() ;
float getQuantity() ;
void updateQuantity(float q);
};

//Deleting existing file
void deleteExistingFile(){--------}

//Appending item in file
void appendToFille(){------------}

//Displaying items
void displayAll(){------------}

//Increasing Quantity of item
void increaseQuanity(){------------}
``````

Program Output:

1. User’s prompt when program will execute for the first time.

2. When user press ‘1’ , It will take data about an inventory item as an input from the user.

3. When user press ‘2’, it will read data from “Inventory.txt” file and display record of all inventory items on the screen

4. If user press ‘3’, it will ask to enter Item id against which user want to increase item quantity.

5. Now, when the user will press ‘2’, the inventory item record will be shown as:

Assignment#3 covers course contents from lecture 17 to 30.

Best of Luck!

• Assignment No. 3
Semester: Fall 2020
CS201 – Introduction to Programming Total Marks: 20
Due Date:
29-01-2021

Instructions
It should be clear that your assignment will not get any credit if:

o Assignment is submitted after due date.
o Submitted assignment does not open or file is corrupt.
o Assignment is copied (From internet/students).
o Assignment is not in .cpp format.

Software allowed to develop Assignment

• Dev C++

Objectives:
In this assignment, the students will learn:
• Use of class in programming
• How to deal with a file in programming
• How to implement switch statement for Class based functions.

ABC bakery is using a console based inventory management system for the receipt generation purposes. Console application will help the cashier in calculating total price of each item with respect to its price. Menu list will provide Add an item option to take data about an inventory item, include Item Id, Item name, price and quantity. Relevant data of all items will be displayed on screen with the help of menu option. Quantity amount of items will changeable if user wants to add quantity of an item.

Problem Statement
Write a C++ program to manage the inventory item using your knowledge about file handling and classes. User will manage details of an Inventory item using menu list that will provide three options:

ENTER CHOICE

2. DISPLAY FILE DATA
3. INCREASE QUANTITY

Prompt will show to the user for continue the program after dealing with each option, until user will press a key other than ‘y’.

Instructions to write C++ program:

You will use class “Inventory” to declare inventory data and info
Make “Inventory.txt” file to save inventory item record
“Inventory.txt” file will delete each time when the program will run
“ERROE IN OPENING FILE” will be shown if user not press ‘1’ when program will execute first time.
You will use switch statement to handle different conditions and to perform different actions based on the different actions, that is, choice 1, 2, and 3.

Code structure [ Demonstration]:
You will be using the following class and other functions to develop the assignment:

``````class Inventory
{
private:
int itemID;
char itemName[20];
float itemPrice;
float quantity;
float totalPrice;

public:
void displayItem();
int getItemID() ;
float getPrice() ;
float getQuantity() ;
void updateQuantity(float q);
};

//Deleting existing file
void deleteExistingFile(){--------}

//Appending item in file
void appendToFille(){------------}

//Displaying items
void displayAll(){------------}

//Increasing Quantity of item
void increaseQuanity(){------------}
``````

Program Output:

1. User’s prompt when program will execute for the first time.

2. When user press ‘1’ , It will take data about an inventory item as an input from the user.

3. When user press ‘2’, it will read data from “Inventory.txt” file and display record of all inventory items on the screen

4. If user press ‘3’, it will ask to enter Item id against which user want to increase item quantity.

5. Now, when the user will press ‘2’, the inventory item record will be shown as:

Assignment#3 covers course contents from lecture 17 to 30.

Best of Luck!

``````#include<iostream>
#include<fstream>
#include<stdio.h>

using namespace std;

class Inventory
{
private:
int itemID;
char itemName[20];
float itemPrice;
float quantity;
float totalPrice;
public:
void displayItem();
int getItemID()   { return itemID;}
float getPrice()    { return itemPrice;}
float getQuantity()    { return quantity;}
void updateQuantity(float q)
{
quantity=q;
totalPrice = (itemPrice*quantity);
}
};

//    Getting Item
cout << "Please enter item id: ";
cin >> itemID;
cout << "Please enter item name: ";
cin.ignore(1);
cin.getline(itemName,20);
cout << "Please enter price: ";
cin >> itemPrice;
cout << "Please enter quantity: ";
cin >> quantity;
totalPrice = (itemPrice*quantity);
}

//  Displaying Item
void Inventory::displayItem()
{
cout << "Item id:" << itemID << "\tItem name:" << itemName << "\t ItemPrice:" << itemPrice << "\t Quantity:" << quantity << "\t TotalPrice:" << totalPrice << endl;
}

//  Deleting existing file
void deleteExistingFile(){
remove("Inventory.txt");
}

//  Appending item in file
void appendToFille(){
Inventory x;

ofstream file;
file.open("Inventory.txt",ios::binary | ios::app);

if(!file){
cout << "ERROR WHILE CREATING A FILE!\n";
return;
}

file.write((char*)&x,sizeof(x));
file.close();
}

//  Displaying items
void displayAll(){
Inventory x;
ifstream file;
file.open("Inventory.txt",ios::binary | ios::in);

if(!file){
cout<<"ERROR IN OPENING FILE \n";
return;
}
while(file){
x.displayItem();
}
file.close();
}

//  Increasing Quantity of item
void increaseQuanity(){
int itemValue;
int isFound=0;
int q;
Inventory x;

cout<<"Enter item id: \n";
cin >> itemValue;

cout<<"ERROR IN OPENING FILE \n";
return;
}

if(x.getItemID() == itemValue){
cin >> q;
x.updateQuantity(x.getQuantity() + q);
isFound=1;
break;
}
}
}

if(isFound==0){
}

deleteExistingFile();

ofstream fileWrite;
fileWrite.open("Inventory.txt",ios::binary | ios::app);
fileWrite.write((char*)&x,sizeof(x));

fileWrite.close();
cout << "Item Quantity updated successfully."<<endl;
}

int main()
{
char ch;

do
{
char n;

cout << "ENTER CHOICE\n" << "1. ADD AN INVENTORY ITEM\n" << "2. DISPLAY FILE DATA\n" << "3. INCREASE QUANTITY\n";
cout << "Please select a choice: ";

cin >> n;

switch(n)
{
case '1':
appendToFille();
break;
case '2' :
displayAll();
break;
case '3':
increaseQuanity();
break;
default :
cout << "Invalid Choice\n";
break;
}

/*    else
cout<<"Enter integer value only";
}
*/
cout << "Do you want to continue? : ";
cin >> ch;

}
while(ch=='Y'||ch=='y');

return 0;

}

``````

5

5

2

4

2

7

1

2
| |