1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the...
-
Upload
kevin-osborne -
Category
Documents
-
view
215 -
download
2
Transcript of 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the...
![Page 1: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/1.jpg)
1
Command Processor VSaving Changes
![Page 2: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/2.jpg)
Command Processor
We need to be able to save changes to the database that result from user actions.
Download example from last class http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Dow
nloads/2011_04_11_Command_Processor_4/ File Geography_with_getline.zip
2
![Page 3: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/3.jpg)
3
Saving Changes
The DOM can output itself to a file. Requires updating the DOM.
Easier to work with our own objects. Add code to output XML.
http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2011_04_13_Saving_Changes/ File output_XML.cpp
Add to Command_Processor.cpp3
![Page 4: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/4.jpg)
4
Command_Processor.cpp
void Command_Processor::Output_XML()
{
ofstream outfile;
string output_file_name = "states2.xml";
outfile.open(output_file_name.c_str());
if (!outfile.is_open())
{
cout << "Failed to open file " << output_file_name
<< " for output\n";
return;
}
cout << "Writing file " << output_file_name << endl;
outfile << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
outfile << "<USA>\n";
// Tell each state to output itself
outfile << "</USA>\n";
outfile.close();
cout << "File " << output_file_name << " written\n";
}
4
![Page 5: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/5.jpg)
Command_Processor.cpp
Add
#include <fstream>
5
![Page 6: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/6.jpg)
6
Comand_Processor.h
Add to private section:
static void Output_XML();
6
![Page 7: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/7.jpg)
Saving the Changes
void Command_Processor::Process_Commands(State** states_,
int* nr_states_)
{
cout << "Process_Commands starting\n";
states = states_;
nr_states = nr_states_;
Create_Menus();
while (command_state != Done)
{
...
}
Output_XML();
cout << "Process_Commands exiting\n";
}
7
![Page 8: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/8.jpg)
Try it!
8
![Page 9: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/9.jpg)
Look at state2.xml
So far so good!
9
![Page 10: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/10.jpg)
Saving the Changes
Now we need to add code to class State and class City to let objects write their data out to an XML file.
Add to state.h and city.h:
#include <fstream>...
void Output_XML(std::ofstream& outfile) const;
10
![Page 11: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/11.jpg)
State.cppvoid State::Output_XML(ofstream& outfile) const
{
outfile << "\t<state>\n";
outfile << "\t\t<name>\n";
outfile << "\t\t\t" << name << "\n";
outfile << "\t\t</name>\n";
outfile << "\t\t<capital>\n";
outfile << "\t\t\t" << capital << "\n";
outfile << "\t\t</capital>\n";
list<City>::const_iterator c;
c = cities.begin();
while (c != cities.end())
{
if (c->Name() == capital)
{
++c;
continue;
}
c->Output_XML(outfile);
++c;
}
cout << endl;
outfile << "\t</state>\n";
} 11
![Page 12: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/12.jpg)
12
City.cpp
#include <fstream>
void City::Output_XML(std::ofstream& outfile) const
{
outfile << "\t\t<city>\n";
outfile << "\t\t\t" << name << "\n";
outfile << "\t\t</city>\n";
}
12
![Page 13: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/13.jpg)
13
Command_Processor::Output_XML
// Tell each state to output itself
for (int i = 0; i < *nr_states; ++i)
{
states[i]->Output_XML(outfile);
}
Build and run.
13
![Page 14: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/14.jpg)
14
Program Running
14
![Page 15: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/15.jpg)
15
states2.xml
<?xml version="1.0" encoding="utf-8"?>
<USA>
<state>
<name>
Florida
</name>
<capital>
Tallahassee
</capital>
<city>
Clearwater
</city>
<city>
Miami
</city>
<city>
Tampa
</city>
</state>
...
15
![Page 16: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/16.jpg)
<state>
<name>
New York
</name>
<capital>
Albany
</capital>
<city>
Buffalo
</city>
<city>
New York City
</city>
</state>
<state>
<name>
New Mexico
</name>
<capital>
Santa Fe
</capital>
<city>
Las Vegas
</city>
</state>
</USA>16
![Page 17: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/17.jpg)
Test the Output File
Let's check that the output file works as an input file.
Rename states.xml to states_0.xml states2.xml to states.xml
17
![Page 18: 1 Command Processor V Saving Changes. Command Processor We need to be able to save changes to the database that result from user actions. Download example.](https://reader036.fdocuments.us/reader036/viewer/2022082819/56649f355503460f94c53b37/html5/thumbnails/18.jpg)
The New Information is There
18