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.

Pages: 1 2

5 Responses to “The Henry Used Car Dealership”

  1. Vincent Manis said

    HIPO charts are so 1970s. A more modern exercise would require an 11-way multiple-inheritanced class hierarchy.

  2. matthew said

    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

  3. Globules said

    Here’s a Haskell version. Its only trick is that it will prompt repeatedly until you enter a valid amount.

    import Control.Monad (msum)
    import Control.Monad.IO.Class (MonadIO, liftIO)
    import Control.Monad.Trans.Maybe (MaybeT(..), runMaybeT)
    import Data.Decimal (Decimal)
    import Data.Maybe (fromJust)
    import System.IO (hFlush, stdout)
    import Text.Read (readMaybe)
    
    getProfit :: MonadIO m => MaybeT m Decimal
    getProfit = do
      purchPrice <- msum $ repeat $ readValue "Enter purchase price: "
      salePrice  <- msum $ repeat $ readValue "Enter sale price: "
      return (salePrice - purchPrice)
    
    readValue :: (MonadIO m, Read a) => String -> MaybeT m a
    readValue prompt = promptRead prompt >>= maybeRead
    
    promptRead :: MonadIO m => String -> m String
    promptRead prompt = liftIO $ putStr prompt >> hFlush stdout >> getLine
    
    maybeRead :: Monad m => Read a => String -> MaybeT m a
    maybeRead = MaybeT . return  . readMaybe
    
    main :: IO ()
    main = do
      profit <- fromJust <$> runMaybeT getProfit
      print profit
    
    $ ./usedcar
    Enter purchase price: dlkjflj
    Enter purchase price: 12345.67
    Enter sale price: 12567.89
    222.22
    
  4. Daniel said

    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:

    sale price> 20.00
    purchase price> 10.00
    profit: 10.00
    Thanks for using this program
    
  5. Richard A. O'Keefe said

    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.

Leave a comment