Updated, improved and cleaned RESTbucks example.

Moved it under "rest" subfolder.
This commit is contained in:
2017-02-13 16:23:38 +01:00
parent a44c4d9a16
commit b56aec67a9
35 changed files with 1826 additions and 1419 deletions

View File

@@ -0,0 +1,135 @@
note
description: "Summary description for {ORDER}."
date: "$Date$"
revision: "$Revision$"
class
ORDER
create
make,
make_empty
feature -- Initialization
make_empty
do
create {ARRAYED_LIST [ORDER_ITEM]} items.make (10)
revision := 0
set_id ("")
status := {RESTBUCKS_API}.status_unset
end
make (a_id: READABLE_STRING_8; a_status: detachable READABLE_STRING_GENERAL)
do
make_empty
set_id (a_id)
if a_status /= Void then
set_status (a_status)
else
check status.same_string_general ({RESTBUCKS_API}.status_unset) end
end
end
feature -- Access
id: IMMUTABLE_STRING_8
location: detachable STRING_32
status: STRING_32
-- Status of the order, see {RESTBUCKS_API}.Order_states
revision: INTEGER
items: LIST [ORDER_ITEM]
feature -- Status report
has_id: BOOLEAN
-- Has valid identifier `id`?
do
Result := not id.is_whitespace and not id.is_case_insensitive_equal ("0")
end
is_submitted: BOOLEAN
do
Result := status.is_case_insensitive_equal_general ({RESTBUCKS_API}.status_submitted)
end
feature -- element change
set_id (a_id: READABLE_STRING_8)
do
if attached {IMMUTABLE_STRING_8} a_id as l_id then
id := l_id
else
create id.make_from_string (a_id)
end
ensure
id_assigned : a_id.same_string (id)
end
set_location (a_location: detachable READABLE_STRING_GENERAL)
do
if a_location = Void then
location := Void
else
create location.make_from_string_general (a_location)
end
ensure
location_assigned: (a_location = Void implies location = Void)
or (a_location /= Void implies attached location as loc and then a_location.same_string (loc))
end
mark_submitted
do
status := {RESTBUCKS_API}.status_submitted
end
set_status (a_status: READABLE_STRING_GENERAL)
do
create status.make_from_string_general (a_status)
ensure
status_assigned : a_status.same_string (status)
end
add_item (a_item: ORDER_ITEM)
require
valid_item: a_item /= Void
do
items.force (a_item)
ensure
has_item : items.has (a_item)
end
add_revision
do
revision := revision + 1
ensure
revision_incremented : old revision + 1 = revision
end
feature -- Report
hash_code: INTEGER_32
-- Hash code value
do
from
items.start
Result := items.item.hash_code
until
items.off
loop
Result:= ((Result \\ 8388593) |<< 8) + items.item.hash_code
items.forth
end
if items.count > 1 then
Result := Result \\ items.count
end
end
note
copyright: "2011-2017, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,93 @@
note
description: "Summary description for {ORDER_ITEM}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
ORDER_ITEM
inherit
ORDER_ITEM_VALIDATION
create
make
feature -- Initialization
make (a_name: STRING_32; a_size: STRING_32; a_option: STRING_32; a_quantity: NATURAL_8)
do
set_name (a_name)
set_size (a_size)
set_option (a_option)
set_quantity (a_quantity)
end
feature -- Access
name: READABLE_STRING_32
-- product name type of Coffee(Late, Cappuccino, Expresso)
option: READABLE_STRING_32
-- customization option Milk (skim, semi, whole)
size: READABLE_STRING_32
-- small, mediumm large
quantity: NATURAL_8
feature -- Element Change
set_name (a_name: like name)
require
valid_name: is_valid_coffee_type (a_name)
do
name := a_name
ensure
name_assigned: name.same_string (a_name)
end
set_size (a_size: like size)
require
valid_size: is_valid_size_option (a_size)
do
size := a_size
ensure
size_assigned: size.same_string (a_size)
end
set_option (a_option: like option)
require
valid_option: is_valid_milk_type (a_option)
do
option := a_option
ensure
option_assigned: option.same_string (a_option)
end
set_quantity (a_quantity: NATURAL_8)
do
quantity := a_quantity
ensure
quantity_assigned: quantity = a_quantity
end
feature -- Report
hash_code: INTEGER
-- Hash code value
do
Result := option.hash_code + name.hash_code + size.hash_code + quantity.hash_code
end
invariant
valid_size: is_valid_size_option (size)
valid_coffe: is_valid_coffee_type (name)
valid_customization: is_valid_milk_type (option)
valid_quantity: quantity > 0
note
copyright: "2011-2017, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,51 @@
note
description: "Summary description for {ORDER_ITEM_VALIDATION}."
date: "$Date$"
revision: "$Revision$"
class
ORDER_ITEM_VALIDATION
feature -- Access
is_valid_coffee_type (a_type: READABLE_STRING_GENERAL): BOOLEAN
-- Is `a_type' a valid coffee type
do
Result := across coffe_types as ic some a_type.is_case_insensitive_equal (ic.item) end
end
Coffe_types: ARRAY [STRING]
-- List of valid Coffee types
once
Result := <<"late", "cappuccino", "expresso">>
end
is_valid_milk_type (a_type: READABLE_STRING_GENERAL): BOOLEAN
-- Is `a_type' a valid milk type
do
Result := across milk_types as ic some a_type.is_case_insensitive_equal (ic.item) end
end
Milk_types: ARRAY [STRING]
-- List of valid Milk types
once
Result := <<"skim", "semi", "whole">>
end
is_valid_size_option (a_option: READABLE_STRING_GENERAL): BOOLEAN
-- Is `a_option' a valid size option
do
Result := across size_options as ic some a_option.is_case_insensitive_equal (ic.item) end
end
Size_options: ARRAY [STRING]
-- List of valid Size_options
once
Result := <<"small", "medium", "large">>
end
note
copyright: "2011-2017, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end