oldPropertyDatabase.cpp
上传用户:jed0755
上传日期:2022-02-21
资源大小:13k
文件大小:5k
- // This is a property database that reads properties from a text file
// containing the property listings. The property listing can be displayed and searched
// New properties can be added and the new listing saved to a file
// Author: Colin Lemmon - 19/4/05
// Last modified: Lindsay Ward - 29/08/08
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MAX_PROPERTIES = 50;
struct property
{
- string address;
string suburb;
int price;
};
int readFile(property properties[]);
int menu();
void listAll(property properties[], int items);
void search(property properties[], int items);
void addProperty(property properties[], int &items);
void saveProperties(property properties[], int items);
void displayProperty(property oneProperty);
int main()
{
int option = 0;
int items = 0;
property properties[MAX_PROPERTIES];
items = readFile(properties);
do
{
option = menu();
switch (option)
{
case 1:
listAll(properties, items);
break;
case 2:
search(properties, items);
break;
case 3:
addProperty(properties, items);
break;
case 4:
saveProperties(properties, items);
break;
case 5:
break;
default:
cout << "Illegal menu option" << endl;
}
} while (option != 5);
return 0;
}
//
// Read the property listing file
//
int readFile(property properties[])
{
string filename;
string remainder;
int items = 0;
cout << "Enter property filename: ";
cin >> filename;
getline(cin, remainder);
ifstream fin(filename.c_str());
if (!fin.is_open())
{
cout << "File ""<< filename << "" does not exist" << endl << endl;
cout << "No properties in database." << endl;
return 0;
}
for (int i = 0; i < MAX_PROPERTIES && fin.peek()!= -1; i++)
{
getline(fin, properties[i].address);
getline(fin, properties[i].suburb);
fin >> properties[i].price;
getline(fin, remainder);
items++;
}
cout << endl << "Read " << items << " properties to database." << endl;
fin.close();
return items;
}
//
// Display the menu
// Returns the user selction
//
int menu()
{
int option;
cout << endl;
cout << "Menu:" << endl;
cout << "1 - List all properties" << endl;
cout << "2 - Property search" << endl;
cout << "3 - Add a property" << endl;
cout << "4 - Save property list" << endl;
cout << "5 - Exit" << endl << endl;
cout << "Please enter option: ";
cin >> option;
string remainder;
getline(cin, remainder);
cout << endl;
return option;
}
//
// Display all properties in the current listing
//
void listAll(property properties[], int items)
{
if (items == 0)
{
cout << "No properties in database." << endl;
return;
}
for (int i = 0; i < items; i++)
{
displayProperty(properties[i]);
}
}
//
// Search for properties up to a maximum price
//
void search(property properties[], int items)
{
int limit;
if (items == 0)
{
cout << "No properties in database." << endl;
return;
}
cout << "Please enter price limit: $";
cin >> limit;
cout << endl;
for (int i = 0; i < items; i++)
{
if (properties[i].price <= limit)
{
displayProperty(properties[i]);
}
}
}
//
// Add a property to the current listings without saving to file
//
void addProperty(property properties[], int &items)
{
if (items == MAX_PROPERTIES)
{
cout << "Maximum number of properties reached." << endl;
return;
}
- cout << "Enter address: ";
- getline(cin, properties[items].address);
- while (properties[items].address == "")
{
cout << "Invalid address!" << endl;
cout << "Enter address: ";
getline(cin, properties[items].address);
}
- cout << "Enter suburb: ";
- getline(cin, properties[items].suburb);
-
cout << "Enter price: $";
cin >> properties[items].price;
cout << endl << "Property: " << endl;
displayProperty(properties[items]);
cout << "added to list" << endl;
items++;
}
//
// Save the current property listing to a file
//
void saveProperties(property properties[], int items)
{
string filename;
cout << "Enter filename: ";
cin >> filename;
ofstream fout(filename.c_str());
if (!fout.is_open())
{
cout << "File ""<< filename << "" does not exist" << endl << endl;
return;
}
for (int i = 0; i < items; i++)
{
fout << properties[i].address << endl;
fout << properties[i].suburb << endl;
fout << properties[i].price << endl;
}
cout << "Property details saved to file "" << filename << """ << endl;
fout.close();
}
//
// Display a single property
//
void displayProperty(property oneProperty)
{
cout << "$" << oneProperty.price << " - ";
cout << oneProperty.address << ", ";
cout << oneProperty.suburb << endl;
}