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.

Advertisements

Pages: 1 2

4 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
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: