1 Command Processor II. 2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

Operator overloading redefine the operations of operators
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)
Computer Science 1620 Loops.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Introduction to Object- Oriented Programming Prof. Shie-Jue Lee Dept. of Electrical Engineering National Sun Yat-sen University.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion.
11 Introduction to Object Oriented Programming (Continued) Cats II.
Java: Chapter 1 Computer Systems Computer Programming II Aug
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
Binary Search Trees II Morse Code.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
1 Huffman Codes Drozdek Chapter Objectives You will be able to Construct an optimal variable bit length code for an alphabet with known probability.
Chapter 4 Selection Structures: Making Decisions.
Adding a New Option to the Framework. Introduction This is intended as a step by step guide to adding a new action to the menu or toolbar. The order of.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
C++ Loop Statements Repetition Revisited. Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
1 Command Processor IV. 2 Getting Started Download example from last class (finished):
11 Introduction to Object Oriented Programming (Continued) Cats.
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.
C++ Classes and Data Structures Jeffrey S. Childs
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
1 Modeling CDs (Continued). 2 Getting Started Download: 2011_02_07_CD_Collection_Incomplete/
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes.
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Huffman Codes Drozdek Chapter Encoding Next we will add the capability to encode a message entered as normal text. The Huffman Tree that we use.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
1 Modeling CDs. 2 Objectives You will be able to: Sketch a diagram showing the relationship between a class and classes that it contains a members. Define.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
1 Introduction to Object Oriented Programming Chapter 10.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.
1 Low Level ADO.NET Operations II Microsoft Visual C# 2008 Step by Step Chapter 25.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
1 Implementing Ticket Printer. Download project from last class Downloads/2016_02_12_In_Class/
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
1 Huffman Codes Using Binary Files. 2 Getting Started Last class we extended a program to create a Huffman code and permit the user to encode and decode.
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
1 Command Processor. Objectives You will be able to Create and understand State Diagrams. Write a text based command processor with multilevel menus.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
1 Using an XML Parser. 2 Objective You will be able to use a publically available open source parser to convert an XML file into an internal data structure.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Object Lifetime and Dynamic Objects
group work #hifiTeam
Govt. Polytechnic,Dhangar
Inheritance: Polymorphism and Virtual Functions
Jeff West - Quiz Section 6
Constructors, GUI’s(Using Swing) and ActionListner
Class rational part2.
Presentation transcript:

1 Command Processor II

2 Command Processor Example Let's look at a simple example of a command processor using states and cities. Get initial information from XML file. Permit user to add information using text menu based commands. Download example from last class _04_04_Command_Processor/ File Menu.zip _04_04_Command_Processor/ Rename as Command_Processor Expand Build Run

Program Running 3

4 Command Processor We have a working Menu class. Now let's add the command processor. Will use the Menu class. Text based menu Select one of a small number of choices Choices identified by number

5 Menu Hierarchy Initial Command State Menu 1. Select State 2. Add State 3. Quit When user selects 1 Ask state. Remember as selected state. Enter new command state, State Selected. When users selects 2 Get new state info Add state. Enter new command state, State Selected with new state selected. When user selects 3 Update file and terminate.

6 Menu Hierarchy State Selected Menu Add City Change State Quit When user selects 1 Get new city info. Add city to currently selected state. Remain in same command state. When user selects 2 Switch to Initial command state. When user selects 3 Update file and terminate.

7 Command Processor Display menu for current command state. Get user's selection. Invoke function to perform action for selected command. Update command state.

8 Command Processing We have two menus. Command state says which one to use. Command results in: Action Possibly new command state.

9 Command Processor Select Menu object based on command state. Use Menu object to get command Execute command. Update command state. Start with stubs for commands. Get the wiring right.

10 Command Processor Add new class Command_Processor. The control class for our program. Only purpose is as home for a static method that will be the top level of control. Process_Commands. Define a private default constructor to show the intent that the class should never be instantiated.

11 Command_Processor.h #pragma once #include "Menu.h" class Command_Processor { public: static void Process_Commands(); private: enum Command_States { Initial, State_Selected, Done }; static const int NR_STATES = (int) Done + 1; static Command_States command_state; static Menu* menus[NR_STATES]; Command_Processor(void) {}; }; Private constructor ensures that class cannot be instantiated.

12 Command_Processor.cpp #include #include "Command_Processor.h" #include "Menu.h" using namespace std; Command_Processor::Command_States Command_Processor::command_state = Initial; Menu* Command_Processor::menus[NR_STATES]; void Command_Processor::Process_Commands() { cout << "Process commands starting\n"; while (command_state != Done) { // Execute next command command_state = Done; } cout << "Command_Processor exiting\n"; } Start with a stub.

13 main.cpp #include #include "Command_Processor.h" using namespace std; int main(void) { Command_Processor::Process_Commands(); cout << "Normal Termination\n"; cin.get(); return 0; } Build and run.

14 Inital Stub Running

15 Command_Processor.cpp void Command_Processor::Process_Commands() { cout << "Process commands starting\n"; Create_Menus(); while (command_state != Done) { const string* cmd = menus[command_state]->Get_Command(); switch (command_state) { case Initial: Process_Command_0(*cmd); break; case State_Selected: Process_Command_1(*cmd); break; case Done: break; // Can't happen } cout << "Command_Processor exiting\n"; }

16 Command_Processor.cpp void Command_Processor::Create_Menus() { // Menu for Initial command state Menu* menu = new Menu("Enter command number"); menu->Add_Command("Select State"); menu->Add_Command("Add State"); menu->Add_Command("Quit"); menus[0] = menu; // Menu for State Selected menu = new Menu("Enter command number"); menu->Add_Command("Add City"); menu->Add_Command("Change State"); menu->Add_Command("Quit"); menus[1] = menu; }

Initial Command State // Process command in Initial command state void Command_Processor::Process_Command_0(const string& cmd) { if (cmd == "Select State") { // Select state cout << "Select State command\n"; command_state = State_Selected; } else if (cmd == "Add State") { // Add state cout << "Add State command\n"; command_state = State_Selected; } else { cout << "Quit command\n"; command_state = Done; }

18 State Selected Command State // Process command in State Selected command state void Command_Processor::Process_Command_1(const string& cmd) { if (cmd == "Add City") { // Add City cout << "Add City command\n"; } else if (cmd == "Change State") { cout << "Change State command\n"; command_state = Initial; } else { cout << "Quit command\n"; command_state = Done; }

Update Command_Processor.h #pragma once #include "Menu.h" #include using std::string; class Command_Processor { public: static void Process_Commands(); private: enum Command_States { Initial, State_Selected, Done }; static const int NR_STATES = (int) Done + 1; static Command_States command_state; static Menu* menus[NR_STATES]; Command_Processor(void) {}; static void Create_Menus(); static void Process_Command_0(const string& cmd); static void Process_Command_1(const string& cmd); }; 19

20 Running with Command Stubs