Giter VIP home page Giter VIP logo

railwaycli's Introduction

Ali Koleiny Zadeh

My Skills

  • ๐Ÿ’ป I'm currently learning backend development from this roadmap
  • โค๏ธ I'm a user and lover of linux

footer-separator

GitHub Metrics 01

GitHub Metrics 02

railwaycli's People

Contributors

alikzalikz avatar

Watchers

 avatar

railwaycli's Issues

includes and namespace

includes and namespace

RailwayCli/main.cpp

Lines 1 to 6 in 021ebb1

#include<iostream>
#include<fstream>
#include<string>
#include<bits/stdc++.h>
using namespace std;

the #include<bits/stdc++.h> used for transform(... ::tolower) function
like this example

RailwayCli/main.cpp

Lines 148 to 154 in 021ebb1

cout << "Enter Passenger name: ";
getline(cin, person.name);
transform(person.name.begin(), person.name.end(), person.name.begin(), ::tolower);
cout << "Enter Passenger family: ";
getline(cin, person.family);
transform(person.family.begin(), person.family.end(), person.family.begin(), ::tolower);

Input: 5 & other

Input: 5. Exit & other

RailwayCli/main.cpp

Lines 331 to 341 in 021ebb1

case e5:
{
cout << "\n-- GOOD LUCK --";
state = false;
break;
}
case other:
{
cout << "\n-- Invalid Number! Please Try again --\n\n\n";
break;
}

why other on #3

Input: 2

Input: 2. Reserve a Compartment

RailwayCli/main.cpp

Lines 306 to 310 in 021ebb1

case e2:
{
AddCompartment();
break;
}

void AddCompartment()

RailwayCli/main.cpp

Lines 183 to 226 in 021ebb1

void AddCompartment()
{
cout << endl;
Person person;
ofstream File("Train.txt", ios::app);
ifstream InFile("Train.txt");
cin.ignore();
cout << "Enter Passenger name: ";
getline(cin, person.name);
transform(person.name.begin(), person.name.end(), person.name.begin(), ::tolower);
cout << "Enter Passenger family: ";
getline(cin, person.family);
transform(person.family.begin(), person.family.end(), person.family.begin(), ::tolower);
string fullName = person.name + ", " + person.family;
if (HaveTicket(fullName))
{
cout << "\n-- Passenger Have Ticket Already! --" << endl;
ShowTicket(fullName);
}
else
{
if (AvailabeCompartment() != "0")
{
person.compartment = AvailabeCompartment();
for (int seatsI = 0; seatsI < 6; seatsI++)
{
person.seat = Availabe(1);
File << person.name << ", " << person.family << ", " << person.compartment << ", " << person.seat << endl;
}
File.close();
cout << "\n-- Tickets Added Successfully! --" << endl;
ShowTicket(fullName);
}
else
{
cout << "\n-- Don't Have Empty Compartment! --\n\n\n";
}
}
}

string AvailabeCompartment()

RailwayCli/main.cpp

Lines 67 to 80 in 021ebb1

string AvailabeCompartment()
{
string compartmentNum = "0";
for (int compartmentsI = 1; compartmentsI <= 10; compartmentsI++)
{
string str = " " + to_string(compartmentsI) + ", ";
if (!HaveTicket(str))
{
compartmentNum = to_string(compartmentsI);
break;
}
}
return compartmentNum;
}

string Availabe(int state)

RailwayCli/main.cpp

Lines 36 to 65 in 021ebb1

string Availabe(int state)
{
string stateNum = "0";
for (int compartmentsI = 1; compartmentsI <= 10; compartmentsI++)
{
for (int seatsI = 1; seatsI <= 6; seatsI++)
{
string str = " " + to_string(compartmentsI) + ", " + to_string(seatsI);
if (!HaveTicket(str))
{
if (state == 0)
{
stateNum = to_string(compartmentsI);
break;
}
if (state == 1)
{
stateNum = to_string(seatsI);
break;
}
}
}
if (stateNum != "0")
{
break;
}
}
return stateNum;
}

Input: 3

Input: 3. Check Ticket

RailwayCli/main.cpp

Lines 311 to 324 in 021ebb1

case e3:
{
string fullName = GiveFullName();
if (HaveTicket(fullName))
{
cout << "\n-- Passenger Have Ticket Already! --" << endl;
ShowTicket(fullName);
}
else
{
cout << "\nSorry, Passenger don't have Ticket\n\n\n";
}
break;
}

string GiveFullName()

RailwayCli/main.cpp

Lines 122 to 138 in 021ebb1

string GiveFullName()
{
cout << endl;
string str;
string name;
string family;
cin.ignore();
cout << "Enter Passenger Name: ";
cin >> name;
transform(name.begin(), name.end(), name.begin(), ::tolower);
cout << "Enter Passenger Family: ";
cin >> family;
transform(family.begin(), family.end(), family.begin(), ::tolower);
return str = name + ", " + family;
}

HaveTicket() and ShowTicket() on #5

Input: 1

Task 1. Reserve a Seat

RailwayCli/main.cpp

Lines 301 to 305 in 021ebb1

case e1:
{
AddSeat();
break;
}

so, we need to reserve a new seat between 10 compartments & 6 Seats on every of them

RailwayCli/main.cpp

Lines 140 to 181 in 021ebb1

void AddSeat()
{
cout << endl;
Person person;
ofstream File("Train.txt", ios::app);
ifstream InFile("Train.txt");
cin.ignore();
cout << "Enter Passenger name: ";
getline(cin, person.name);
transform(person.name.begin(), person.name.end(), person.name.begin(), ::tolower);
cout << "Enter Passenger family: ";
getline(cin, person.family);
transform(person.family.begin(), person.family.end(), person.family.begin(), ::tolower);
string fullName = person.name + ", " + person.family;
if (HaveTicket(fullName))
{
cout << "\n-- Passenger Have Ticket Already! --" << endl;
ShowTicket(fullName);
}
else
{
if (Availabe(0) != "0")
{
person.compartment = Availabe(0);
person.seat = Availabe(1);
File << person.name << ", " << person.family << ", " << person.compartment << ", " << person.seat << endl;
File.close();
cout << "\n-- Ticket Added Successfully! --" << endl;
ShowTicket(fullName);
}
else
{
cout << "\n-- Train Was Full! --\n\n\n";
}
}
}

i create an object of Person for working with input data
first we most initial an object from ifstream and ofstream on the fstream Library

give person.name & person.family from client and make those ::tolower

put name and family together and make new string fullName
maybe you question why we do that??
for better query on database Train.txt
cause data save like this:

sara, irani, 9, 6 
ehsan , reziea, 10, 1

when we gonna know about this name is record on database or not
we must use this format <person.name>, <person.family> for search

then pass fullName to bool HaveTicket(string searchContent) function

RailwayCli/main.cpp

Lines 16 to 34 in 021ebb1

bool HaveTicket(string searchContent)
{
ifstream inFile("Train.txt");
string line;
bool state = false;
while (getline(inFile, line))
{
if (line.find(searchContent) != string::npos)
{
state = true;
return true;
}
}
if (!state)
{
return false;
}
}

that a bool function, so, return true false value
if data base have same name and family call another method whose name void ShowTicket(string personFullName)

RailwayCli/main.cpp

Lines 82 to 120 in 021ebb1

void ShowTicket(string personFullName)
{
ifstream inFile("Train.txt");
string line;
while (getline(inFile, line))
{
if (line.find(personFullName) != string::npos)
{
cout << "---------TICKET----------" << endl;
int countComma = 0;
cout << "Name: ";
for (int i = 0; i < line.size(); i++)
{
if (line[i] != ',' && line[i] != ' ')
{
cout << line[i];
}
if (line[i] == ',')
{
countComma++;
cout << endl;
if (countComma == 1)
{
cout << "Family: ";
}
if (countComma == 2)
{
cout << "Compartment: ";
}
if (countComma == 3)
{
cout << "Seat: ";
}
}
}
cout << "\n-------------------------\n\n\n";
}
}
}

that output something like this

-- Ticket Added Successfully! --
---------TICKET----------
Name: sara
Family: irani
Compartment: 1
Seat: 1
-------------------------

give different name, family on reserve compartment

  • when reserve a compartment give 6 name and family for 6 seats on compartment when available

    RailwayCli/main.cpp

    Lines 183 to 226 in 021ebb1

    void AddCompartment()
    {
    cout << endl;
    Person person;
    ofstream File("Train.txt", ios::app);
    ifstream InFile("Train.txt");
    cin.ignore();
    cout << "Enter Passenger name: ";
    getline(cin, person.name);
    transform(person.name.begin(), person.name.end(), person.name.begin(), ::tolower);
    cout << "Enter Passenger family: ";
    getline(cin, person.family);
    transform(person.family.begin(), person.family.end(), person.family.begin(), ::tolower);
    string fullName = person.name + ", " + person.family;
    if (HaveTicket(fullName))
    {
    cout << "\n-- Passenger Have Ticket Already! --" << endl;
    ShowTicket(fullName);
    }
    else
    {
    if (AvailabeCompartment() != "0")
    {
    person.compartment = AvailabeCompartment();
    for (int seatsI = 0; seatsI < 6; seatsI++)
    {
    person.seat = Availabe(1);
    File << person.name << ", " << person.family << ", " << person.compartment << ", " << person.seat << endl;
    }
    File.close();
    cout << "\n-- Tickets Added Successfully! --" << endl;
    ShowTicket(fullName);
    }
    else
    {
    cout << "\n-- Don't Have Empty Compartment! --\n\n\n";
    }
    }
    }

Input: 4

Input: 4. Ticket Refund

RailwayCli/main.cpp

Lines 325 to 330 in 021ebb1

case e4:
{
string fullName = GiveFullName();
RefundTicket(fullName);
break;
}

GiveFullName on #13

void RefundTicket(string fullName)

RailwayCli/main.cpp

Lines 228 to 261 in 021ebb1

void RefundTicket(string fullName)
{
string line;
string newFile = "";
bool state = false;
ifstream InFile("Train.txt");
while (getline(InFile, line))
{
if (line.find(fullName) == string::npos)
{
newFile += line + '\n';
}
else
{
ShowTicket(fullName);
state = true;
}
}
InFile.close();
ofstream File("Train.txt");
File << newFile;
File.close();
if (state)
{
cout << "-- Ticket Refunded Successfully!--\n\n\n";
}
else
{
cout << "\nTicket not found!!\n\n\n";
}
}

ShowTicket() on #11

output 01

โฏ .\a.exe
---- RAILWAY Cli ----

1. Reserve a Seat
2. Reserve a Compartment
3. Check Ticket
4. Ticket Refund
5. Exit
Please Choose Number: 2

Enter Passenger name: ali
Enter Passenger family: koleiny

-- Tickets Added Successfully! --
---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 1
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 2
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 3
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 4
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 5
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 6
-------------------------


1. Reserve a Seat
2. Reserve a Compartment
3. Check Ticket
4. Ticket Refund
5. Exit
Please Choose Number: 4

Enter Passenger Name: ali
Enter Passenger Family: koleiny
---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 1
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 2
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 3
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 4
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 5
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 6
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 1
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 2
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 3
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 4
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 5
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 6
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 1
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 2
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 3
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 4
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 5
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 6
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 1
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 2
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 3
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 4
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 5
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 6
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 1
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 2
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 3
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 4
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 5
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 6
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 1
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 2
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 3
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 4
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 5
-------------------------


---------TICKET----------
Name: ali
Family: koleiny
Compartment: 1
Seat: 6
-------------------------


-- Ticket Refunded Successfully!--


1. Reserve a Seat
2. Reserve a Compartment
3. Check Ticket
4. Ticket Refund
5. Exit
Please Choose Number:

main

main function

RailwayCli/main.cpp

Lines 283 to 345 in 021ebb1

int main()
{
cout << "---- RAILWAY Cli ----\n\n";
bool state = true;
string num;
while(state)
{
cout << "1. Reserve a Seat" << endl;
cout << "2. Reserve a Compartment" << endl;
cout << "3. Check Ticket" << endl;
cout << "4. Ticket Refund" << endl;
cout << "5. Exit" << endl;
cout << "Please Choose Number: ";
cin >> num;
switch(HashIt(num))
{
case e1:
{
AddSeat();
break;
}
case e2:
{
AddCompartment();
break;
}
case e3:
{
string fullName = GiveFullName();
if (HaveTicket(fullName))
{
cout << "\n-- Passenger Have Ticket Already! --" << endl;
ShowTicket(fullName);
}
else
{
cout << "\nSorry, Passenger don't have Ticket\n\n\n";
}
break;
}
case e4:
{
string fullName = GiveFullName();
RefundTicket(fullName);
break;
}
case e5:
{
cout << "\n-- GOOD LUCK --";
state = false;
break;
}
case other:
{
cout << "\n-- Invalid Number! Please Try again --\n\n\n";
break;
}
}
}
return 0;
}

in switch case statement, don't worry about e1, e2 ,e3, ... and so on.
when we use normally 1, 2, 3, ...
and client is type like 53 get stuck at a infinite loop and crash
even though we use default: key word on last case,
so, we need two use something different for this issue
use enum!
and translate elements to numbers like this:

RailwayCli/main.cpp

Lines 263 to 281 in 021ebb1

enum string_code
{
e1,
e2,
e3,
e4,
e5,
other
};
string_code HashIt (string const& inString)
{
if (inString == "1") return e1;
if (inString == "2") return e2;
if (inString == "3") return e3;
if (inString == "4") return e4;
if (inString == "5") return e5;
else return other;
}

Input: 1

Input 1. Reserve a Seat

RailwayCli/main.cpp

Lines 301 to 305 in 021ebb1

case e1:
{
AddSeat();
break;
}

so, we need to reserve a new seat between 10 compartments & 6 Seats on every of them

RailwayCli/main.cpp

Lines 140 to 181 in 021ebb1

void AddSeat()
{
cout << endl;
Person person;
ofstream File("Train.txt", ios::app);
ifstream InFile("Train.txt");
cin.ignore();
cout << "Enter Passenger name: ";
getline(cin, person.name);
transform(person.name.begin(), person.name.end(), person.name.begin(), ::tolower);
cout << "Enter Passenger family: ";
getline(cin, person.family);
transform(person.family.begin(), person.family.end(), person.family.begin(), ::tolower);
string fullName = person.name + ", " + person.family;
if (HaveTicket(fullName))
{
cout << "\n-- Passenger Have Ticket Already! --" << endl;
ShowTicket(fullName);
}
else
{
if (Availabe(0) != "0")
{
person.compartment = Availabe(0);
person.seat = Availabe(1);
File << person.name << ", " << person.family << ", " << person.compartment << ", " << person.seat << endl;
File.close();
cout << "\n-- Ticket Added Successfully! --" << endl;
ShowTicket(fullName);
}
else
{
cout << "\n-- Train Was Full! --\n\n\n";
}
}
}

i create an object of Person for working with input data
first we most initial an object from ifstream and ofstream on the fstream Library

give person.name & person.family from client and make those ::tolower

put name and family together and make new string fullName
maybe you question why we do that??
for better query on database Train.txt
cause data save like this:

sara, irani, 9, 6 
ehsan , reziea, 10, 1

when we gonna know about this name is record on database or not
we must use this format <person.name>, <person.family> for search

then pass fullName to bool HaveTicket(string searchContent) function

RailwayCli/main.cpp

Lines 16 to 34 in 021ebb1

bool HaveTicket(string searchContent)
{
ifstream inFile("Train.txt");
string line;
bool state = false;
while (getline(inFile, line))
{
if (line.find(searchContent) != string::npos)
{
state = true;
return true;
}
}
if (!state)
{
return false;
}
}

that a bool function, so, return true false value
if data base have same name and family call another method whose name void ShowTicket(string personFullName)

RailwayCli/main.cpp

Lines 82 to 120 in 021ebb1

void ShowTicket(string personFullName)
{
ifstream inFile("Train.txt");
string line;
while (getline(inFile, line))
{
if (line.find(personFullName) != string::npos)
{
cout << "---------TICKET----------" << endl;
int countComma = 0;
cout << "Name: ";
for (int i = 0; i < line.size(); i++)
{
if (line[i] != ',' && line[i] != ' ')
{
cout << line[i];
}
if (line[i] == ',')
{
countComma++;
cout << endl;
if (countComma == 1)
{
cout << "Family: ";
}
if (countComma == 2)
{
cout << "Compartment: ";
}
if (countComma == 3)
{
cout << "Seat: ";
}
}
}
cout << "\n-------------------------\n\n\n";
}
}
}

that output something like this

-- Ticket Added Successfully! --
---------TICKET----------
Name: sara
Family: irani
Compartment: 9
Seat: 6
-------------------------

show different data when check a compartmant

  • when check ticket if passenger reserve a compartments show different data

    RailwayCli/main.cpp

    Lines 16 to 34 in 021ebb1

    bool HaveTicket(string searchContent)
    {
    ifstream inFile("Train.txt");
    string line;
    bool state = false;
    while (getline(inFile, line))
    {
    if (line.find(searchContent) != string::npos)
    {
    state = true;
    return true;
    }
    }
    if (!state)
    {
    return false;
    }
    }

    RailwayCli/main.cpp

    Lines 82 to 120 in 021ebb1

    void ShowTicket(string personFullName)
    {
    ifstream inFile("Train.txt");
    string line;
    while (getline(inFile, line))
    {
    if (line.find(personFullName) != string::npos)
    {
    cout << "---------TICKET----------" << endl;
    int countComma = 0;
    cout << "Name: ";
    for (int i = 0; i < line.size(); i++)
    {
    if (line[i] != ',' && line[i] != ' ')
    {
    cout << line[i];
    }
    if (line[i] == ',')
    {
    countComma++;
    cout << endl;
    if (countComma == 1)
    {
    cout << "Family: ";
    }
    if (countComma == 2)
    {
    cout << "Compartment: ";
    }
    if (countComma == 3)
    {
    cout << "Seat: ";
    }
    }
    }
    cout << "\n-------------------------\n\n\n";
    }
    }
    }

show ticket when refund

  • when refund a ticket show data of tickets was refunded

    RailwayCli/main.cpp

    Lines 228 to 261 in 021ebb1

    void RefundTicket(string fullName)
    {
    string line;
    string newFile = "";
    bool state = false;
    ifstream InFile("Train.txt");
    while (getline(InFile, line))
    {
    if (line.find(fullName) == string::npos)
    {
    newFile += line + '\n';
    }
    else
    {
    ShowTicket(fullName);
    state = true;
    }
    }
    InFile.close();
    ofstream File("Train.txt");
    File << newFile;
    File.close();
    if (state)
    {
    cout << "-- Ticket Refunded Successfully!--\n\n\n";
    }
    else
    {
    cout << "\nTicket not found!!\n\n\n";
    }
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.