The Henry Used Car Dealership
March 15, 2019
This is simple:
(define (henry) (display "Enter the sale price: ") (let ((sale (read))) (display "Enter the purchase price: ") (let ((purch (read))) (display "Profit is ") (display (- sale purch)) (newline) (display "Thanks for using this program.") (newline))))
> (henry) Enter the sale price: 1100 Enter the purchase price: 800 Profit is 300 Thanks for using this program.
You can run the program at https://ideone.com/XSZrYU.
HIPO charts are so 1970s. A more modern exercise would require an 11-way multiple-inheritanced class hierarchy.
It does seem a little old-fashioned. This book seems to be the source: https://books.google.co.uk/books?id=s72HJL9Z-4AC&pg=PT97
Here’s a Haskell version. Its only trick is that it will prompt repeatedly until you enter a valid amount.
Here’s a solution in Python.
from decimal import Decimal def housekeeping(): global sale_price sale_price = Decimal(input('sale price> ')) def detail(): global purchase_price purchase_price = Decimal(input('purchase price> ')) profit = sale_price - purchase_price print('profit: {}'.format(profit)) def end_of_job(): print('Thanks for using this program') if __name__ == '__main__': housekeeping() detail() end_of_job()Example Usage:
In response to @matthew, the book contains such gems as “In Chapter 2, you learned about many features of program modules,
also called methods.” It should come as no surprise that it makes HEAVY use of flowcharts, rather than say Nassi-Shneiderman charts. By the 1970s, we had learned a lot about program design and logic. They’re in the title of the book. But the book does not mention ‘assertion’, ‘precondition’, ‘postcondition’, (data structure) ‘invariant’ or (loop) ‘invariant’. ‘Termination’ is absent from the index, so good luck searching for advice on how to make sure your loops and recursions stop.
As a free speech advocate, I am pleased that such a book can find a publisher in the 21st century. But it makes me very sad that anyone would try to learn programming from such a book.