CSCI 6962: Server-side Design and Programming Support Classes and Shopping Carts.

19
CSCI 6962: Server-side Design and Programming Support Classes and Shopping Carts

Transcript of CSCI 6962: Server-side Design and Programming Support Classes and Shopping Carts.

CSCI 6962: Server-side Design and Programming

Support Classes and Shopping Carts

Model-View-Control Architecture

• Model = software classes that store/manipulate information gathered by application– Usually separate from beans, which interact with them– Often interact with databases

request JSF page

Classes that model business process

Database

Managed bean

Business Model Objects

Key idea:• Methods in model objects should implement

business model– Methods to validate values (number in stock, etc.)– Methods to perform computations (prices, taxes, etc.)– Methods to store/retrieve information in database

• Often created by other developers as part of overall information system

Model-View-Control Architecture

• Model: Support objects that implement business logic

• View:JSF pages that define how information presented to user

• Control:Managed beans that act as intermediary between JSF pages and support classes, determine next pages, etc.

4

Model Class Example

• Widget model class for widget ordering(designed for shopping cart)– Name, ID, price of specific widget added to order– Quantity of that widget in this order– Constructor takes ID and retrieves price, name

from database– Provides array of all widget IDs for selection

(retrieved from database)

5

Widget Support Class

6

Member variables

Getters for data that does not change(required if hook to JSF elements)

Getters and setters for data that might change

Widget Support Class

7

Given ID, constructor “looks up” corresponding widget name and price (would be done with database access)

Returns list of all widget IDs to construct menu of choices for purchase (would also be done in database)

Shopping Carts

• Displays list of items added to cart– Name, price, quantity, other relevant information

• Usually not product ID– May display other information

• Total order price, etc.• User can usually modify cart contents

– Remove items– Update quantities– …

Shopping Carts

• Usually implemented as list of items– List = Vector type in Java– Has methods to add new element to end, get ith

element, and remove ith element• Each list element is business model object

– Has fields for all relevant data about item purchased– May have way to set quantity purchased

• Not all models have quantity (course registration, etc.)

Widget Cart in JSF

10

Implementing Widget Selection

• Simple approach: Generate array of SelectItem elements in bean– Created from static Widget method

11

Iterate through array, constructing Widget for each to get name, price

Construct selectItem for each with ID as value and combination of name and price as label

Implementing Widget Selection

• Displayed as list of items using <f:selectItems• Linked to itemPurchased in cart bean• Validation to insure something selected…

12

Implementing the Cart

• Contains Vector (linked list) of items• When item purchased, new Widget object

constructed from ID and added to that list

13

Displaying the Cart

• Method returns list of items as array– Works best with JSF page

14

Displaying the Cart

• JSF page loops through array– Each iteration accesses different Widget object

• Can the access fields of that Widget object for display– Syntax: ${widgetobject.fieldname}– Automatically calls correpsonding getFieldname

method in support class

15

Updating the Cart

Bean provides methods for • Removing widget with given ID• Updating quantity of widget with given ID

16

Updating the Cart

• Requires methods to look up item to modify by ID – Passed from JSF page

17

Displaying Buttons in Cart Page

• Each table row contains entire form for remove, update quantity buttons

• Passes widget ID as parameter (created from widget object in loop)

18

Updating Quantity

• Display current quantity in inputText element– Use validation to make sure user changes legally

• Call update method with ID, new quantity value

19