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 aTimeinstance for the date the invoice item was first createdupdated_at- returns aTimeinstance 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 knownInvoiceIteminstancesfind_by_id- returns eithernilor an instance ofInvoiceItemwith 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 newInvoiceIteminstance with the providedattributes. The newInvoiceItem’s id should be the current highestInvoiceItemid plus 1.update(id, attribute)- update theInvoiceIteminstance with the correspondingidwith the providedattributes. Only the invoice_item’squantityandunit_pricecan be updated. This method will also change the invoice_item’supdated_atattribute to the current time.delete(id)- delete theInvoiceIteminstance 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 aTimeinstance for the date the transaction was first createdupdated_at- returns aTimeinstance 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 knownTransactioninstancesfind_by_id- returns eithernilor an instance ofTransactionwith 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 newTransactioninstance with the providedattributes. The newTransaction’s id should be the current highestTransactionid plus 1.update(id, attribute)- update theTransactioninstance with the correspondingidwith the providedattributes. Only the transaction’scredit_card_number,credit_card_expiration_date, andresultcan be updated. This method will also change the transaction’supdated_atattribute to the current time.delete(id)- delete theTransactioninstance 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 aTimeinstance for the date the customer was first createdupdated_at- returns aTimeinstance 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 knownCustomerinstancesfind_by_id- returns eithernilor an instance ofCustomerwith 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 newCustomerinstance with the providedattributes. The newCustomer’s id should be the current highestCustomerid plus 1.update(id, attribute)- update theCustomerinstance with the correspondingidwith the providedattributes. Only the customer’sfirst_nameandlast_namecan be updated. This method will also change the customer’supdated_atattribute to the current time.delete(id)- delete theCustomerinstance 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)returnstrueif theInvoicewith the corresponding id is paid in fullsales_analyst.invoice_total(invoice_id)returns the total $ amount of theInvoicewith 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.