Newbie learning OOP 2nd ?

L

LenS

I have coded this little program which is a small little tip calculator
program. I am in the process of trying to convert this program to use
OOP. Would appreciate others more experienced in OOP code in how they
might do it.

Would be happy to forward all profits from the sale of the program;-))

#Program name: tipcalc05.py
#This program calculates the dollar amount of a tip on a resturant bill
import datetime
import string

def calc_full_bill():
'''This function is used to get information regarding the bill and
calculate the tax, tip, and total amount of the bill'''
global billamt
global tippct
global taxpct
global totalbill

billamt = raw_input("Please enter the bill amount: ")
tippct = raw_input("Please enter the percent tip: ")
location = getanswers("Are you in Chicago, ")

if location == True:
taxpct = 7.25
else:
taxpct = 6.75

tipamt = calcpct(billamt, tippct)

taxamt = calcpct(billamt, taxpct)

totalbill = float(billamt) + taxamt + tipamt

print "Tip = %.2f Tax = %.2f Total = %.2f" % (tipamt, taxamt,
totalbill)

def group_bill_process():
grpbill = open("grpbill.txt", 'a+')
group = raw_input("Please give the name of the group? ")

for line in grpbill:
print string.split(line, sep=',')

who_paid = raw_input("Who paid the bill? ")
grpdate = datetime.datetime.now()
group_detail = group + ',' + who_paid + ',' + str(totalbill) + ','
+ str(grpdate) + '\n'
grpbill.write(group_detail)

def calc_bill_by_guest():
'''This function allows you to get multiple amounts for any number
of individuals and calculate each individuals share of the tip
tax and bill amount'''

guest = []
netbill = float(billamt)
while True:
gf_name = raw_input("Enter girlfriends name: ")
gf_amt = raw_input("Enter girlfriends amount: ")
guest.append((gf_name, gf_amt))
netbill = netbill - float(gf_amt)
print "Amount remaining %.2f" % netbill
anymore = getanswers("Anymore girlfriends, ")
if anymore == False:
break

#these print lines are here just to show the tuples within a list

print guest
print

for (g, a) in (guest):
gf_tax = calcpct(a, taxpct)
gf_tip = calcpct(a, tippct)
gf_total = float(a) + gf_tax + gf_tip
print "%s Tip = %.2f Tax = %.2f Total = %.2f" % (g, gf_tip,
gf_tax, gf_total)
print

def getanswers(question):
'''This function allows you to print some question looking for
and answer of the form Yes or No'''
answer = raw_input(question + "'Yes' or 'No': ")
print
if answer in ["Yes", "yes", "Y", "y", "OK", "ok", "Ok", "yeh",
"Yeh"]:
return(True)
else:
return(False)

def calcpct(bill, pct):
'''This function calculates the amount base off of some
given percentage'''
amt = float(bill) * float(pct) / 100
return(amt)

while True:
calc_full_bill()

group_bill = getanswers("Is this a group bill? ")
if group_bill == True:
group_bill_process()
else:
split_the_bill = getanswers("Do you want to equally spilt the
Bill, ")
if split_the_bill == True:
no_of_dinners = raw_input("Split between how many people?
")
print "Each person owes %.2f" % (float(totalbill) /
float(no_of_dinners))
else:
calc_bill_by_guest()

runagain = getanswers("Do you want to run again, ")

if runagain == False:
break


PS If there is somewhere else I should be posting this kind of thing
let me know otherwise thanks everyone for your help.

Len Sumnler
 
W

wittempj

I would split your code such that the Q&A is seperated from the
calculations, and I would model the bill something like :

class allbills(object):
def __init__(self, bill, tip):
self._bill = bill
self._tip = tip

def gettip(self):
return self._tip / 100.0
tip = property(gettip)

def gettotal(self):
return self._bill * (1 + self.tax + self.tip)
total = property(gettotal)

def __repr__(self):
return 'tip = %.2f, tax = %.2f, total = %.2f ' % \
(self.tip, self.tax, self.total)

class usbill(allbills):
def __init__(self, bill, tip):
super(usbill, self).__init__(bill, tip)

def gettax(self):
return 0.0675
tax = property(gettax)

class chicago_bill(allbills):
def __init__(self, bill, tip):
super(chicago_bill, self).__init__(bill, tip)

def gettax(self):
return 0.0725
tax = property(gettax)
 
L

LenS

Thank you for your suggestion and especially your time. I will study
your code:)

Len Sumnler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top