Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

HOPE UNIVERSITY COLLEGE

INFORMATICS FACULTY

DEPARTMENT OF COMPUTER SCIENCE


COURSE TITLE: Basic Programming 2

Assignment on Broker Sysytem


Section: 2
Prepared By: Id No:

1. Amanuel Berihun RCS/017/22


2. Mikyas Abebe RCS/096/22
3. Nathaniel Tekalign RCS/105/22
4. Samuel Italo RCS/123/22
5. Tadiyos Amare RCS/134/22

Submitted to: Belilign


Date: 01 / 06 /2023 G.C.
Final File-Incorporated Deliverable
#include <iostream>
#include <string>
#include <fstream> // include the header file for file streams
using namespace std;

struct Car {
string make;
string model;
int year;
int price;
string engineSize;
string mileage;
string color;
string transmission;
string fuelType;
string contact;
};

struct Seller {
string name;
string phone;
Car cars[10]; // a fixed-size array of cars owned by the
seller
int numCars; // a variable to store the number of cars in the
array
};

struct Buyer {
string name;
string phone;
};

// a global array of sellers


Seller sellers[10];
// a global variable to store the number of sellers in the
array
int numSellers = 0;

// a global array of buyers


Buyer buyers[10];
// a global variable to store the number of buyers in the array
int numBuyers = 0;

// a function to display the details of a car


void displayCarDetails(Car car) {
cout << "Details of the car:" << endl;
cout << "Make: " << car.make << endl;
cout << "Model: " << car.model << endl;
cout << "Year: " << car.year << endl;
cout << "Price: $" << car.price << endl;
cout << "Engine Size: " << car.engineSize << endl;
cout << "Mileage: " << car.mileage << endl;
cout << "Color: " << car.color << endl;
cout << "Transmission: " << car.transmission << endl;
cout << "Fuel Type: " << car.fuelType << endl;
cout << "Owner's contact: " << car.contact << endl;
}

// a function to display all the cars in the system


void displayAllCars() {
cout<<" HOT SELLING CARS!!!!!"<<endl;
cout<<"| Index | Make |Model | Year | Price |\n"

<<"|-------|---------------|---------------|------|---------|\
n";
int index =1; // a counter for the index of each car
for (int i =0; i < numSellers; i++) { // loop through all the
sellers
for (int j =0; j < sellers[i].numCars; j++) { // loop
through all the cars of each seller
cout<<"| "<<index<<" "
<<"| "<<sellers[i].cars[j].make<<" "
<<"|"<<sellers[i].cars[j].model<<" "
<<"| "<<sellers[i].cars[j].year<<""
<<"|$"<<sellers[i].cars[j].price<<"|\n";
index++; // increment the index
}
}
cout<<"\
n|-------|---------------|---------------|------|---------|\n";
}

// a function to display the matching cars based on the user's


input
void displayMatchingCars(string make, string model,int minYear,
int maxPrice) {
bool foundMatch = false; // a flag to indicate if any match
is found
for (int i =0; i < numSellers; i++) { // loop through all the
sellers
for (int j =0; j < sellers[i].numCars; j++) { // loop
through all the cars of each seller
if (sellers[i].cars[j].make == make &&
sellers[i].cars[j].model == model && sellers[i].cars[j].year >=
minYear && sellers[i].cars[j].price <= maxPrice) { // check if
the car matches the criteria
cout<< sellers[i].cars[j].year<< " "<<
sellers[i].cars[j].make<< " "<< sellers[i].cars[j].model<< "
$"<< sellers[i].cars[j].price<< endl; // display the basic
information of the matching car
displayCarDetails(sellers[i].cars[j]); // display the
details of the matching car
foundMatch = true; // set the flag to true
}
}
}
if (!foundMatch) { // if no match is found
cout<< "No matching cars found."<< endl; // display a
message
}
}

// a function to register a new seller and add it to the global


array of sellers
void registerSeller() {
Seller newSeller; // create a new seller object
newSeller.numCars =0; // initialize the number of cars to
zero
cout<<"Enter your name: "; // prompt for name
getline(cin,newSeller.name); // read name from input
cout<<"Enter your phone number: "; // prompt for phone number
getline(cin,newSeller.phone); // read phone number from input

if (numSellers < 10) { // check if there is space in the


global array of sellers
sellers[numSellers] = newSeller; // add the new seller to
the global array of sellers
numSellers++; // increment the number of sellers
cout<<"You have successfully registered as a
seller."<<endl; // display a confirmation message
} else {
cout<<"Sorry, there is no more space for new
sellers."<<endl; // display an error message
}
}

// a function to register a new buyer and add it to the global


array of buyers
void registerBuyer() {
Buyer newBuyer; // create a new buyer object
cout<<"Enter your name: "; // prompt for name
getline(cin,newBuyer.name); // read name from input
cout<<"Enter your phone number: "; // prompt for phone number
getline(cin,newBuyer.phone); // read phone number from input

if (numBuyers < 10) {


buyers[numBuyers] = newBuyer; // add the new buyer to the
global array of buyers
numBuyers++; // increment the number of buyers
cout<<"You have successfully registered as a buyer."<<endl;
// display a confirmation message
} else {
cout<<"Sorry, there is no more space for new
buyers."<<endl; // display an error message
}
}

// a function to add a new car to a seller's list of cars


void addCar() {
string name, phone; // variables to store the name and phone
number of the seller
Car newCar; // create a new car object
bool foundSeller = false; // a flag to indicate if the seller
is found
int sellerIndex = -1; // a variable to store the index of the
seller in the global array of sellers

cout<<"Enter your name: "; // prompt for name


getline(cin,name); // read name from input
cout<<"Enter your phone number: "; // prompt for phone number
getline(cin,phone); // read phone number from input

for (int i =0; i < numSellers; i++) { // loop through all the
sellers
if (sellers[i].name == name && sellers[i].phone == phone) {
// check if the seller matches the input
foundSeller = true; // set the flag to true
sellerIndex = i; // store the index of the seller
break; // exit the loop
}
}

if (foundSeller) { // if the seller is found


cout<<"Enter make of your car: "; // prompt for make
getline(cin, newCar.make); // read make from input
cout<<"Enter model of your car: "; // prompt for model
getline(cin,newCar.model); // read model from input
cout<<"Enter year of your car: "; // prompt for year
cin>>newCar.year;
cin.ignore(); // read year from input and ignore newline
character
cout<<"Enter price of your car: "; // prompt for price
cin>>newCar.price;
cin.ignore(); // read price from input and ignore newline
character
cout<<"Enter engine size of your car: "; // prompt for
engine size
getline(cin, newCar.engineSize); // read engine size from
input
cout<<"Enter mileage of your car: "; // prompt for mileage
getline(cin, newCar.mileage); // read mileage from input
cout<<"Enter color of your car: "; // prompt for color
getline(cin, newCar.color); // read color from input
cout<<"Enter transmission type of your car: "; // prompt
for transmission type
getline(cin, newCar.transmission);
cout<<"Enter fuel type of your car: "; // prompt for fuel
type
getline(cin, newCar.fuelType); // read fuel type from input
newCar.contact = phone; // set the contact of the car to
the seller's phone number

if (sellers[sellerIndex].numCars < 10) { // check if there


is space in the seller's array of cars
sellers[sellerIndex].cars[sellers[sellerIndex].numCars] =
newCar; // add the new car to the seller's array of cars
sellers[sellerIndex].numCars++; // increment the number
of cars for the seller
cout<<"You have successfully added a new car."<<endl; //
display a confirmation message
} else {
cout<<"Sorry, you have reached the maximum number of cars
you can sell."<<endl; // display an error message
}
} else {
cout<<"Sorry, you are not registered as a seller."<<endl;
}
}
// a function to update an existing car in a seller's list of
cars
void updateCar() {
string name, phone;
int carIndex;
bool foundSeller = false;
int sellerIndex = -1;

cout<<"Enter your name: ";


getline(cin,name);
cout<<"Enter your phone number: ";
getline(cin,phone);

for (int i =0; i < numSellers; i++) {


if (sellers[i].name == name && sellers[i].phone == phone) {
foundSeller= true;
sellerIndex = i;
break;
}
}

if (foundSeller) {
cout<<"Enter the index of the car you want to update: ";
cin>>carIndex;
cin.ignore();

if (carIndex > 0 && carIndex <=


sellers[sellerIndex].numCars) {
carIndex--; // adjust the car index to match the array
index
cout<<"Enter new make of your car: "; // prompt for new
make
getline(cin,
sellers[sellerIndex].cars[carIndex].make); // read new make
from input and update the car
cout<<"Enter new model of your car: "; // prompt for new
model
getline(cin,
sellers[sellerIndex].cars[carIndex].model); // read new model
from input and update the car
cout<<"Enter new year of your car: "; // prompt for new
year
cin>>sellers[sellerIndex].cars[carIndex].year;
cin.ignore(); // read new year from input and update the car
cout<<"Enter new price of your car: "; // prompt for new
price
cin>>sellers[sellerIndex].cars[carIndex].price;
cin.ignore(); // read new price from input and update the car
cout<<"Enter new engine size of your car: "; // prompt
for new engine size
getline(cin,
sellers[sellerIndex].cars[carIndex].engineSize); // read new
engine size from input and update the car
cout<<"Enter new mileage of your car: "; // prompt for
new mileage
getline(cin,
sellers[sellerIndex].cars[carIndex].mileage); // read new
mileage from input and update the car
cout<<"Enter new color of your car:"; // prompt for new
color
getline(cin,
sellers[sellerIndex].cars[carIndex].color); // read new color
from input and update the car
cout<<"Enter new transmission type of your car: "; //
prompt for new transmission type
getline(cin,
sellers[sellerIndex].cars[carIndex].transmission); // read new
transmission type from input and update the car
cout<<"Enter new fuel type of your car: "; // prompt for
new fuel type

getline(cin,sellers[sellerIndex].cars[carIndex].fuelType); //
read new fuel type from input and update the car
cout<<"You have successfully updated your car."<<endl; //
display a confirmation message
} else {
cout<<"Invalid car index."<<endl; // display an error
message
}
} else {
cout<<"Sorry, you are not registered as a seller."<<endl;
}
}

// a function to remove an existing car from a seller's list of


cars
void removeCar() {
string name, phone;
int carIndex;
bool foundSeller = false;
int sellerIndex = -1;

cout<<"Enter your name: ";


getline(cin,name);
cout<<"Enter your phone number: ";
getline(cin,phone);

for (int i =0; i < numSellers; i++) {


if (sellers[i].name == name && sellers[i].phone == phone) {
foundSeller = true;
sellerIndex = i;
break;
}
}

if (foundSeller) {
cout<<"Enter the index of the car you want to remove: ";
cin>>carIndex;
cin.ignore();

if (carIndex > 0 && carIndex <=


sellers[sellerIndex].numCars) {
carIndex--;
for (int i = carIndex; i < sellers[sellerIndex].numCars -
1; i++) {
sellers[sellerIndex].cars[i] =
sellers[sellerIndex].cars[i+1];
}
sellers[sellerIndex].numCars--;
cout<<"You have successfully removed your car."<<endl;
} else {
cout<<"Invalid car index."<<endl;
}
} else {
cout<<"Sorry, you are not registered as a seller."<<endl;
}
}

// a function to search for cars based on the user's input


void searchCars() {
string make, model;
int minYear, maxPrice;
displayAllCars();
cout<<"\nEnter preferred make: ";
getline(cin,make);
cout<<"\nEnter preferred model: ";
getline(cin,model);
cout<<"\nEnter minimum year: ";
cin>>minYear;
cin.ignore();
cout<<"\nEnter maximum price: ";
cin>>maxPrice;
cin.ignore();

displayMatchingCars(make, model, minYear, maxPrice);


}

// a function to write data to files


void writeData() {

// create an output file stream for sellers


ofstream sellerFile;
// open the file "sellers.txt" in write mode
sellerFile.open("sellers.txt");
// check if the file is opened successfully
if (sellerFile.is_open()) {
// write the number of sellers to the file
sellerFile << numSellers << endl;
// loop through all the sellers
for (int i = 0; i < numSellers; i++) {
// write the name and phone number of each seller to the
file
sellerFile << sellers[i].name << endl;
sellerFile << sellers[i].phone << endl;
// write the number of cars of each seller to the file
sellerFile << sellers[i].numCars << endl;
// loop through all the cars of each seller
for (int j = 0; j < sellers[i].numCars; j++) {
// write the details of each car to the file
sellerFile << sellers[i].cars[j].make << endl;
sellerFile << sellers[i].cars[j].model << endl;
sellerFile << sellers[i].cars[j].year << endl;
sellerFile << sellers[i].cars[j].price << endl;
sellerFile << sellers[i].cars[j].engineSize << endl;
sellerFile << sellers[i].cars[j].mileage << endl;
sellerFile << sellers[i].cars[j].color << endl;
sellerFile << sellers[i].cars[j].transmission << endl;
sellerFile << sellers[i].cars[j].fuelType << endl;
sellerFile << sellers[i].cars[j].contact << endl;
}
}
// close the file
sellerFile.close();
} else {
// display an error message if the file cannot be opened
cout << "Unable to open file for writing." << endl;
}

// create an output file stream for buyers


ofstream buyerFile;
// open the file "buyers.txt" in write mode
buyerFile.open("buyers.txt");
// check if the file is opened successfully
if (buyerFile.is_open()) {
// write the number of buyers to the file
buyerFile << numBuyers << endl;
// loop through all the buyers
for (int i = 0; i < numBuyers; i++) {
// write the name and phone number of each buyer to the
file
buyerFile << buyers[i].name << endl;
buyerFile << buyers[i].phone << endl;
}
// close the file
buyerFile.close();
} else {
// display an error message if the file cannot be opened
cout << "Unable to open file for writing." << endl;
}
}

// a function to read data from files


void readData() {

// create an input file stream for sellers


ifstream sellerFile;
// open the file "sellers.txt" in read mode
sellerFile.open("sellers.txt");
// check if the file is opened successfully
if (sellerFile.is_open()) {
// read the number of sellers from the file
sellerFile >> numSellers;
sellerFile.ignore(); // ignore the newline character
// loop through all the sellers
for (int i = 0; i < numSellers; i++) {
// read the name and phone number of each seller from the
file
getline(sellerFile, sellers[i].name);
getline(sellerFile, sellers[i].phone);
// read the number of cars of each seller from the file
sellerFile >> sellers[i].numCars;
sellerFile.ignore(); // ignore the newline character
// loop through all the cars of each seller
for (int j = 0; j < sellers[i].numCars; j++) {
// read the details of each car from the file
getline(sellerFile, sellers[i].cars[j].make);
getline(sellerFile, sellers[i].cars[j].model);
sellerFile >> sellers[i].cars[j].year;
sellerFile >> sellers[i].cars[j].price;
sellerFile.ignore(); // ignore the newline character
getline(sellerFile, sellers[i].cars[j].engineSize);
getline(sellerFile, sellers[i].cars[j].mileage);
getline(sellerFile, sellers[i].cars[j].color);
getline(sellerFile, sellers[i].cars[j].transmission);
getline(sellerFile, sellers[i].cars[j].fuelType);
getline(sellerFile, sellers[i].cars[j].contact);
}
}
// close the file
sellerFile.close();
} else {
// display an error message if the file cannot be opened
cout << "Unable to open file for reading." << endl;
}

// create an input file stream for buyers


ifstream buyerFile;
// open the file "buyers.txt" in read mode
buyerFile.open("buyers.txt");
// check if the file is opened successfully
if (buyerFile.is_open()) {
// read the number of buyers from the file
buyerFile >> numBuyers;
buyerFile.ignore(); // ignore the newline character
// loop through all the buyers
for (int i = 0; i < numBuyers; i++) {
// read the name and phone number of each buyer from the
file
getline(buyerFile, buyers[i].name);
getline(buyerFile, buyers[i].phone);
}
// close the file
buyerFile.close();
} else {
// display an error message if the file cannot be opened
cout << "Unable to open file for reading." << endl;
}
}

// a function to display the menu and return the user's choice


int displayMenu() {
int choice;
cout<<"Welcome to the car selling and buying system!"<<endl;
cout<<"Please choose an option from the menu:"<<endl;
cout<<"1. Register as a seller"<<endl;
cout<<"2. Register as a buyer"<<endl;
cout<<"3. Add a car"<<endl;
cout<<"4. Update a car"<<endl;
cout<<"5. Remove a car"<<endl;
cout<<"6. Search for cars"<<endl;
cout<<"7. Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
cin.ignore();
return choice;
}

// the main function of the program


int main() {

int choice; // a variable to store the user's choice


bool exit = false; // a flag to indicate if the user wants to
exit
readData(); // read data from files
while (!exit) { // loop until the user wants to exit
choice = displayMenu(); // display the menu and get the
user's choice
switch (choice) { // execute different actions based on the
user's choice
case 1: // register as a seller
registerSeller();
break;
case 2: // register as a buyer
registerBuyer();
break;
case 3: // add a car
addCar();
break;
case 4: // update a car
updateCar();
break;
case 5: // remove a car
removeCar();
break;
case 6: // search for cars
searchCars();
break;
case 7: // exit
writeData(); // write data to files
cout << "Thank you for using the system. Goodbye!" <<
endl; // display a farewell message
exit = true; // set the flag to true
break;
default: // invalid choice
cout << "Invalid choice. Please try again." << endl; //
display an error message
}
}
return 0; // end the program
}

You might also like