1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
#include <string>
#include <iostream>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <limits>
#include <cctype>
const std::string tnam[] {"apartment", "terrace", "bungalow", "factory", "mall"};
const std::string ttyp[] {"commercial", "residential"};
const std::string totain[] {"sale", "rent"};
const auto mx {std::max({std::size(tnam), std::size(ttyp), std::size(totain)})};
const size_t MaxProp {10};
using PropSum = unsigned[std::size(tnam)][std::size(ttyp)];
std::string tolower(const std::string& txt)
{
std::string low;
low.reserve(txt.size());
for (const auto& ch : txt)
low += static_cast<char> (std::tolower(static_cast<unsigned char>(ch)));
return low;
}
std::string tocap(const std::string& txt)
{
auto lc {tolower(txt)};
if (!lc.empty())
lc[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(lc[0])));
return lc;
}
class Property
{
private:
size_t itype {}; // Index into ttype
size_t iname {}; // Index into tnam
size_t iobtain {}; // Index into totain
float price {};
bool avail {};
public:
Property() {}
Property(const std::string& proptype, float propprice, const std::string& propname,
const std::string& proprecordtype, bool av = false) {
set_details(proptype, propprice, propname, proprecordtype, av);
}
bool set_details(const std::string& proptype, float propprice, const std::string& propname,
const std::string& propobtain, bool av = false) {
price = propprice;
avail = av;
itype = iobtain = iname = 0;
if (const auto fd {std::find(std::begin(ttyp), std::end(ttyp), tolower(proptype))}; fd != std::end(ttyp))
itype = fd - std::begin(ttyp) + 1;
if (const auto fd {std::find(std::begin(totain), std::end(totain), tolower(propobtain))}; fd != std::end(totain))
iobtain = fd - std::begin(totain) + 1;
if (const auto fd {std::find(std::begin(tnam), std::end(tnam), tolower(propname))}; fd != std::end(tnam))
iname = fd - std::begin(tnam) + 1;
else
iname = itype = iobtain = 0;
return iname != 0;
}
size_t getiname() const { return iname; }
size_t getitype() const { return itype; }
friend void displayProps(const Property props[], size_t noProp);
};
template<typename T = int>
auto getNum(const std::string& prm)
{
const auto notsp {[&]() {while (std::isspace(static_cast<unsigned char>(std::cin.peek())) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; }};
T n {};
while ((std::cout << prm) && (!(std::cin >> n) || notsp())) {
std::cout << "Not a number\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return n;
}
Property getProp() {
size_t nam {}, typ {}, obt {};
std::cout << "\n " << std::left << std::setw(24) << "Property name" << std::setw(24) << "Property type" << "Obtain\n";
for (size_t t = 0; t < mx; ++t) {
if (t < std::size(tnam))
std::cout << std::left << std::setw(4) << t + 1 << std::setw(20) << tocap(tnam[t]);
else
std::cout << std::setw(24) << " ";
if (t < std::size(ttyp))
std::cout << std::left << std::setw(4) << t + 1 << std::setw(20) << tocap(ttyp[t]);
else
std::cout << std::setw(24) << " ";
if (t < std::size(totain))
std::cout << std::left << std::setw(4) << t + 1 << tocap(totain[t]);
std::cout << '\n';
}
std::cout << '\n';
do {
nam = getNum<unsigned>("Enter property name number (0 to exit): ");
} while (nam > std::size(tnam) && (std::cout << "Invalid name no\n"));
if (nam) {
do {
typ = getNum<unsigned>("Enter property type number: ");
} while ((typ < 1 || typ > std::size(ttyp)) && std::cout << "Invalid type no\n");
const auto price {getNum<float>("Enter price: ")};
do {
obt = getNum<unsigned>("Enter property obtainability number: ");
} while ((obt < 1 || obt > std::size(totain)) && std::cout << "Invalid obtain no\n");
const bool avail = getNum<unsigned>("Is available (1 - yes, 0 - no: ");
return {ttyp[typ - 1], price, tnam[nam - 1], totain[obt -1], avail};
}
return {Property {}};
}
void showProps(const PropSum cnts)
{
std::cout << '\n' << std::setw(13) << " ";
for (const auto& t22 : ttyp)
std::cout << std::left << std::setw(13) << tocap(t22);
std::cout << '\n';
for (unsigned t11 = 0; t11 < std::size(tnam); ++t11) {
std::cout << std::setw(17) << tocap(tnam[t11]);
for (unsigned t22 = 0; t22 < std::size(ttyp); ++t22)
std::cout << std::setw(13) << cnts[t11][t22];
std::cout << '\n';
}
}
void displayProps(const Property props[], size_t noProp)
{
std::cout << '\n' << std::left << std::setw(17) << "Property Name" << std::setw(16) << "Property Type"
<< std::setw(7) << "Price" << std::setw(7) << "Obtain" << " Avail\n";
std::cout << std::setw(53) << std::setfill('-') << "-" << std::setfill(' ') << '\n';
for (size_t i = 0; i < noProp; ++i) {
const auto& p {props[i]};
if (p.itype)
std::cout << std::left << std::setw(17) << tocap(tnam[p.iname - 1]) << std::setw(14) << tocap(ttyp[p.itype - 1])
<< std::setw(7) << std::right << p.price << " "
<< std::setw(7) << std::left << tocap(totain[p.iobtain - 1]) << std::boolalpha << p.avail << '\n';
}
}
int main()
{
unsigned opt {};
Property data[MaxProp] {};
data[0].set_details("Residential", 100, "Terrace", "Sale", 1);
data[1].set_details("Residential", 100, "Apartment", "Rent");
data[2].set_details("Residential", 900, "Bungalow", "Sale");
data[3].set_details("Commercial", 500, "Factory", "Rent");
data[4].set_details("Commercial", 1200, "Mall", "Sale");
for (size_t i = 0; i < 5; ++i)
if (data[i].getiname() == 0) {
std::cout << "Error adding details\n";
return 1;
}
size_t noProp {5};
do {
std::cout << "\n Property menu\n\n"
<< "1. Add property\n"
<< "2. Display property summary by name/type\n"
<< "3. Display all properties\n"
<< "0. Quit\n\n";
switch (opt = getNum<unsigned>("Enter option: ")) {
case 1:
{
Property p;
do {
p = getProp();
if (p.getiname())
data[noProp++] = p;
} while (p.getiname() && noProp < MaxProp);
}
break;
case 2:
{
PropSum cnts {};
for (size_t p = 0; p < noProp; ++p)
++cnts[data[p].getiname() - 1][data[p].getitype() - 1];
showProps(cnts);
}
break;
case 3:
displayProps(data, noProp);
break;
case 0:
break;
default:
std::cout << "Invalid option\n";
break;
}
} while (opt);
}
|