Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

COSC 210 – Object Oriented and GUI Programming Assignment 6 The objectives of th

ID: 3814077 • Letter: C

Question

COSC 210 – Object Oriented and GUI Programming
                      Assignment 6

The objectives of this assignment are to:

- Gain an understanding of inheritance.
- Gain further understanding of input and output of text files.
- Develop and test a small Java program which is composed of several classes that apply the concepts of objectives 1 and 2.
- Continue to practice good programming techniques.



COSC 210 – COSC 210 – Object Oriented and GUI Programming
                  Assignment 6 Problem Statement

A bank provides two types of accounts; checking account and savings account. Both types of accounts have an account number and a balance. Both accounts support deposits and withdrawals.   However, for a savings account, a withdrawal can never exceed the balance. An attempt to withdraw an amount that exceeds the balance of a savings account will withdraw just the balance. On the other hand, a checking account can go into overdraft state. During the course of a month, many transactions (e.g. deposits and withdrawals) can be performed against an account. These are recorded and the balance of the account is updated. All transactions record a description; “Deposit” for deposits, “Withdrawal” for withdrawals against savings accounts, and “Check” for withdrawals against checking accounts. At the end of the month checking accounts incur a 10 cent per check fee which is recorded as a single transaction with a description “Check fee” which is recorded at the end of the month. In addition, a checking account incurs a $25 overdraft fee for each withdrawal which results in a balance less than zero. Each overdraft fees will be recorded as a transaction with description “Overdraft fees”. Savings accounts receive a 0.5% interest. Interest, if applicable, is recorded as a transaction with description “Interest”. After accounting for end of month processing, statements are printed.

*** Your task is to write a program to: i) post a set of transactions to set of accounts, ii) perform end of month processing applying interest and check fees, iii) print statements, and iv) save the updated accounts to a new file.

Input:
Account.txt file - a comma separated text file containing the fields:
Account type - either ‘C’ or ‘S’ for checking or savings
Account number - 8 digits
Account balance - up to 8 numeric digits with 2 decimals

Trans.txt file - a comma separated text file containing the fields:
Account number - 8 digits
Date - in the format of “MM/dd/yyyy”
Transaction type - either ‘D’ or ‘W’ for deposit or withdrawal.
Amount - up to 8 numeric digits with 2 decimals

Output:

1) Printed statements according to the following sample:

Account: 34385543
Type:       Checking
Balance: $ 602.00

Date          Transaction    Amount      Balance
9/02/2009     Deposit        $ 500.00   $ 1000.00
9/05/2009     Check          $ 1200.00   $ -200.00
9/07/2009     Deposit        $ 500.00   $ 300.00
9/15/2009     Check          $ 400.00   $ -100.00
9/25/2009     Deposit        $  500.00   $ 400.00
9/30/2009     Check fees     $     .20   $ 299.90
9/30/2009     Overdraft fees $ 50.00   $ 349.90

Account: 34385577
Type:     Savings
Balance: $ 2241.15

Date          Transaction    Amount      Balance
9/12/2009     Deposit        $ 1000.00   $ 2230.00
9/30/2009     Interest       $   11.15   $ 2241.15

2) ResultAccounts.txt same format at Account.txt

Implementation requirements:

You must use inheritance for the accounts. Do not have any test for account type of account except during input of the account file. Input processing simply creates different types of account according to the account type field. (Hint: provide an abstract method for end of month processing).
You do not need to use inheritance for the transaction record. A transaction record need only have attributes date, description, amount, and balance.
ABIDE by good programming practices. Define javadoc, good formatting, naming conventions, etc. Failing to abide by good programming practices will result in a greater deduction of points.

Here are the values for the transactions and accounts txt files

Trans.txt

https://www.dropbox.com/s/2x8h9nys5dvjrsq/Trans.txt?dl=0

Accounts.txt

https://www.dropbox.com/s/9mxcn7tf3qb8oep/Accounts.txt?dl=0

Explanation / Answer

class Square: def __init__(self, side): self.side = side def calculateArea(self): return self.side**2 class Circle: def __init__(self, radius): self.radius = radius def calculateArea(self): import math return math.pi*(self.radius**2) class BalanceError(Exception): value = "Sorry you only have $%6.2f in your account" class BankAccount: def __init__(self, initialAmount): self.balance = initialAmount print "Account created with balance %5.2f" % self.balance def deposit(self, amount): self.balance = self.balance + amount def withdraw(self, amount): if self.balance >= amount: self.balance = self.balance - amount else: raise BalanceError, BalanceError.value % self.balance def checkBalance(self): return self.balance def transfer(self, amount, account): try: self.withdraw(amount) account.deposit(amount) except BalanceError: print BalanceError.value % self.balance class ChargingAccount(BankAccount): def __init__(self, initialAmount): BankAccount.__init__(self, initialAmount) self.fee = 3 def withdraw(self, amount): BankAccount.withdraw(self, amount+self.fee) from bankaccount import * # First a standard BankAccount a = BankAccount(500) b = BankAccount(200) a.withdraw(100) # a.withdraw(1000) a.transfer(100,b) print "A = ", a.checkBalance() print "B = ", b.checkBalance() # Now an InterestAccount c = InterestAccount(1000) c.deposit(100) print "C = ", c.checkBalance() # Then a ChargingAccount d = ChargingAccount(300) d.deposit(200) print "D = ", d.checkBalance() d.withdraw(50) print "D = ", d.checkBalance() d.transfer(100,a) print "A = ", a.checkBalance() print "D = ", d.checkBalance() # Finally transfer from charging account to the interest one # The charging one should charge and the interest one add # interest print "C = ", c.checkBalance() print "D = ", d.checkBalance() d.transfer(20,c) print "C = ", c.checkBalance() print "D = ", d.checkBalance() from bankaccount import * import time # Create new function to generate unique id numbers def getNextID(): ok = raw_input("Create account[y/n]? ") if ok[0] in 'yY': # check valid input id = time.time() # use current time as basis of ID id = int(id) % 10000 # convert to int and shorten to 4 digits else: id = -1 # which will stop the loop return id # Let's create some accounts and store them in a dictionary accountData = {} # new dictionary while 1: # loop forever id = getNextID() if id == -1: break # break forces an exit from the while loop bal = float(raw_input("Opening Balance? ")) # convert string to float accountData[id] = BankAccount(bal) # use id to create new dictionary entry print "New account created, Number: %04d, Balance %0.2f" % (id,bal) # Now let's access the accounts for id in accountData.keys(): print "%04d %0.2f" % (id,accountData[id].checkBalance()) # and find a particular one # Enter non-number to force exception and end program while 1: id = int(raw_input("Which account number? ")) if id in accountData.keys(): print "Balance = %0.2d" % accountData[id].checkBalance() else: print "Invalid ID" class A: def __init__(self,x,y): self.x = x self.y = y def save(self,fn): f = open(fn,"w") f.write(str(self.x)+ ' ') # convert to a string and add newline f.write(str(self.y)+' ') return f # for child objects to use def restore(self, fn): f = open(fn) self.x = int(f.readline()) # convert back to original type self.y = int(f.readline()) return f class B(A): def __init__(self,x,y,z): A.__init__(self,x,y) self.z = z def save(self,fn): f = A.save(self,fn) # call parent save f.write(str(self.z)+' ') return f # in case further children exist def restore(self, fn): f = A.restore(self,fn) self.z = int(f.readline()) return f # create instances a = A(1,2) b = B(3,4,5) # save the instances a.save('a.txt').close() # remember to close the file b.save('b.txt').close() # retrieve instances newA = A(5,6) newA.restore('a.txt').close() # remember to close the file newB = B(7,8,9) newB.restore('b.txt').close() print "A: ",newA.x,newA.y print "B: ",newB.x,newB.y,newB.z