Ad Code

CS201 Assignment No 3 Solution 2021

Download Short Notes Download GDB Download Past Paper Assignment Solution 

CS201 Assignment No 3 Solution 2021

#include <iostream>
#include <stdio.h> #include <string> #include <fstream> #include <sstream> using namespace std; class Inventory{ private: int itemID; char itemName[20]; float itemPrice; float quantity; float totalPrice; public: void readItem(){ cout<<"Please enter item id: "; cin>>itemID; cout<<"Please enter item name: "; cin>>itemName; cout<<"Please enter item price: "; cin>>itemPrice; cout<<"Please enter item quantity: "; cin>>quantity; totalPrice = itemPrice * quantity; char inventory[] = "Inventory.txt"; fstream fs; fs.open(inventory, ios::app); if(!fs.is_open()){ cout<<"Error Opeingin File, try again!"; return; } fs<<"Item id:"<<itemID<<"\t Item name:"<<itemName; fs<<"\t ItemPrice:"<<itemPrice<<"\t Quantity:"<<quantity; fs<<"\t TotalPrice:"<<totalPrice<<"\n"; if(!fs.bad()){ cout<<"Inventory record(s) added successfully."<<endl; } fs.close(); } void displyItem(){ fstream fs; fs.open("Inventory.txt"); if(!fs.is_open()){ cout<<"ERROE IN OPENING FILE"; } string line; while ( getline (fs, line) ){ cout << line <<endl; } fs.close(); } int getItemID(){ return itemID; } float getPrice(){ return itemPrice; } float getQuantity(){ return quantity; } void updateQuantity(float q){ quantity = q; } }; //Deleting existing file void deleteExistingFile(){ fstream fs; fs.open("Inventory.txt"); if(fs.is_open()){ fs.close(); remove("Inventory.txt"); } } //Appending item in file void appendToFille(){ Inventory inv; inv.readItem(); } //Displaying items void displayAll(){ Inventory inv; inv.displyItem(); } //Increasing Quantity of item void increaseQuanity(){ int id; float q; Inventory inv; cout<<"Enter item id:"<<endl; cin>>id; cout<<"Add quantity? "; cin>>q; inv.updateQuantity(q); ifstream fs; fs.open("Inventory.txt"); if(!fs.is_open()){ cout<<"ERROE IN OPENING FILE"; } string line, targetId, newContent, Q, T; int colonCount = 0, spaceCount = 0, preSpCount = 0; int strToInt, nq, t, newQuantity; bool found = false; while(getline(fs, line)){ if(!found){ for(int i = 0; i < line.length(); i++){ if(line[i] == ':'){ colonCount++; } if(isspace(line[i])){ spaceCount++; } if(colonCount == 1 && spaceCount == 1 && line[i] != ':'){ targetId += line[i]; stringstream ss(targetId); ss >> strToInt; if(strToInt == id){ found = true; } } if(colonCount == 5){ preSpCount = spaceCount; } if(found && colonCount == 4 && line[i] != ':' && spaceCount == 8){ Q += line[i]; if(isspace(line[i + 1]) || line[i + 1] == '\t' || line[i + 1] == '\0' || line[i + 1] == '\n'){ stringstream ss2(Q); ss2 >> nq; stringstream ss3; newQuantity = nq + inv.getQuantity(); ss3 << newQuantity; string temp; ss3 >> temp; newContent += temp; } }else if(found && colonCount == 5 && line[i] != ':' && preSpCount == spaceCount){ T += line[i]; if(isspace(line[i + 1]) || line[i + 1] == '\0' || line[i + 1] == '\n'){ stringstream ss4(T); ss4 >> t; int tempPrice = t / nq; tempPrice *= newQuantity; stringstream ss5; ss5 << tempPrice; string temp2; ss5 >> temp2; cout<<"if"<<tempPrice<<temp2; newContent += temp2; } }else{ newContent += line[i]; } } }else{ newContent += line; } } fs.close(); if(found){ ofstream of; of.open("Inventory.txt", ios::trunc); of << newContent; of.close(); } } void showMenu(){ cout<<"ENTER CHOICE"<<endl; cout<<"1. ADD AN INVENTORY ITEM"<<endl; cout<<"2. DISPLAY FILE DATA"<<endl; cout<<"3. INCREASE QUANTITY"<<endl; cout<<"Please select a choice:"; } int main(){ deleteExistingFile(); char useAgain; do{ showMenu(); int ch; cin>>ch; switch(ch){ case 1: appendToFille(); break; case 2: displayAll(); break; case 3: increaseQuanity(); break; } cout<<endl<<"Do you want to continue? : "; cin>>useAgain; }while(useAgain == 'y' || useAgain == 'Y');

Post a Comment

0 Comments

Close Menu