In: Computer Science
We want to develop a sales system for a mobile shop. The system should keep information about the mobile phones available at the shop. the system should allow the user the ADD new mobile phones, one at a time, along with their features. The system should also allow to search mobile phones , update their information, and delete them (one by one). Try to provide different criteria for search. When the system starts, it should load the information about the mobile phones forma file. if the file doesn't exist, the system should create the file. The system should provide a (console based) menu to interact with it. The system not use any arrays and all the changes should be reflected in the file.
Language: C++
The structure of mobile data which is have taken is:
Model No , Brand, Price, launching year, RAM, camera, Internal Storage
These all are seperated by commas in the file. Each line of the file contains info about a mobile.
I have implemented search for model no. only, but you can expand it to as many features as you want.
The program is:
#include<iostream>
#include<fstream>
#include<string.h>
#include<sstream>
using namespace std;
int main(){
//filename of file to be manipulated
string filename = "data.txt";
//Loading the data_file
fstream f;
//It wil create file if it does not exits and will open it if it exists
f.open(filename, std::fstream::in | std::fstream::out | std::fstream:: app );
//Menu for the User
cout<<"\n";
cout<<"\t-----------------Welcome to the XYZ mobile shop----------------\n";
//option for choosing
int op;
//variables required for query
string temp, temp1, data, model_no, brand, price, launch_yr, ram, camera, istorage;
int flag, k;
//while loop to take continuous inputs from user
while(1){
cout<<"\n\nChoose one of the following query to perfomr:\n\n";
cout<<"1) Add a new mobile phone to record\n";
cout<<"2) Search for mobile phones\n";
cout<<"3) Update specs of a mobile phone\n";
cout<<"4) Delete a mobile phone from record\n";
cout<<"5) Exit the system\n";
cin>>op;
//choosing what to do on the basis of user's choice
switch(op){
//Add new mobile to record
case 1:
cout<<"Enter the following details about the phone:"<<endl;
cout<<"Enter the model no. of mobile phone\n";
cin>>model_no;
cout<<"Enter the brand of mobile phone\n";
cin>>brand;
cout<<"Enter the year when mobile phone was launched\n";
cin>>launch_yr;
cout<<"Enter the price of mobile phone\n";
cin>>price;
cout<<"Enter the RAM of mobile phone\n";
cin>>ram;
cout<<"Enter the camera strength(MP) of mobile phone\n";
cin>>camera;
cout<<"Enter the internal storage of mobile phone\n";
cin>>istorage;
cout<<"saving info of the new mobile phone............."<<endl;
data = model_no + "," + brand + "," + price + "," + launch_yr + "," + ram + "," + camera + "," + istorage + "\n";
f << data;
f.flush();
cout<<"Info saved successfuly.........."<<endl;
break;
//Update record of a mobile phone
case 3:
cout<<"Enter the Model No of the mobile phone to Update:\n";
cin>>model_no;
flag = 0;
data = "";
f.seekg(0);
while(getline(f, temp)){
stringstream check1(temp);
// Tokenizing w.r.t. space ' '
while(getline(check1, temp1, ','))
{
if(temp1 == model_no){
flag = 1;
}
else data += temp + "\n";
break;
}
}
if(flag == 0) cout<<"The model_no is not found in record\n";
else{
cout<<"Enter the brand of mobile phone\n";
cin>>brand;
cout<<"Enter the year when mobile phone was launched\n";
cin>>launch_yr;
cout<<"Enter the price of mobile phone\n";
cin>>price;
cout<<"Enter the RAM of mobile phone\n";
cin>>ram;
cout<<"Enter the camera strength(MP) of mobile phone\n";
cin>>camera;
cout<<"Enter the internal storage of mobile phone\n";
cin>>istorage;
cout<<"updating info of the new mobile phone............."<<endl;
data = model_no + "," + brand + "," + price + "," + launch_yr + "," + ram + "," + camera + "," + istorage + "\n";
f.close();
f.open("dt.txt", std::fstream::in | std::fstream::out | std::fstream:: app);
remove("data.txt");
rename("dt.txt", "data.txt");
f << data;
f.flush();
cout<<"Info updated successfuly.........."<<endl;
}
break;
//Delete record of a mobile phone
case 4:
cout<<"Enter the Model No of the mobile phone to delete:\n";
cin>>model_no;
flag = 0;
data = "";
f.seekg(0);
while(getline(f, temp)){
stringstream check1(temp);
// Tokenizing w.r.t. space ' '
while(getline(check1, temp1, ','))
{
if(temp1 == model_no){
flag = 1;
cout<<"Deletion sucessfull\n";
}
else data += temp + "\n";
break;
}
}
if(flag == 0) cout<<"The model_no is not found in record\n";
if(flag == 1) {
f.close();
f.open("dt.txt", std::fstream::in | std::fstream::out | std::fstream:: app);
remove("data.txt");
rename("dt.txt", "data.txt");
f << data;
f.flush();
}
break;
//Search a mobile no. on the basis of model no.
case 2:
cout<<"Enter the model no. to search\n";
cin>>model_no;
flag = 0;
k = 0;
data = "";
f.seekg(0);
while(getline(f, temp)){
k = 0;
stringstream check1(temp);
// Tokenizing w.r.t. space ' '
while(getline(check1, temp1, ','))
{
if(temp1 == model_no || k == 1){
flag = 1;
k = 1;
cout<<temp1<<"\t";
}
}
cout<<endl;
}
if(flag == 0) cout<<"The model_no is not found in record\n";
break;
//exit the system
case 5:
cout<<"Have a good day!"<<endl;
f.close();
exit(0);
}
}
return 0;
}
Output: