Skip to content
This repository has been archived by the owner on Feb 16, 2019. It is now read-only.

Commit

Permalink
add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
hadisfr committed Jul 10, 2018
0 parents commit aeeb6e4
Show file tree
Hide file tree
Showing 15 changed files with 706 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
###C++###

# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app


###OSX###

.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear on external disk
.Spotlight-V100
.Trashes

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
56 changes: 56 additions & 0 deletions customer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "customer.h"
#include <string>
#include <vector>

using namespace std;

Customer::Customer(string _name, long _id, string _location): name(_name), id(_id), location(_location){}

long Customer::getId() const{
return id;
}

string Customer::getName() const{
return name;
}

string Customer::getLocation() const{
return location;
}

string Customer::getLastBill() const{
if(!orders.size())
return "\n";
return getName() + string(" ") + to_string(getId()) + string("\n") + orders.back().getBill();
}

string Customer::getAllBills() const{
string result = getName() + string(" ") + to_string(getId()) + string("\n");
for(int i = 0; i < orders.size(); i++)
result += orders[i].getBill() + string("#\n");
result += string("total purchase ") + to_string(getTotalPurchase()) + string("\n");
return result;
}

void Customer::newOrder(){
orders.push_back(Order(this));
}

void Customer::addToOrder(Food *food, Restaurant *restaurant, int num, string personalizations){
if(!orders.size())
return;
orders.back().addToOrder(food, restaurant, num, personalizations);
}

string Customer::getOrderReport() const{
if(!orders.size())
return "\n";
return getName() + string(" ") + to_string(getId()) + string(" ") + to_string(orders.back().getTotalCost()) + string("\n");
}

int Customer::getTotalPurchase() const{
int sum = 0;
for(int i = 0; i < orders.size(); i++)
sum += orders[i].getTotalCost();
return sum;
}
27 changes: 27 additions & 0 deletions customer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef CUSTOMER_H
#define CUSTOMER_H

#include <vector>
#include <string>
#include "order.h"

class Customer{
private:
std::string name;
long id;
std::vector <Order> orders;
std::string location;
public:
Customer(std::string name, long id, std::string location);
long getId() const;
std::string getName() const;
std::string getLocation() const;
std::string getLastBill() const;
std::string getAllBills() const;
void newOrder();
void addToOrder(Food *, Restaurant *, int num, std::string personalizations);
std::string getOrderReport() const;
int getTotalPurchase() const;
};

#endif
50 changes: 50 additions & 0 deletions food.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "food.h"
#include <string>

using namespace std;

Food::Food(int _id, string _name, Type _type, int _cost): id(_id), name(_name), type(_type), cost(_cost){}

string Food::getDetails() const{
return idToString(id) + " " + name + " " + typeToString(type) + " " + to_string(cost);
}

int Food::getCost() const{
return cost;
}

string Food::typeToString(Type type){
switch(type){
case Iranian:
return "Iranian";
case Eastern:
return "Eastern";
case European:
return "European";
default:
return "";
}
}

string Food::idToString(int id){
return (id >= 100 ? string("") : (string("0") + (id >= 10 ? string("") : string("0")))) + to_string(id);
}

int Food::getId() const{
return id;
}

Food::Type Food::stringToType(string str){
if(str == "Iranian")
return Iranian;
else if(str == "Eastern")
return Eastern;
else if(str == "European")
return European;
else
return All; // default
}

Food::Type Food::getType() const{
return type;
}
25 changes: 25 additions & 0 deletions food.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef FOOD_H
#define FOOD_H

#include <string>

class Food{
public:
enum Type{All, Iranian, Eastern, European};
private:
int id;
std::string name;
int cost;
Type type;
static std::string idToString(int);
public:
Food(int id, std::string name, Type, int cost);
std::string getDetails() const;
int getCost() const;
static std::string typeToString(Type);
static Type stringToType(std::string);
int getId() const;
Type getType() const;
};

#endif
99 changes: 99 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "system.h"
#include "util.h"

#define NEW_SUPER -1
#define ERRSTR "ERR"

using namespace std;

void readDatabase(System &system, string customersFileName, string restaurantsFileName){
string line;
ifstream restaurantsFileStr(restaurantsFileName, ifstream::in);
int restaurantId = NEW_SUPER;
while(getline(restaurantsFileStr, line)){
if(line == ""){
restaurantId = NEW_SUPER;
}
else{
vector <string> inputParameters = parseString(line, ',');
if(restaurantId == NEW_SUPER){
restaurantId = atoi(inputParameters[1].c_str());
system.addRestaurant(inputParameters[0], restaurantId, inputParameters[2]);
}
else{
system.addFood(restaurantId, atoi(inputParameters[0].c_str()), inputParameters[1], Food::stringToType(inputParameters[2]), atoi(inputParameters[3].c_str()));
}
}
}
restaurantsFileStr.close();

ifstream customersFileStr(customersFileName, ifstream::in);
while(getline(customersFileStr, line)){
while(getline(customersFileStr, line)){
vector <string> inputParameters = parseString(line, ',');
system.addCustomer(inputParameters[0], atol(inputParameters[1].c_str()), inputParameters.size() > 2 ? inputParameters[2] : "");
}
}
customersFileStr.close();
}

void systemIo(istream &istr, ostream &ostr, System &system){
string line;
while(getline(istr, line)){
vector <string> inputParameters = parseString(line, ' ');
if(inputParameters[0] == "menu"){
ostr << system.getMenu(atoi(inputParameters[1].c_str()));
}
else if(inputParameters[0] == "restaurants"){
ostr << system.getRestaurantsDetail(inputParameters.size() > 1 ? atol(inputParameters[2].c_str()) : 0);
}
else if(inputParameters[0] == "list"){
if(inputParameters[1] == "near"){
ostr << system.listFoods(atol(inputParameters[2].c_str()));
}
else if(inputParameters[1] == "type"){
ostr << system.listFoods(Food::stringToType(inputParameters[2]));
}
else ostr << ERRSTR << endl;
}
else if(inputParameters[0] == "order"){
long customerId = atol(inputParameters[1].c_str());
system.newOrder(customerId);
while(getline(istr, line) && line != "$"){
inputParameters = parseString(line, ' ');
string personalizations = "";
for(int i = 2; i < inputParameters.size(); i++){
if(i > 2)
personalizations += string(" ");
personalizations += inputParameters[i];
}
system.addToOrder(customerId, atol(inputParameters[0].c_str()), atoi(inputParameters[1].c_str()), personalizations);
}
ostr << system.getOrderReport(customerId);
}
else if(inputParameters[0] == "bill"){
if(inputParameters[1] == "all"){
ostr << system.getAllBills(atol(inputParameters[2].c_str()));
}
else{
ostr << system.getLastBill(atol(inputParameters[1].c_str()));
}
}
else
ostr << ERRSTR << endl;
// ostr << endl;
}
}

int main(){
System system;

readDatabase(system, "customers.txt", "restaurants.txt");
systemIo(cin, cout, system);

return 0;
}
33 changes: 33 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CC=g++
CF=
CCF=-c

a.out: main.o customer.o food.o restaurant.o util.o order.o system.o
$(CC) $(CF) main.o customer.o food.o restaurant.o util.o order.o system.o -o a.out;

main.o: main.cpp customer.h food.h restaurant.h util.h order.h system.h
$(CC) $(CF) $(CCF) main.cpp

customer.o: customer.cpp customer.h util.h order.h
$(CC) $(CF) $(CCF) customer.cpp

food.o: food.cpp food.h util.h
$(CC) $(CF) $(CCF) food.cpp

restaurant.o: restaurant.cpp restaurant.h food.h util.h food.h
$(CC) $(CF) $(CCF) restaurant.cpp

order.o: order.cpp order.h util.h customer.h
$(CC) $(CF) $(CCF) order.cpp

system.o: system.cpp system.h util.h customer.h restaurant.h
$(CC) $(CF) $(CCF) system.cpp

util.o: util.cpp util.h
$(CC) $(CF) $(CCF) util.cpp

test: a.out
./a.out

clean:
rm .DS_Store *.o a.out
Loading

0 comments on commit aeeb6e4

Please sign in to comment.