123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- __author__ = "Inyoung Choung"
- import tkinter
- from tkinter import messagebox
- from impl_interface import ImplInterface
- class Impl(ImplInterface):
-
- global itemtype, brandN1, brandN2
- global price1, price2
- global weight1, weightT1, weight2, weightT2
- global discount1, discountT1, discount2, discountT2
- global newWeightForSecondItem
- global isEmpty
- global isFinalized
- global result
- global list
- global path
- def __init__(self, itemtype, brandN1, brandN2, price1, price2, weight1, weightT1,
- weight2, weightT2, discount1, discountT1, discount2, discountT2):
- """
- This method is a constructor that initializes parameters.
- :param itemtype:
- :param brandN1:
- :param brandN2:
- :param price1:
- :param price2:
- :param weight1:
- :param weightT1:
- :param weight2:
- :param weightT2:
- :param discount1:
- :param discountT1:
- :param discount2:
- :param discountT2:
- """
-
- self.itemtype = itemtype
- self.brandN1 = brandN1
- self.brandN2 = brandN2
- self.price1 = price1
- self.price2 = price2
- self.weight1 = weight1
- self.weightT1 = weightT1
- self.weight2 = weight2
- self.weightT2 = weightT2
- self.discount1 = discount1
- self.discountT1 = discountT1
- self.discount2 = discount2
- self.discountT2 = discountT2
-
- self.validate_emptyfields()
-
- if self.weightT1 != self.weightT2:
-
-
- self.newWeightForSecondItem = self.convert(self.weightT1, self.weightT2, self.weight2)
- else:
-
- self.newWeightForSecondItem = self.weight2
-
- self.calculate()
- def validate_emptyfields(self):
- """
- This function checks any null values.
- :return:
- """
-
- if (self.itemtype is None or self.itemtype == "" or
- self.brandN1 is None or self.brandN1 == "" or
- self.brandN2 is None or self.brandN2 == "" or
- self.price1 is None or self.price1 == "" or
- self. price2 is None or self.price2 == "" or
- self.weight1 is None or self.weight1 == "" or
- self.weightT1 is None or self.weightT1 == "" or
- self. weight2 is None or self.weight2 == "" or
- self.weightT2 is None or self.weightT2 == "" or
- self.discount1 is None or self.discount1 == "" or
- self.discountT1 is None or self.discountT1 == "" or
- self.discount2 is None or self.discount2 == "" or
- self.discountT2 is None or self.discountT2 == ""):
-
- self.isEmpty = True
-
- self.display_message(None)
- else:
-
- self.isEmpty = False
- def convert(self, weightT1, weightT2, weight2):
- """
- This function converts the second item weight type to the first weight item type.
- :param weightT1:
- :param weightT2:
- :param weight2:
- :return: self.weight2
- """
- if weightT1 == "lb":
- if weightT2 == "kg":
- self.weight2 = float(weight2) * 2.20462
- elif weightT2 == "g":
- self.weight2 = float(weight2) * 0.002205
- elif weightT1 == "kg":
- if weightT2 == "lb":
- self.weight2 = float(weight2) * 0.453592
- elif weightT2 == "g":
- self.weight2 = float(weight2) * 0.001
- elif weightT1 == "g":
- if weightT2 == "kg":
- self.weight2 = float(weight2) * 1000
- elif weightT2 == "lb":
- self.weight2 = float(weight2) * 453.592
-
- return self.weight2
- def calculate(self):
- """
- This function calculates to see which item is cheaper by the unit weight.
- :return: self.result
- """
-
- if self.discountT1 == "percentage":
- finalPrice1 = float(float(self.price1) - (float(self.price1) * (float(self.discount1) * float(0.01))) / float(self.weight1))
- elif self.discountT1 == "dollar":
- finalPrice1 = float((float(self.price1) - float(self.discount1)) / float(self.weight1))
-
- if self.discountT2 == "percentage":
- finalPrice2 = float((float(self.price2) - (float(self.price2) * (float(self.discount2) * float(0.01)))) / float(self.newWeightForSecondItem))
- elif self.discountT2 == "dollar":
- finalPrice2 = float((float(self.price2) - (float(self.discount2))) / float(self.newWeightForSecondItem))
-
- if finalPrice1 > finalPrice2:
- priceDiff = float(round(finalPrice1 - finalPrice2, 3))
- print(self.brandN1 + str(" is cheaper by "), priceDiff, str(" cents per unit weight "))
- self.result = str(self.brandN1 + str(" is cheaper by ") + str(priceDiff) + str(" cents per unit weight "))
- elif finalPrice1 < finalPrice2:
- priceDiff = float(round(finalPrice2 - finalPrice1, 3))
- print(self.brandN2 + str(" is cheaper by "), priceDiff, str(" cents per unit weight "))
- self.result = str(self.brandN2 + str(" is cheaper by ") + str(priceDiff) + str(" cents per unit weight "))
- else:
- self.result = str("The price is equal so get anything!")
-
- self.isFinalized = True
-
- self.display_message(None)
- return self.result
- def get_result(self):
- """
- This function returns String; result.
- :return: self.result
- """
- return self.result
- def display_message(self, message):
- """
- This function is to check which message is to display to user based on boolean variables.
- :param message:
- :return: message
- """
- if (message is None):
- if self.isEmpty is True:
-
- root = tkinter.Tk()
- root.withdraw()
-
- messagebox.showerror("Alert - Empty fields", "Please fill out all the forms.")
-
- self.isEmpty = False
- elif self.isFinalized is True:
-
- root = tkinter.Tk()
- root.withdraw()
-
- messagebox.showinfo("Calculation Result", self.result)
- else:
-
- root = tkinter.Tk()
- root.withdraw()
-
- messagebox.showinfo("Calculation Result", message)
- return message
- def sort(self, list):
- """
- This function gets list, sort it and returns that list.
- :param list:
- :return: list
- """
-
- list.sort()
-
- print("This is a sorted brand_name list by the alphabetical order.")
- print(list)
- return list
- def writefile(self, path, stuff_to_print):
- """
- TFile I/O - prints a sorted list of grocery items.
- :param path:
- :param stuff_to_print:
- :return:
- """
- if path is None :
-
- self.path = "C:\In-young Choung\Computer Programming\Self Programming Files\grocerycalc\itemlist.txt"
-
- f = open(self.path, "w+")
-
- for i in stuff_to_print :
- f.write(i)
- else :
- self.path = path
-
- f = open(self.path, "w+")
-
- f.write(stuff_to_print)
- def readfile(self):
- """
- read file that is created and pass filepath as a parameter.
- :return:
- """
-
- f = open(self.path, "r")
-
- if f.mode == 'r':
-
- contents = f.read()
-
- print("This is a list from reading a file.")
-
- for i in contents:
- print(i)
- else :
- print("wrong mode in readfile function.")
|