Showing posts with label tip calculator. Show all posts
Showing posts with label tip calculator. Show all posts

Sunday, February 10, 2019

Exercises for Programmers: 57 Challenges to Develop Your Coding Skills - Chapter 1 Part g/h

Ok....now it gets tricky.  I'm supposed to turn the tip calculator into a GUI, and then a GUI with a slide.  I could just use C#, but that's pretty week.  I might as well go VB COM (and in case that's offensive, I still have a secret santa program running on VB COM including Outlook integration for almost 15+ years.  I compiled it and just use it...I'm too lazy to update.  If I went digging, I probably have tgXML around as well, my treasure generator using XML in VB COM for AD&D third edition).  So I was thinking I might try pure javascript, but then thought, well, I'll have to pull in bootstrap and this npm live-server thing is cool, but ultimately sort of a waste of my time and disk space.  And as soon as I thought that, I thought, why don't I just use Angular?  And that made me think, that seems so heavy...oh yeah, people use Vue and React. I pondered React, but realized I have a developer on my team who keeps telling me how wonderful it is so, being the obstinate person I am, I used Vue, on JSFiddle so I could use someone's pre-fiddled fun circle slider, and save my work without having to find it locally.

My biggest issue wasn't the basic structure of Vue, it was trying to parseFloat and Math.roundup and get it to work within the framework of Vue without concatenating.

Here it was doing the stupid concatenation instead of math, which really annoyed me, because it wasn't a Vue-specific issue.

Here I fixed it.  You can see where I started to put it into a function instead of inline, which is better, but I was lazy and just proving my point (that I could do it).


You can play with it here...
https://jsfiddle.net/NodToNothing/e6c7amk9/1/

And here's the code using a cheater textarea....

Friday, February 08, 2019

Exercises for Programmers: 57 Challenges to Develop Your Coding Skills - Chapter 1 Part f

Version f...I think that's also 6. Break it out into functions. Some of that was already done, but I finished the job. I guess, if I was being a good programmer, I might have it ask whether the tip should be rounded up or not. And yeah, there's some weird code in a few spots, but fast and functional was my goal, not elegant.
 
import math

#Version 1: basic
#Version 2: round up
#Version 3: next, ONLY enter numbers
#Version 4: ask again for a number - already done!
#Version 5: no negative numbers
#Version 6: use some functions

def inputNumber(message):
  while True:
    try:
       returnValue = float(raw_input(message))
       if returnValue < 0:
           raise ValueError
       else:
           return returnValue
    except ValueError:
       return inputNumber("Not a non-negative value! Try again.")

def grandTotal(tip, bill):
    return str(y + (y * x/100))

def grandTotalRounded(tip, bill):
    return str(y + math.ceil((y * x/100)*100)/100)
    
def tipPrint(tip):
    return str((y * x/100)*100)

def tipRoundedPrint(tip):
    return str(math.ceil((y * x/100)*100))


#print("Enter a tip:")
x = inputNumber("Enter a tip:")
print("Tip is: " + str(x))
#print("Enter a bill/total: ")
y = inputNumber("Enter a bill/total: ")
print("Bill is: " + str(y))

#this won't work because I need a precision factor (cents) in my ceil()
#so use full ints?
print("Tip is: " + tipPrint(x) + " cents") #cents
print("Tip rounded up is: " + tipRoundedPrint(x) + " cents")  #what do I expect?
print("Total is: " + grandTotal(x,y))

#with 3% and 1.85 should be 1.91
print("Total rounded up is: " + grandTotalRounded(y,x))

Sigh...mismatched function name.  If I had a dime for every time...

Enter a tip:3
Tip is: 3.0
Enter a bill/total: 82.55
Bill is: 82.55
Tip is: 247.65 cents
Tip rounded up is: 248.0 cents

Traceback (most recent call last):
  File "C:/Python27/TipCalculator.py", line 45, in 
    print("Total is: " + grandTotal(x,y))
NameError: name 'grandTotal' is not defined
>>> ================================ RESTART ================================
>>> 
Enter a tip:3
Tip is: 3.0
Enter a bill/total: 82.55
Bill is: 82.55
Tip is: 247.65 cents
Tip rounded up is: 248.0 cents
Total is: 85.0265
Total rounded up is: 85.03
>>> ================================ RESTART ================================
>>> 
Enter a tip:-2
Not a non-negative value! Try again.1.8
Tip is: 1.8
Enter a bill/total: 77.25
Bill is: 77.25
Tip is: 139.05 cents
Tip rounded up is: 140.0 cents
Total is: 78.6405
Total rounded up is: 78.65

Thursday, February 07, 2019

Exercises for Programmers: 57 Challenges to Develop Your Coding Skills - Chapter 1 Part d/e

It was exciting to find out I'd already done part d/4, so I jumped right into no negative numbers. That would be a really mean tipping system. I found some other bugs as I went - I spelled negative wrong, the tip info didn't say "cents" so you couldn't be sure what was going on there, and my error handling message wasn't generic enough to cover all cases (versus specific messages, which is annoying for me, although good for the user).
import math

#Version 1: basic
#Version 2: round up
#Version 3: next, ONLY enter numbers
#Version 4: ask again for a number - already done!
#Version 5: no negative numbers

def inputNumber(message):
  while True:
    try:
       returnValue = float(raw_input(message))
       if returnValue < 0:
           raise ValueError
       else:
           return returnValue
    except ValueError:
       return inputNumber("Not a non-negative value! Try again.")


#print("Enter a tip:")
x = inputNumber("Enter a tip:")
print("Tip is: " + str(x))
#print("Enter a bill/total: ")
y = inputNumber("Enter a bill/total: ")
print("Bill is: " + str(y))

#this won't work because I need a precision factor (cents) in my ceil()
#so use full ints?
print("Tip is: " + str((y * x/100)*100) + " cents") #cents
print("Tip rounded up is: " + str(math.ceil((y * x/100)*100)) + " cents")  #what do I expect?
print("Total is: " + str(y + (y * x/100)))

#with 3% and 1.85 should be 1.91
print("Total rounded up is: " + str(y + math.ceil((y * x/100)*100)/100))

I notice that I don't always have the right precision...my money isn't necessarily two decimal spots. I think I know how to fix that with a decimal or I could bite the bullet and use one of the money packages/imports that exists. That might be part 8. I notice I have to flip it to a GUI coming up here soon...I'll have to port this code over to javascript (although I'd be much, much faster with C#).

Enter a tip:-1
Tip is: -1.0
Enter a bill/total: 1.50
Bill is: 1.5
Tip is: -1.5
Tip rounded up is: -1.0
Total is: 1.485
Total rounded up is: 1.49
>>> ================================ RESTART ================================
>>> 
Enter a tip:-1
Not an integer! Try again.-2
Not an integer! Try again.3
Not an integer! Try again.4
Not an integer! Try again.
>>> ================================ RESTART ================================
>>> 
Enter a tip:-2
Not an integer! Try again.3
Tip is: 3.0
Enter a bill/total: 150
Bill is: 150.0
Tip is: 450.0
Tip rounded up is: 450.0
Total is: 154.5
Total rounded up is: 154.5
>>> ================================ RESTART ================================
>>> 
Enter a tip:-3
Not a non-negiative value! Try again.0
Tip is: 0.0
Enter a bill/total: 150
Bill is: 150.0
Tip is: 0.0 cents
Tip rounded up is: 0.0
Total is: 150.0
Total rounded up is: 150.0
>>> ================================ RESTART ================================
>>> 
Enter a tip:-2
Not a non-negative value! Try again.-3
Not a non-negative value! Try again.-4
Not a non-negative value! Try again.3
Tip is: 3.0
Enter a bill/total: 17.25
Bill is: 17.25
Tip is: 51.75 cents
Tip rounded up is: 52.0
Total is: 17.7675
Total rounded up is: 17.77
>>>