We have updated the content of our program. To access the current Software Engineering curriculum visit curriculum.turing.edu.
Black Thursday Iteration 3 - Item Sales
We’ve got a good foundation, now it’s time to actually track the sale of items. There are three new data files to mix into the system, so for this iteration we’ll focus primarily on DAL with just a bit of Business Intelligence.
Parts to iteration 3: Data Access Layer Business Intelligence
Data Access Layer
InvoiceItem
InvoiceItem
objects are how invoices are connected to items. A single InvoiceItem
connects a single Item
with a single Invoice
.
The InvoiceItem
has the following data accessible:
id
- returns the integer iditem_id
- returns the item idinvoice_id
- returns the invoice idquantity
- returns the quantityunit_price
- returns the unit_pricecreated_at
- returns aTime
instance for the date the invoice item was first createdupdated_at
- returns aTime
instance for the date the invoice item was last modified
It also offers the following method:
unit_price_to_dollars
- returns the price of the invoice item in dollars formatted as aFloat
We create an instance like this:
ii = InvoiceItem.new({
:id => 6,
:item_id => 7,
:invoice_id => 8,
:quantity => 1,
:unit_price => BigDecimal(10.99, 4),
:created_at => Time.now,
:updated_at => Time.now
})
# => <InvoiceItem...>
ii.unit_price_to_dollars
# => 23.48
InvoiceItemRepository
The InvoiceItemRepository
is responsible for holding and searching our InvoiceItem
instances. It offers the following methods:
all
- returns an array of all knownInvoiceItem
instancesfind_by_id
- returns eithernil
or an instance ofInvoiceItem
with a matching IDfind_all_by_item_id
- returns either[]
or one or more matches which have a matching item IDfind_all_by_invoice_id
- returns either[]
or one or more matches which have a matching invoice IDcreate(attributes)
- create a newInvoiceItem
instance with the providedattributes
. The newInvoiceItem
’s id should be the current highestInvoiceItem
id plus 1.update(id, attribute)
- update theInvoiceItem
instance with the correspondingid
with the providedattributes
. Only the invoice_item’squantity
andunit_price
can be updated. This method will also change the invoice_item’supdated_at
attribute to the current time.delete(id)
- delete theInvoiceItem
instance with the correspondingid
The data can be found in data/invoice_items.csv
so the instance is created and used like this:
sales_engine = SalesEngine.from_csv(:invoice_items => "./data/invoice_items.csv")
invoice_item = sales_engine.invoice_items.find_by_id(6)
# => <InvoiceItem...>
Transaction
Transactions are billing records for an invoice. An invoice can have multiple transactions, but should have at most one that is successful.
The transaction has the following data accessible:
id
- returns the integer idinvoice_id
- returns the invoice idcredit_card_number
- returns the credit card numbercredit_card_expiration_date
- returns the credit card expiration dateresult
- the transaction resultcreated_at
- returns aTime
instance for the date the transaction was first createdupdated_at
- returns aTime
instance for the date the transaction was last modified
We create an instance like this:
t = Transaction.new({
:id => 6,
:invoice_id => 8,
:credit_card_number => "4242424242424242",
:credit_card_expiration_date => "0220",
:result => "success",
:created_at => Time.now,
:updated_at => Time.now
})
TransactionRepository
The TransactionRepository
is responsible for holding and searching through our Transaction
instances. It offers the following methods:
all
- returns an array of all knownTransaction
instancesfind_by_id
- returns eithernil
or an instance ofTransaction
with a matching IDfind_all_by_invoice_id
- returns either[]
or one or more matches which have a matching invoice IDfind_all_by_credit_card_number
- returns either[]
or one or more matches which have a matching credit card numberfind_all_by_result
- returns either[]
or one or more matches which have a matching statuscreate(attributes)
- create a newTransaction
instance with the providedattributes
. The newTransaction
’s id should be the current highestTransaction
id plus 1.update(id, attribute)
- update theTransaction
instance with the correspondingid
with the providedattributes
. Only the transaction’scredit_card_number
,credit_card_expiration_date
, andresult
can be updated. This method will also change the transaction’supdated_at
attribute to the current time.delete(id)
- delete theTransaction
instance with the correspondingid
The data can be found in data/transactions.csv
. An instance is created and used like this:
sales_engine = SalesEngine.from_csv(:transactions => "./data/transactions.csv")
transaction = sales_engine.transactions.find_by_id(6)
# => <Transaction... @id=6>
Customer
A Customer represents a person who has made one or more purchases in our system.
The customer
instance has the following data accessible:
id
- returns the integer idfirst_name
- returns the first namelast_name
- returns the last namecreated_at
- returns aTime
instance for the date the customer was first createdupdated_at
- returns aTime
instance for the date the customer was last modified
We create an instance like this:
c = Customer.new({
:id => 6,
:first_name => "Joan",
:last_name => "Clarke",
:created_at => Time.now,
:updated_at => Time.now
})
# => <Customer...>
CustomerRepository
The CustomerRepository
is responsible for holding and searching our Customer
instances. It offers the following methods:
all
- returns an array of all knownCustomer
instancesfind_by_id
- returns eithernil
or an instance ofCustomer
with a matching IDfind_all_by_first_name
- returns either[]
or one or more matches which have a first name matching the substring fragment suppliedfind_all_by_last_name
- returns either[]
or one or more matches which have a last name matching the substring fragment suppliedcreate(attributes)
- create a newCustomer
instance with the providedattributes
. The newCustomer
’s id should be the current highestCustomer
id plus 1.update(id, attribute)
- update theCustomer
instance with the correspondingid
with the providedattributes
. Only the customer’sfirst_name
andlast_name
can be updated. This method will also change the customer’supdated_at
attribute to the current time.delete(id)
- delete theCustomer
instance with the correspondingid
The data can be found in data/customers.csv
so the instance is created and used like this:
sales_engine = SalesEngine.from_csv(:customers => "./data/customers.csv")
customer = sales_engine.customers.find_by_id(6)
# => <Customer... @id=6>
Business Intelligence
Assuming we have a SalesEngine
instance called sales_engine
, let’s initialize a SalesAnalyst
like this:
sales_analyst = sales_engine.analyst
sales_analyst.invoice_paid_in_full?(invoice_id)
returnstrue
if theInvoice
with the corresponding id is paid in fullsales_analyst.invoice_total(invoice_id)
returns the total $ amount of theInvoice
with the corresponding id.
Notes:
- Failed charges should never be counted in revenue totals or statistics.
- An invoice is considered paid in full if it has a successful transaction.