I am not sure how properly use the classes correctly that he is asking for (not static
Question:
I am not sure how properly use the classes correctly that he is asking for (not static method)
I have attached my previous code. Thank you for your help.
You are going to enhance the prior assignment by doing the following.
1) Create class that contains all prior functions, you must convert the functions to methods of that class (do not create static methods, see the lesson for the correct way to create class)
2) Test the application(You can create own application to demonstrate the usage of the class library)
main
# Importing class library
# Importing modules from Mylib file
from Mylib import MyOperationClass
# Loop to simulate menu options
while True:
option = MyOperationClass.getMenuOption()
if option == '0':
print("Thanks for using our calculator")
break
elif option in ['1', '2', '3', '4', '5', '6']:
while True:
try:
lr = int(input("Enter lower range:"))
hr = int(input("Enter higher range:"))
if lr >= hr:
print("Lower range should be less than Higher range. Try again")
continue
else:
break
except ValueError:
print("Please enter numbers only")
continue
while True:
try:
num1 = float(input("Enter first number:"))
num2 = float(input("Enter second number:"))
break
except ValueError:
print("Please enter numbers only")
continue
if MyOperationClass.inRange(lr, hr, num1, num2) == True:
if option == '1':
print("\nThe result of", num1, "+", num2, "=", MyOperationClass.addition(num1, num2))
elif option == '2':
print("\nThe result of", num1, "-", num2, "=", MyOperationClass.subtraction(num1, num2))
elif option == '3':
print("\nThe result of", num1, "*", num2, "=", MyOperationClass.multiplication(num1, num2))
elif option == '4':
print("\nThe result of", num1, "/", num2, "=", MyOperationClass.division(num1, num2))
elif option == '6':
res = MyOperationClass.allInOne(num1, num2)
print()
print(num1, '+', num2, "=", res['add'])
print(num1, '-', num2, "=", res['sub'])
print(num1, '*', num2, "=", res['mult'])
print(num1, '/', num2, "=", res['div'])
elif option == '7':
res = MyOperationClass.allInOne(num1, num2)
wrfile.writeToFile(num1, num2, res)
elif option == '5':
string = input("Enter format (Num1,Num2,operator): ")
print("Result:", MyOperationClass.scalc(string))
elif option == '8':
wrfile.readFromFile()
else:
print("Invalid selection. Try gain")
print()
Mylib
# Class to perform mathematical operations
class MyOperationClass:
# Method to add to numbers
def addition(num1, num2):
return num1 + num2
# Method to get difference between two numbers
def subtraction(num1, num2):
return num1 - num2
# Method to get product of two numbers
def multiplication(num1, num2):
return num1 * num2
# Method to divide two numbers
def division(num1, num2):
# Exception will check the divide by zero exception
try:
div = num1 / num2
return div
except ZeroDivisionError:
return "Cannot divide by zero!"
# Method to check numbers entered by the user
# are in given range or not
def inRange(lr, hr, num1, num2):
if (lr < num1 < hr) and (lr < num2 < hr):
return True
else:
print("The input values are outside the input ranges. Try again")
# Method to perform special calculation
def scalc(p1):
splits = p1.split(",")
# Parse the first and second number
num1 = float(splits[0])
num2 = float(splits[1])
# Parse the operator
operator = splits[2]
if operator == "+":
return MyOperationClass.addition(num1, num2)
elif operator == "-":
return MyOperationClass.subtraction(num1, num2)
elif operator == "*":
return MyOperationClass.multiplication(num1, num2)
elif operator == "/":
return MyOperationClass.division(num1, num2)
else:
return "Invalid operator"
# Method to perform all basic four mathematical operations
def allInOne(n1, n2):
result = dict()
result["add"] = MyOperationClass.addition(n1, n2)
result["sub"] = MyOperationClass.subtraction(n1, n2)
result["mult"] = MyOperationClass.multiplication(n1, n2)
result["div"] = MyOperationClass.division(n1, n2)
return result
# Method to display menu opetions and get user option
def getMenuOption():
menu_options = ["1. Add two numbers",
"2. Subtract two numbers",
"3. Multiply two numbers",
"4. Divide two numbers",
"5. Special calculation",
"6. All in one",
"7. Write to file",
"8. Read from file",
"0. Exit"
]
for menu in menu_options:
print(menu)
option = input("\nSelect option: ")
return option
# Class to perform read and write operations
class wrfile:
# Method to write into file
def writeToFile(num1, num2, dictionary):
filename = input("Enter text filename to write: ")
File = open(filename, 'w')
File.write(str(num1) + ' + ' + str(num2) + " = " + str(dictionary['add']) + '\n')
File.write(str(num1) + ' - ' + str(num2) + " = " + str(dictionary['sub']) + '\n')
File.write(str(num1) + ' * ' + str(num2) + " = " + str(dictionary['mult']) + '\n')
File.write(str(num1) + ' / ' + str(num2) + " = " + str(dictionary['div']) + '\n')
File.close()
print("Successfully written!")
# Method to get data from file
def readFromFile():
filename = input("Enter text filename to read: ")
try:
File = open(filename, 'r')
except FileNotFoundError:
print(filename, "not found!")
else:
for line in File:
line = line.strip()
print(line)
File.close()
Fundamentals of Cost Accounting
ISBN: 978-0077398194
3rd Edition
Authors: William Lanen, Shannon Anderson, Michael Maher