Programming with Visual C++: Concepts and Projects Chapter 4B: Selection (Tutorial)

Post on 06-Jan-2018

222 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

Problem Analysis Construct a program that allows the user to select a destination and indicate the number of nights and the number of rooms required The program will calculate cost and apply multiple room discounts, if appropriate Programming with Visual C++: Concepts and Projects3

Transcript

Programming with Visual C++: Concepts and Projects

Chapter 4B: Selection (Tutorial)

Tutorial: Vacation Planner

• Problem Analysis• Design– Interface sketch– Control and Data Tables– Algorithms

• Development– Create the interface– Code event handlers

• TestingProgramming with Visual C++: Concepts and Projects 2

Problem Analysis

• Construct a program that allows the user to select a destination and indicate the number of nights and the number of rooms required

• The program will calculate cost and apply multiple room discounts, if appropriate

Programming with Visual C++: Concepts and Projects 3

Problem Analysis (continued)

Programming with Visual C++: Concepts and Projects 4

Problem Analysis (continued)

Programming with Visual C++: Concepts and Projects 5

Design

• Interface Sketch– MonthCalendar controls for date of arrival and

departure– GroupBox control used to contain RadioButtons– RadioButtons indicate choice of destination• When one is selected the others in the group will

automatically become deselected

– Some control settings will be initialized upon loading the form in memory

Programming with Visual C++: Concepts and Projects 6

Design (continued)

Programming with Visual C++: Concepts and Projects 7

Design (continued)

Programming with Visual C++: Concepts and Projects 8

Design (continued)

Programming with Visual C++: Concepts and Projects 9

Design (continued)

• Algorithm for Form1_Load() event

Programming with Visual C++: Concepts and Projects 10

Design (continued)

• Algorithm for btnCalc_Click() event– Step 1: Declare variables– Step 2: Read the input data• Read rooms• Read arrival date• Read departure date• Assign room cost based on destination (radio buttons)

– Step 3: Process data

Programming with Visual C++: Concepts and Projects 11

Design (continued)

Programming with Visual C++: Concepts and Projects 12

Design (continued)

Programming with Visual C++: Concepts and Projects 13

• A look ahead at Visual C++ code (a multiple alternative if statement) to implement Step 2.4 (determining the cost of the room)

Design (continued)

• Algorithm for btnCalc_Click() event – Step 3– If rooms < 1 then error message– Calculate nights– If nights < 1 then error message– If no errors• Select room discount• Calculate total cost• Display total cost

Programming with Visual C++: Concepts and Projects 14

Design (continued)

Programming with Visual C++: Concepts and Projects 15

Design (continued)

Programming with Visual C++: Concepts and Projects 16

• A look ahead at Visual C++ code (a single alternative if statement) to implement Step 3.1

Design (continued)

Programming with Visual C++: Concepts and Projects 17

Design (continued)

Programming with Visual C++: Concepts and Projects 18

• A look ahead at Visual C++ code (a multiple alternative switch statement) to implement Step 3.4.1

Design (continued)

Programming with Visual C++: Concepts and Projects 19

• Final Algorithm for btnCalc_Click()

Design (continued)

Programming with Visual C++: Concepts and Projects 20

Design (continued)

• Test strategy– Identify valid data ranges– Identify invalid data ranges

Programming with Visual C++: Concepts and Projects 21

Design (continued)• Test strategy– Create test scenarios that involve valid and invalid

data

Programming with Visual C++: Concepts and Projects 22

Design (continued)• Use a structured walkthrough (trace table) to verify

that the algorithm is correct under each scenario• List algorithm steps down the left side and variables

across the top

Programming with Visual C++: Concepts and Projects 23

Design (continued)

Programming with Visual C++: Concepts and Projects 24

• The first scenario fails because in Step 3.4 because ok does not contain a value. It was never assigned in Step 1 (we should make a note of this and revise the algorithm accordingly

Design (continued)

Programming with Visual C++: Concepts and Projects 25

• With the revision made, the algorithm now traces correctly to its finish

Design (continued)

Programming with Visual C++: Concepts and Projects 26

• Each test scenario should be verified this way

Design (continued)

• If the algorithm passes all tests then you are ready to move on to the Development stage

• If the algorithm fails any test then it must be revised until it passes

Programming with Visual C++: Concepts and Projects 27

Development

• Create the interface– Use a GroupBox control to contain the

RadioButtons– GroupBoxes must be laid down before

RadioButtons are placed in them• Code the event handlers– Form1_Load()– btnCalc_Click()

Programming with Visual C++: Concepts and Projects 28

Development (continued)

Programming with Visual C++: Concepts and Projects 29

Development (continued)

Programming with Visual C++: Concepts and Projects30

Development (continued)

Programming with Visual C++: Concepts and Projects 31

Development (continued)

• The ComboBox control– Use the Items property to enter the collection of

items you wish to appear in the ComboBox list– Each item has its own index value– The first (empty) item has a value of -1

Programming with Visual C++: Concepts and Projects 32

Development (continued)

Programming with Visual C++: Concepts and Projects 33

Development (continued)

Programming with Visual C++: Concepts and Projects 34

Development (continued)

Programming with Visual C++: Concepts and Projects 35

• You can set a default selection for the ComboBox by assigning a SelectedIndex value

Development (continued)

• Code the event handlers– Form1_Load()– btnCalc_Click()

Programming with Visual C++: Concepts and Projects 36

Development (continued)

Programming with Visual C++: Concepts and Projects 37

• Form1_Load()– Assigns default ComboBox value– Initializes destination names on RadioButtons– Sets default destination RadioButton

Development (continued)

Programming with Visual C++: Concepts and Projects 38

• Variables depart and arrive are DateTime objects read from the monthlyCalendars

• Subtraction is defined for DateTime objects for– Months– Days– Years

On Your Own

• Test scenarios– Verify that your program works as expected for each

of the test scenarios• Rewrite the switch statement– Use a multiple alternative if statement instead

• Extreme nesting– Remove the ok variable from your program– Use nested if…else statements to filter out error

conditions

Programming with Visual C++: Concepts and Projects 39

top related