Eiffel "Gotcha" #10 - discounts 
Author Message
 Eiffel "Gotcha" #10 - discounts

A business offers a discount to certain customers. On some products, the
discount is 10% of the gross price; on other products the discount is
15% of the net price.

There is a "discount" column on the invoices, in which the discount is
printed by this code:

  if discount_type.is_equal("gross") then
    print("10%GROSS")
  else
    print("15%NET  ")
  end

Despite the use of a monospace font and the two extra spaces after "NET", the
invoice layout is messed up and the preprinted stationery is wasted every time
a customer purchases a product with a "net" discount. Why?

Answer tomorrow.

(The "Gotcha" series is archived at http://www.*-*-*.com/ )

Regards,
Roger
--
--
-- Roger Browne, 6 Bambers Walk, Wesham, PR4 3DG, UK | Ph 01772-687525
-- Everything Eiffel: http://www.*-*-*.com/ | +44-1772-687525



Mon, 08 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - discounts

Quote:
>     print("15%NET  ")

                   ^
here.  %N is a newline.  ETL page 422.  So how do you print what you really
want?

Jim



Mon, 08 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - discounts



Quote:
>A business offers a discount to certain customers. On some products, the
>discount is 10% of the gross price; on other products the discount is
>15% of the net price.

>There is a "discount" column on the invoices, in which the discount is
>printed by this code:

>  if discount_type.is_equal("gross") then
>    print("10%GROSS")
>  else
>    print("15%NET  ")
>  end

>Despite the use of a monospace font and the two extra spaces after "NET", the
>invoice layout is messed up and the preprinted stationery is wasted every
>time a customer purchases a product with a "net" discount. Why?

The %N is the newline character for Eiffel strings.  The corrected print
line would be:
        print("15% NET ")
or perhaps:
        print("15%%NET  ")

Does anyone else besides me like the C escapes better?  I think they
stick out better in program text.

The Gotcha series has been cool, thanks Roger.

James Graves



Mon, 08 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - discounts

Quote:
> A business offers a discount to certain customers. On some products, the
> discount is 10% of the gross price; on other products the discount is
> 15% of the net price.

> There is a "discount" column on the invoices, in which the discount is
> printed by this code:

>   if discount_type.is_equal("gross") then
>     print("10%GROSS")
>   else
>     print("15%NET  ")
>   end

> Despite the use of a monospace font and the two extra spaces after "NET",
> the invoice layout is messed up and the preprinted stationery is wasted
> every time a customer purchases a product with a "net" discount. Why?

As others have already pointed out, '%' is an escape character inside
Eiffel strings. The sequence "%N" is replaced by a newline, causing the rest
of the page to be one line out of step.

The full list of escape codes is at
http://www.totalweb.co.uk/gustave/ref/syntax/char.htm

Quote:

> So how do you print what you really want?

This won't work:

  print("15%")
  print("NET  ")

..because   %"   is the escape code for a double-quote within a string, and
therefore the string on the first line will never be terminated.

This will work:

  print("15%%NET  ")

..because %% is the escape code for a normal percent character.

Quote:

> Does anyone else besides me like the C escapes better?  I think they
> stick out better in program text.

Early versions of Eiffel used the backslash as C does. And C uses the
percent character as a kind of escape within printf strings.

Personally I prefer the Delphi approach. Delphi has no escapes within
the string (except for the quote mark itself). So, this Delphi statement:

  write('15%NET  ');

..does exactly what it says - no gotcha. To include a newline within
a Delphi string you actually write the escape code "#13" _outside_ the
quotes. For example, to write "15%NET" on _two_ lines, you would write
it like this:

  Delphi: write("15%"#13"NET")  -- or --  write("15%" #13 "NET")

  Eiffel: print("15%%%NNET")

Regards,
Roger
--
--
-- Roger Browne, 6 Bambers Walk, Wesham, PR4 3DG, UK | Ph 01772-687525
-- Everything Eiffel: http://www.eiffel.demon.co.uk/ | +44-1772-687525



Tue, 09 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - discounts


Quote:
>  if discount_type.is_equal("gross") then
>    print("10%GROSS")
>  else
>    print("15%NET  ")
>  end
>Despite the use of a monospace font and the two extra spaces after "NET", the
>invoice layout is messed up and the preprinted stationery is wasted every time
>a customer purchases a product with a "net" discount. Why?

The second one is printed as

15
NET__

(where _ is a space).

It's got a newline in the middle.  %G might mean something, too, but I
don't have a book handy.

That was too easy (or I've missed something obvious and now look like a
fool)!  It seems to me that you might be running out of Gotchas for
Eiffel.  Does anyone fancy starting a list for C++?  :-)

Simon Willcocks



Tue, 09 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - discounts

[...]

Quote:
> Does anyone else besides me like the C escapes better?  I think they
> stick out better in program text.

Until you start putting Windows style paths in your C programs.
I made this error yesterday:

                foo ("c:\jdk1.1.1\lib\classes.zip");

and got compilation warnings (which I ignored at first) and
I was suprized that the program didn't work!

...richie  

-


*          Home page:   http://www.netlabs.net/hp/richieb          *
*        "Fight software piracy, use free software!" (me)          *
*        (Remove XYZZY  from my address before replying)           *



Tue, 09 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - discounts

...

Quote:
> That was too easy (or I've missed something obvious and now look like a
> fool)!  It seems to me that you might be running out of Gotchas for
> Eiffel.  Does anyone fancy starting a list for C++?  :-)

Unfortunately, it would be a little like War and Peace. However,
in comp.lang.c++.moderated there has been a very similar set of postings
in the same style. One a week.

--

Nick



Tue, 09 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - discounts

Quote:

> That was too easy...

But Simon, you've been familiar with Eiffel for at least three years - did you
never get caught by that "Gotcha" when you were learning? I certainly did.

[Aside: In the UK the law requires that interest rates for loans are quoted as
an "Annualised percentage rate", usually written "%APR". So, if you ever

was generated by a program written in Eiffel :-]

Quote:
> It seems to me that you might be running out of Gotchas for
> Eiffel.

Not for a while, yet. I like to mix easy and {*filter*}es. "Once functions"
provide a particularly rich source for the latter, and I haven't even
touched the "select" keyword yet!

The hard bit is finding "Gotchas" that can be expressed in just a few
paragraphs.

Regards,
Roger
--
--
-- Roger Browne, 6 Bambers Walk, Wesham, PR4 3DG, UK | Ph 01772-687525
-- Everything Eiffel: http://www.*-*-*.com/ | +44-1772-687525



Tue, 09 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - discounts



Quote:
> Not for a while, yet. I like to mix easy and {*filter*}es. "Once functions"
> provide a particularly rich source for the latter, and I haven't even
> touched the "select" keyword yet!

I was wondering when you'd get around to those...

(They should show how rusty I am).
--

Aldur Systems Ltd, UK



Tue, 09 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - discounts

:> That was too easy (or I've missed something obvious and now look like a
:> fool)!  It seems to me that you might be running out of Gotchas for
:> Eiffel.  Does anyone fancy starting a list for C++?  :-)
:
:Unfortunately, it would be a little like War and Peace. However,
:in comp.lang.c++.moderated there has been a very similar set of postings
:in the same style. One a week.

You see, in C++ it's politically incorrect to call them "gotchas"---
"powerful idioms", "tricks of the C++ gurus" is more like it, and you can sell
whole books about them, and products fixing them.

How do you tell the difference between a "gotcha" and a "it's a feature"?

:Nick

--
Matthew B. Kennel/Institute for Nonlinear Science, UCSD/
Don't blame me, I voted for Emperor Mollari.



Sun, 14 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - discounts


Quote:



> :> That was too easy (or I've missed something obvious and now look like a
> :> fool)!  It seems to me that you might be running out of Gotchas for
> :> Eiffel.  Does anyone fancy starting a list for C++?  :-)
> :
> :Unfortunately, it would be a little like War and Peace. However,
> :in comp.lang.c++.moderated there has been a very similar set of postings
> :in the same style. One a week.

> You see, in C++ it's politically incorrect to call them "gotchas"---
> "powerful idioms", "tricks of the C++ gurus" is more like it, and you can sell
> whole books about them, and products fixing them.

> How do you tell the difference between a "gotcha" and a "it's a feature"?

Very simple, a 'gotcha' is a 'feature' that causes you problems! ;-)

So far all the 'gotchas' that Roger has posted have been relativly
straight forward. Once functions will provide a little more fun, which
I understand we have to look forward too.

Coming back to C++. One of the noticable difference I have found between
C++ and Eiffel is that there are not as many Gotchas as C++. Witness the
books by Scott Meyers. Super Lints that check classes. The equivalent
postings to Roger's by Herb Sutter in C comp.lang.c++.moderated. Herb's
problems are difficult ones to solve. You need to be a very experienced
C++ programmer to be a safe C++ programmer. It is not the same with
Eiffel.

--

Nick



Mon, 15 Nov 1999 03:00:00 GMT  
 Eiffel "Gotcha" #10 - discounts

Naturally. That's one of the reasons why Bertrand (et al) designed Eiffel.
Eiffel is very small and as safe as a modern programming language can be.
I've always been impressed by its conceptual integrity and the clean feel
it has. I've always been disappointed that it is not more widely used.

--

Aldur Systems Ltd, UK



Quote:
> Coming back to C++. One of the noticable difference I have found between
> C++ and Eiffel is that there are not as many Gotchas as C++. Witness the
> books by Scott Meyers. Super Lints that check classes. The equivalent
> postings to Roger's by Herb Sutter in C comp.lang.c++.moderated. Herb's
> problems are difficult ones to solve. You need to be a very experienced
> C++ programmer to be a safe C++ programmer. It is not the same with
> Eiffel.



Mon, 15 Nov 1999 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Eiffel "Gotcha" #10 - cleaning up

2. Eiffel "Gotcha" #12 - onceness and invariants

3. Eiffel "Gotcha" #9 - postconditions

4. Eiffel "Gotcha" #7 - redundant parentheses

5. RH6.1 coredump: float("10.01")

6. A question about "cryptoaritmetical puzzle "(in swi prolog 5.0.10)

7. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

8. Lindersoft WebStore officially launched - get 10% discount!

9. 10% Discount on Component Design by Example Book

10. BEGIN{want[]={"s1o", "s2o", "s2q", "s3q"}

11. Building a "record" in Eiffel

12. Obvious Fake (was "Record in Eiffel")

 

 
Powered by phpBB® Forum Software