Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError...

13
Lab2 Diagrams Cop 4232 Fall 2005

Transcript of Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError...

Page 1: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Lab2 Diagrams

Cop 4232

Fall 2005

Page 2: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Fall 2005 Lab2 Cop4232 2

IOMgmt

IOMgmt

InMgrIOError

OutMgr

Tokenizer

TokenError

StringTokenizer

pgmInMgr

pgmOutMgr

Page 3: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Fall 2005 Lab2 Cop4232 3

SimMgmt & SimModels

SimMgmt SimModels

Agent

#InMgr& finMgr //class#OutMgr& foutMgr //class-string name //instance

+Agent()+Agent(Tokenizer& )+Agent(string )+Insert()+Extract()+Extract(Tokenizer&)+Extract(string)+operator>>(ifstream&, Agent&)+operator<<(ostream&, Agent&)+NameOf()=>string+InMgrOf()=>InMgr&+OutMgrOf()=>OutMgr&+setInMgr(InMgr&)+setOutMgr(OutMgr&)#Put()#Get(Tokenizer&)#Get(StringTokenizer&)

Customer

-Money cash-string paymethod-CreditCard bankcard

+Customer()+Customer(Tokenizer& )+Customer(string )+Extract()+Extract(Tokenizer&)+Extract(string)+Insert()#Get()#Get(Tokenizer&)#Get(StringTokenizer&)#Put()

PumpStation

-Money pricepergal[3]-Money purchaselimit-Money purchaseamt-double purchasegal-double pumprate-Grade gradeselected

+ PumpStation()+ PumpStation(Tokenizer& )+ PumpStation(string )+Extract()+Extract(Tokenizer&)+Extract(string)+Insert()#Get()#Get(Tokenizer&)#Get(StringTokenizer&)#Put()

Page 4: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Fall 2005 Lab2 Cop4232 4

SimMgmt & SimModels

Agent

-string name //instance

+Agent()+Agent( ifstream& )+Agent(Tokenizer& )+Agent(string )+Insert( ostream& )+Extract( ifstream& )+Extract( Tokenizer& )+Extract( string )+operator>>(ifstream&, Agent&)+operator<<(ostream&, Agent&)+NameOf()=>string#Put( ostream& )#Get( ifstream& )#Get( Tokenizer& )#Get( StringTokenizer& )

Customer

-Money cash-string paymethod-CreditCard bankcard

PumpStation

-Money pricepergal[3]-Money purchaselimit-Money purchaseamt-double purchasegal-double pumprate-Grade gradeselected

Version #1

+Customer()+Customer( ifstream& )+Customer(Tokenizer& )+Customer(string )+Insert( ostream& )+Extract( ifstream& )+Extract( Tokenizer& )+Extract( string )#Put( ostream& )#Get( ifstream& )#Get( Tokenizer& )#Get( StringTokenizer& )

+PumpStation()+PumpStation( ifstream& )+PumpStation(Tokenizer& )+PumpStation(string )+Insert( ostream& )+Extract( ifstream& )+Extract( Tokenizer& )+Extract( string )#Put( ostream& )#Get( ifstream& )#Get( Tokenizer& )#Get( StringTokenizer& )

InMgr and OutMgrare NOT USED!

Page 5: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Fall 2005 Lab2 Cop4232 5

Client A for Version #1

int main(){ ifstream fin; ostream fout; string finName, foutName; cout << “Enter input file name: “; cin >> finName; cout << “Enter output file name: “; cin >> foutName; fin.open( finName.c_str()); fout.open( foutName.c_str());

Agent alpha, beta(fin), gamma; fin >> gamma; alpha.Extract(fin);

fout << alpha << endl; beta.Insert(fout); fout << gamma << endl;}

Agent{ name: BETA }Agent Agent{ name: GAMMA }AgentAgent{ name: ALPHA }Agent

Input File

Agent{ name: ALPHA }AgentAgent{ name: BETA }Agent Agent{ name: GAMMA }Agent

Output File

Agent{ name: ALPHA }Agent

Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent

Output Style 1

Output Style 2

ProblemFormatting the output image is limited.To achieve the indented style, each instancemust begin on a new line of the output stream.

Page 6: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Fall 2005 Lab2 Cop4232 6

Client B for Version #1

int main(){ Tokenizer T; //Tokenizer encapsulates input stream

Agent alpha, beta(T), gamma(T); alpha.Extract(T);

string Image = “ Agent{ name: DELTA }Agent “; Agent delta( Image ); //Hidden use of StringTokenizer

fout << alpha << endl; beta.Insert(fout); fout << gamma << endl; fout << delta << endl;}

Agent{ name: BETA }Agent Agent{ name: GAMMA }AgentAgent{ name: ALPHA }Agent

Input File

Agent{ name: ALPHA }AgentAgent{ name: BETA }Agent Agent{ name: GAMMA }AgentAgent{ name: DELTA }Agent

Output File

Agent{ name: ALPHA }Agent

Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent

Agent{ name: DELTA}Agent

Output Style 1

Output Style 2

ProblemFormatting the output image is limited.To achieve the indented style, each instancemust begin on a new line of the output stream.

or

Page 7: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Fall 2005 Lab2 Cop4232 7

SimMgmt & SimModels

Agent

-string name //instance

+Agent()+Agent( InMgr& )+Agent( Tokenizer& )+Agent( string )+Insert( OutMgr& )+Extract( InMgr& )+Extract( Tokenizer& )+Extract( string )+operator>>(ifstream&, Agent&)+operator<<(ostream&, Agent&)+NameOf()=>string#Put( OutMgr& )#Get( InMgr& )#Get( Tokenizer& )#Get( StringTokenizer& )

Customer

-Money cash-string paymethod-CreditCard bankcard

PumpStation

-Money pricepergal[3]-Money purchaselimit-Money purchaseamt-double purchasegal-double pumprate-Grade gradeselected

Version #2

+Customer()+Customer( InMgr& )+Customer( Tokenizer& )+Customer( string )+Insert( OutMgr& )+Extract( InMgr& )+Extract( Tokenizer& )+Extract( string )#Put( OutMgr& )#Get( InMgr& )#Get( Tokenizer& )#Get( StringTokenizer& )

+PumpStation()+PumpStation( InMgr& )+PumpStation( Tokenizer& )+PumpStation( string )+Insert( OutMgr& )+Extract( InMgr& )+Extract( Tokenizer& )+Extract( string )#Put( OutMgr& )#Get( InMgr& )#Get( Tokenizer& )#Get( StringTokenizer& )

InMgr and OutMgrare USED!

Page 8: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Fall 2005 Lab2 Cop4232 8

Client A for Version #2

int main(){ InMgr finMgr; InMgr& fin = finMgr.getStream(); OutMgr foutMgr; OutMgr& fout = foutMgr.getStream(); Agent alpha, beta(finMgr), gamma; fin >> gamma; alpha.Extract(finMgr);

fout << alpha << endl; //<< cannot use foutMgr beta.Insert(foutMgr); //previous line compromises foutMgr fout << gamma << endl; //invalidates state of foutMgr}

Agent{ name: BETA }Agent Agent{ name: GAMMA }AgentAgent{ name: ALPHA }Agent

Input File

Agent{ name: ALPHA }AgentAgent{ name: BETA }Agent Agent{ name: GAMMA }Agent

Output File

Agent{ name: ALPHA }Agent

Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent

Output Style 1

Output Style 2

ProblemUse of insertion operators in clientmay corrupt or invalidate the state of OutMgr.The implementation of << cannot know the associatedOutMgr (and there may not be one)! If the client insertsits own formatting operations (e.g. endl) then the stateof the associated OutMgr becomes invalid and subsequentoutput may not be formatted correctly.

To correct this, operator<< should not be defined as a friend.Defining operator>> is permitted as a friend. In fact, Extract()and Get() can be defined to take ifstream& as a parameter –it is not necessary to use InMgr&

Page 9: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Fall 2005 Lab2 Cop4232 9

SimMgmt & SimModels

Agent

#InMgr& finMgr //class#OutMgr& foutMgr //class-string name //instance

+Agent()+Agent(Tokenizer& )+Agent(string )+Insert() //uses InMgr+Extract() //uses OutMgr+Extract(Tokenizer&)+Extract(string)+operator>>(ifstream&, Agent&)+operator<<(ostream&, Agent&)+NameOf()=>string+InMgrOf()=>InMgr&+OutMgrOf()=>OutMgr&+setInMgr(InMgr&)+setOutMgr(OutMgr&)#Put() //uses OutMgr#Get() //uses InMgr#Get(Tokenizer&)#Get(StringTokenizer&)

Customer

-Money cash-string paymethod-CreditCard bankcard

+Customer()+Customer(Tokenizer& )+Customer(string )+Extract()+Extract(Tokenizer&)+Extract(string)+Insert()#Get()#Get(Tokenizer&)#Get(StringTokenizer&)#Put()

PumpStation

-Money pricepergal[3]-Money purchaselimit-Money purchaseamt-double purchasegal-double pumprate-Grade gradeselected

+ PumpStation()+ PumpStation(Tokenizer& )+ PumpStation(string )+Extract()+Extract(Tokenizer&)+Extract(string)+Insert()#Get()#Get(Tokenizer&)#Get(StringTokenizer&)#Put()

Version #3

InMgr and OutMgrare Encapsulated

by Agent!

Page 10: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Fall 2005 Lab2 Cop4232 10

Client A for Version #3

int main(){ InMgr& finMgr = Agent::InMgrOf(); //access class attributes OutMgr& foutMgr = Agent::OutMgrOf(); //access class attributes ifstream& fin = finMgr.getStream(); ostream& fout = foutMgr.getStream(); Agent alpha, beta, gamma; beta.Extract(); fin >> gamma; alpha.Extract();

fout << alpha; //verifies that fout is associated with foutMgr beta.Insert(); //uses foutMgr fout << gamma; //verifies that fout is associated with foutMgr foutMgr.newLine();}

Agent{ name: BETA }Agent Agent{ name: GAMMA }AgentAgent{ name: ALPHA }Agent

Input File

Agent{ name: ALPHA }Agent Agent{ name: BETA }Agent Agent{ name: GAMMA }Agent

Output File

Page 11: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Fall 2005 Lab2 Cop4232 11

Lab2 Fall05

SimMgmt

SimModels

Customer PumpStation

JiffyStop

Agent

1..*1..*

Money CreditCard

IOMgmt

InMgrOutMgr

TokenError IOError

AppError

pgmInMgrpgmOutMgr

StringTokenizerTokenizer

Page 12: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Fall 2005 Lab2 Cop4232 12

Lab1 Figures A-1,2,3,4Line Org

Cur pos

File Org

idx

MarginStack

MARGINSIZE

Line Org

Cur pos

File Org

Stack(before)

Stack(after)

idx idx

Method: pushMargin()Line Org

Cur pos

File Org

Stack(before)

Stack(after)

idxidx

Method: popMargin()

Cur fin

File Org

Method: deltaMargin(input)

New fin(delta > 0)

New fin(delta < 0)

Line Org

Curpos(before)

File Org

Method: newLine() Curpos(after)

File Org Line Org

Page 13: Lab2 Diagrams Cop 4232 Fall 2005. Lab2 Cop42322 IOMgmt InMgrIOError OutMgr Tokenizer TokenError StringTokenizer pgmInMgr pgmOutMgr.

Fall 2005 Lab2 Cop4232 13

Figures 3-6,7,8

Line Org Cur pos

File Org

Method: deltaMargin(output)

Cur pos(delta > 0)

diff

File Org

Cur pos(delta < 0)

Line Org

diff

Method: toMargin(output)X{

}X

XData1: value1XData2: value2XData3:

XDatak: valuek

Y{

}Y

YData1: value1YData2: value2Instance of X Embedded instance of Y

Cur fout

File Org

New foutNew fout

idx

blanks