Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- household.h
- /*************************************************
- This is a class describing a single household. It has private member
- variables for the name of the head of the household, the street address
- of the household, the number of people living in the household, and
- the household income. The functions allow access to and modification
- of those attributes either by the application programmer or by the
- user through the input functions.
- **********************************************************/
- #include<iostream>
- #include<string>
- #include<fstream>
- #ifndef HOUSEHOLD_H
- #define HOUSEHOLD_H
- class Household{
- public:
- Household();
- // Mutators
- void set_head(const std::string& hd);
- void set_address(const std::string& add);
- void set_occupants(int num);
- void set_income(int inc);
- // Accessors
- std::string get_head()const;
- std::string get_address()const;
- int get_occupants()const;
- int get_income() const;
- // Input and output
- void input(std::istream& ins);
- void output(std::ostream& outs)const;
- private:
- std::string house_head;
- std::string address;
- int occupants;
- int income;
- };
- //overloaded i/o operators
- std::istream& operator >>(std::istream& ins, Household& h);
- std::ostream& operator <<(std::ostream& outs, const Household& h);
- #endif
- /*
- household.cc
- This file contains the function descriptions that will allow
- for the user to read a text file of people with households into
- a census for sorting by name, total income, and the amount of
- people in the house.
- */
- #include "household.h"
- #include <string>
- #include <fstream>
- #include <iostream>
- using namespace std;
- Household::Household()
- {
- house_head = "unknown";
- address = "unknown";
- occupants = 0;
- income = 0;
- }
- void Household::set_head(const std::string& hd)
- {
- house_head = hd;
- }
- void Household::set_address(const std::string& add)
- {
- address = add;
- }
- void Household::set_occupants(int num)
- {
- occupants = num;
- }
- void Household::set_income(int inc)
- {
- income = inc;
- }
- string Household::get_head()const
- {
- return house_head;
- }
- string Household::get_address()const
- {
- return address;
- }
- int Household::get_occupants()const
- {
- return occupants;
- }
- int Household::get_income() const
- {
- return income;
- }
- void Household::input(std::istream& ins)
- {
- while(ins.peek() == '\n')
- {
- ins.ignore();
- }
- getline(ins, house_head);
- getline(ins, address);
- ins >> occupants;
- ins >> income;
- }
- void Household::output(std::ostream& outs) const
- {
- outs<<house_head;
- outs<<"\n";
- outs<<address;
- outs<<"\n";
- outs<<occupants;
- outs<<"\n";
- outs<<income;
- outs<<"\n";
- }
- istream& operator >>(std::istream& ins, Household& h)
- {
- h.input(ins);
- return ins;
- }
- ostream& operator <<(std::ostream& outs, const Household& h)
- {
- h.output(outs);
- return outs;
- }
- /*
- ward.cc
- This is the definition holder for the
- ward class, which contains and
- manipulates all the households
- */
- #include<iostream>
- #include<string>
- #include<fstream>
- #include<string>
- #include<cctype>
- #include"ward.h"
- #include"household.h"
- using namespace std;
- Ward::Ward()
- {
- used = 0;
- }
- void Ward::load(istream& ins)
- {
- ins>>data[used];
- used++;
- while(ins>>data[used])
- {
- cout<< "used = " << used << endl;
- used++;
- }
- }
- void Ward::add(Household& tmp)
- {
- if(used < SIZE)
- {
- data[used] = tmp;
- used++;
- }
- else
- }
- void Ward::display(std::ostream& cout)
- {
- for(size_t i = 0; i<used; i++)
- {
- cout << data[i];
- }
- }
- Household Ward::find(string name)
- {
- string target = lowerCase(name);
- Household error;
- for(size_t i = 0; i<used; i++)
- {
- string search = data[i].get_head();
- string lowSearch = lowerCase(search);
- if (target.compare(lowSearch) == 0)
- {
- return data[i];
- }
- else
- return error;
- }
- }
- void Ward::remove(string name)
- {
- string target = lowerCase(name);
- Household deleted;
- for(size_t i = 0; i<used; i++)
- {
- string search = data[i].get_head();
- string lowSearch = lowerCase(search);
- if (target.compare(lowSearch) == 0)
- {
- data[i] = deleted;
- }
- }
- }
- string Ward::lowerCase(string targ)
- {
- char t;
- int i = 0;
- while(targ[i])
- {
- t = targ[i];
- putchar(tolower(t));
- i++;
- }
- cout << "lowerCase working";
- return targ;
- }
- void Ward::display_richest()
- {
- Household highInc = data[0];
- for(size_t i = 0; i < used; i++)
- {
- for(size_t j = 0; j < used; j++)
- {
- int income1 = data[i].get_income();
- int income2 = data[j].get_income();
- if(income1 < income2)
- {
- highInc = data[j];
- }
- }
- }
- cout << highInc;
- }
- void Ward::display_avg_income()
- {
- int total = 0;
- for(size_t i = 0; i < used; i++)
- {
- total += data[i].get_income();
- }
- int average = total/used;
- cout << average;
- }
- void Ward::display_avg_people()
- {
- int total = 0;
- for(size_t i = 0; i < used; i++)
- {
- total += data[i].get_occupants();
- }
- int average = total/used;
- cout << average;
- }
- void Ward::sort_by_income()
- {
- bool done = false;
- int i;
- Household tmp;
- while(!done)
- {
- done = true;
- for(i=used-1; i > 0; --i)
- {
- if((data[i].get_income()) < (data[i-1].get_income()))
- {
- done = false;
- }
- tmp = data[i];
- data[i] = data[i-1];
- data[i-1] = tmp;
- }
- }
- }
- void Ward::sort_by_name()
- {
- bool done = false;
- int i;
- Household tmp;
- while(!done)
- {
- done = true;
- for(i=used-1; i > 0; --i)
- {
- if((data[i].get_head()) < (data[i-1].get_head()))
- {
- done = false;
- }
- tmp = data[i];
- data[i] = data[i-1];
- data[i-1] = tmp;
- }
- }
- }
- void Ward::save(std::ostream& outs)
- {
- for (size_t i = 0; i < used; ++i)
- {
- outs<<data[i];
- }
- }
- /*
- ward.h
- This is a Ward class definition file
- */
- #include<iostream>
- #include<string>
- #include<fstream>
- #include<string>
- #include<cctype>
- #include"household.h"
- #ifndef WARD_H
- #define WARD_H
- class Ward{
- public:
- const static int SIZE = 200;
- Ward();
- void load(std::istream& ins);
- void add(Household& tmp);
- void display(std::ostream& cout);
- void remove(std::string name);
- std::string lowerCase(std::string targ);
- Household find(std::string name);
- void display_richest();
- void display_avg_income();
- void display_avg_people();
- void sort_by_income();
- void sort_by_name();
- void save(std::ostream& outs);
- private:
- Household data[SIZE];
- size_t used;
- };
- #endif
- /******************************************************
- main.cc
- You are to write two classes, one called Household which stores
- information about a single household, and another called Ward which
- stores information about 200 of these households.
- You can see the necessary memeber functions of the Ward class below.
- *************************************************************/
- #include<iostream>
- #include<fstream>
- #include "household.h"
- #include "ward.h"
- using namespace std;
- // The menu function displays the menu and returns the user's choice
- int menu();
- int main(){
- Household tmp;
- Ward neighborhood;
- int choice;
- string name;
- ofstream outs;
- ifstream ins;
- ins.open("households.txt");
- // if the file is not there we will assume that this is the first
- // time the program has been run on this machine
- if(!ins.fail()){
- neighborhood.load(ins);
- ins.close();
- }
- do{
- choice = menu();
- switch(choice){
- case 1:
- cin>>tmp;
- cout<< "working line 44 ward.cc"<< endl;
- neighborhood.add(tmp);
- break;
- case 2:
- neighborhood.display(cout);
- break;
- case 3:
- cout<<"Name the head of household for the household you wish to remove:";
- getline(cin,name);
- neighborhood.remove(name);
- break;
- case 4:
- cout<<"Name the head of household for the household you wish to view:";
- getline(cin,name);
- tmp = neighborhood.find(name);
- cout<<tmp;
- break;
- case 5:
- neighborhood.display_richest();
- break;
- case 6:
- neighborhood.display_avg_income();
- break;
- case 7:
- neighborhood.display_avg_people();
- break;
- case 8:
- neighborhood.sort_by_income();
- break;
- case 9:
- neighborhood.sort_by_name();
- break;
- case 0:
- cout<<"Good-bye.\n";
- break;
- default:
- cout<<"Not an option.\n";
- break;
- } // bottom of the switch
- } while(choice != 0);
- // bottom of the while
- outs.open("households.txt");
- if(outs.fail())
- cout<<"Unable to save data to file.\n";
- else
- neighborhood.save(outs);
- outs.close();
- return 0;
- } // bottom of the main
- int menu(){
- int choice;
- cout<<"Managing the Ward.\nChoose from the selections below:\n";
- cout<<"1) Add a new household.\n";
- cout<<"2) Display all households in the Ward.\n";
- cout<<"3) Remove a household from the Ward.\n";
- cout<<"4) Find out information about a particular household.\n";
- cout<<"5) Display household with the greatest inome.\n";
- cout<<"6) Display the average income for households in the Ward.\n";
- cout<<"7) Display average number of people living in the household.\n";
- cout<<"8) Sort by income - requires an additional choice 2 to view.\n";
- cout<<"9) Sort by name - requires an addtional choice 2 to view.\n";
- cin>>choice;
- // clear out the extra newline character
- if(cin.peek() == '\n') cin.ignore();
- return choice;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement