Write a cash register class. It should have the following methods: AddTransactio
ID: 2246619 • Letter: W
Question
Write a cash register class. It should have the following methods:
AddTransaction(amount) – add an amount to a running total and increments the number of transactions
TransactionCount( ) – returns the number of transactions received
Total( ) – returns the sum of the amounts of the transactions
ResetTransactions( ) – sets the total amount and transaction count to 0
Register Count( ) – returns the number of cash registers created
Next, write an application that instantiates at least two cash register objects to prove that your class works.
P.S.: Don't worry about storing each individual transaction, and we are dealing only with totals. No need to handle individual orders/items per transaction.
Explanation / Answer
Since no language preference is mentioned the code has been written in Python. The logic will be the same in other languages as well.
class CashRegister:
totalAmount = 0
totalTransactions = 0
totalRegisters = 0
def __init__(self):
CashRegister.totalRegisters += 1
def AddTransaction(self,amount):
self.totalAmount = self.totalAmount + amount
self.totalTransactions = self.totalTransactions + 1
def TransactionCount(self):
return self.totalTransactions
def Total(self):
return self.totalAmount
def ResetTransactions(self):
self.totalAmount=0
self.totalTransactions=0
def RegisterCount(self):
return self.totalRegisters
register1 = CashRegister()
register1.AddTransaction(100)
register1.AddTransaction(400)
print("Total transaction amount in register1: ",register1.Total())
print("Total transactions in register1: ",register1.TransactionCount())
register1.ResetTransactions()
print("Total transactions in register1 after resetting: ",register1.TransactionCount())
register2 = CashRegister()
print("Total register counts: ",register2.RegisterCount())
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.