In: Computer Science
Needs to be done in PYTHON
A. Create a Dollar currency class with two integer attributes and one string attribute, all of which are non-public. The int attributes will represent whole part (or currency note value) and fractional part (or currency coin value) such that 100 fractional parts equals 1 whole part. The string attribute will represent the currency name.
B. Create a CIS22C Dollar derived/inherited class with one additional non-public double attribute to represent the conversion factor from/to US Dollar.
C. In your two currency classes, add public methods for the following:
D. Create a Wallet class with one attribute - an array of two Dollar references / pointers and the following methods to demonstrate polymorphism of the currencies:
E. In your main:
Needs to be done in PYTHON, do the best with the information you have no other info there.
This assignment was to be done in C++ but our teacher allows us to do in Python. Please to the best of your ability to try fufill as many requirements as possible in Python. I am aware this is in C++ but try the best to modify to make it work in Python. Thank you.
# 100 fract = 1 whole
class Dollar:
# Constructors
def _init_(self):
__whole = 0
__fract = 0
__name = ''
def _init_(self, whole, fract, name):
self.__whole = whole
self.__fract = fract
self.__name = name
# Setters and Getters
def set_whole(self, whole):
self.__whole = whole
def set_fractional(self, fractional):
self.__fract = fractional
def set_name(self, name):
self.__name = name
def get_whole(self):
return self.__whole
def get_fractional(self):
return self.__fract
def get_name(self):
return self.__name
# Destructor
def _del_():
print("Destructor called!")
# Adding same currency
def add_sameCurrency(self, node):
fract = self._fract + node._fract
if fract > 99:
wholePart = fract // 100
fract = fract % 100
whole = self._whole + node._whole + wholePart
return whole, fract
# Subtraction same currency
def sub_sameCurrency(self, node):
fract = 0
whole = 0
if self._fract >= node._fract:
fract = self._fract - node._fract
else:
fract = self._fract - node._fract + 100
whole = self.__whole - 1
whole += self._whole - node._whole
return whole, fract
# Checking equality/inequality
def is_equal(self, node):
if self._name == node._name:
if self.__whole == node.whole:
if self._fract == node._fract:
return True
else:
return False
else:
return False
else:
return False
# comapring same currency
def is_greater(self, node):
if self._whole > node._whole:
return True
elif self._whole == node._whole:
if self._fract > node._fract:
return True
else:
return False
else:
return False
def show(self):
print('The amount is: ', self._whole, '.', self.fract, ' ',
self._name)
def C2D2USD(self):
self._fract = 0.74 * self_fract
self._whole = 0.74 * self._whole
if self.__fract > 99:
self._fract = self._fract % 100
self._whole += self._fract // 100
self.__name = 'USD'
return self
# 1000 fract = 1 whole
# 1 USD = 1.36 C2D
# 1 C2D = 0.74 USD
class CIS22C:
# Constructors
def _init_(self):
__whole = 0
__fract = 0
__name = ''
def _init_(self,whole, fract, name):
self.__whole = whole
self.__fract = fract
self.__name = name
# Setters and Getters
def set_whole(self, whole):
self.__whole = whole
def set_fractional(self, fractional):
self.__fract = fractional
def set_name(self, name):
self.__name = name
def get_whole(self):
return self.__whole
def get_fractional(self):
return self.__fract
def get_name(self):
return self.__name
# Destructor
def _del_():
print("Destructor called!")
# Adding same currency
def add_sameCurrency(self, node):
fract = self._fract + node._fract
if fract > 99:
wholePart = fract // 1000
fract = fract % 1000
whole = self._whole + node._whole + wholePart
return whole, fract
# Subtraction same currency
def sub_sameCurrency(self, node):
fract = 0
whole = 0
if self._fract >= node._fract:
fract = self._fract - node._fract
else:
fract = self._fract - node._fract + 1000
whole = self.__whole - 1
whole += self._whole - node._whole
return whole, fract
# Checking equality/inequality
def is_equal(self, node):
if self._name == node._name:
if self.__whole == node.whole:
if self._fract == node._fract:
return True
else:
return False
else:
return False
else:
return False
# comapring same currency
def is_greater(self, node):
if self._whole > node._whole:
return True
elif self._whole == node._whole:
if self._fract > node._fract:
return True
else:
return False
else:
return False
def show(self):
print('The amount is: ', self._whole, '.', self.fract, ' ',
self._name)
def USD2C2D(self):
self._fract = 1.36 * self_fract
self._whole = 1.36 * self._whole
if self.__fract > 999:
self._fract = self._fract % 1000
self._whole += self._fract // 1000
self.__name = 'C2D'
return self
class Wallet:
def _init_(self):
array = [0, 0]
dol = Dollar(0, 0, 'USD')
array[0] = dol
c2d = CIS22C(0, 0, 'C2D')
array[1]= c2d
def _init_(self, whole, fract, name):
array = [0, 0]
if name == 'USD':
dol = Dollar(whole, fract, name)
array[0] = dol
else:
c2d = CIS22C(whole, fract, name)
array[1] = c2d
def _del_(self):
print('The object is deleted!')
def addUSD(self, node):
self[0] = self[0].add_sameCurrency(node)
def addC2D(self, node):
self[1] = self[1].add_sameCurrency(node)
def subUSD(self, node):
self[0] = self[0].sub_sameCurrency(node)
def subC2D(self, node):
self[1] = self[1].sub_sameCurrency(node)
def compareUSD(self, node):
if not self[0].is_equal(node):
if self[0].is_greater(node):
print("Greater")
else:
print("smaller")
else:
print('Equal')
def compareC2D(self, node):
if not self[1].is_equal(node):
if self[1].is_greater(node):
print("Greater")
else:
print("smaller")
else:
print('Equal')
if '_name' == __main_: // This is the main function
wallet = Wallet()
While True:
print("Press 1 to add")
print("Press 2 to subtract")
print("Press 3 to compare")
print("Press 0 to exit")
i = int(input())
if i == 0:
break
if i == 1:
while True:
print("Press 1 for USD")
print("Press 2 to C2D")
print("Press 0 to exit")
k = int(input())
if k == 0:
break
if k == 1:
print("Enter value")
value = input()
value = value.strip().split('.')
value = Dollar(value[0], value[1], 'USD')
wallet.addUSD(value)
if k == 2:
print("Enter value")
value = input()
value = value.strip().split('.')
value = Dollar(value[0], value[1], 'C2D')
wallet.addC2D(value)
if i == 2:
while True:
print("Press 1 for USD")
print("Press 2 to C2D")
print("Press 0 to exit")
k = int(input())
if k == 0:
break
if k == 1:
print("Enter value")
value = input()
value = value.strip().split('.')
value = Dollar(value[0], value[1], 'USD')
wallet.subUSD(value)
if k == 2:
print("Enter value")
value = input()
value = value.strip().split('.')
value = Dollar(value[0], value[1], 'C2D')
wallet.subC2d(value)
if i == 3:
while True:
print("Press 1 for USD")
print("Press 2 to C2D")
print("Press 0 to exit")
k = int(input())
if k == 0:
break
if k == 1:
print("Enter value")
value = input()
value = value.strip().split('.')
value = Dollar(value[0], value[1], 'USD')
wallet.compareUSD(value)
if k == 2:
print("Enter value")
value = input()
value = value.strip().split('.')
value = Dollar(value[0], value[1], 'C2D')
wallet.compareC2d(value)