If you're preparing for TCS PRA (Project Readiness Assessment), this is one of the most important Python OOP-based questions.
In this article, you’ll find:
- ✅ Full question (as it is)
- ✅ Simple explanation
- ✅ Complete Python solution
TCS PRA Python Question – ATM & Account System (Full Question + Solution)
📄 Full Question (As Given in Exam)
Question 2: Define a class Account with the below attributes: cardNumber of type Int pin of type Int balance of type float (represents existing account balance) withdrawalAmount of type float (represents last withdrawal amount) accountType of Type String Define the required method in the Account class which takes all parameters in the above sequence and sets the value of attributes to parameter values while creating an object of the class. Create a method inside Account class. This method takes a withdrawAmount value as argument and if this amount is less than or equal to the existing account balance then the method calculates the new balance and updates the balance. The method also sets/overwrites the withdrawalAmount attribute of Account class with the new withdrawalAmount value. e.g. If the existing account balance is 60 and the given withdrawalAmount (passed as argument) is 10, then the updated balance should be 50 and updated withdrawalAmount would be 10. Define another class ATM with the below attributes: branch_name of type String accountList of type List having Account objects Define the required method in the ATM class which takes all parameters in the above sequence and sets the value of attributes to parameter values while creating an object of the class. Create another method inside the ATM class. This method takes card number as first argument, pin as the second argument and withdrawalAmount as the third argument. This method should calculate the updated balance using the method created in class Account if the card number and pin entered are valid. If the account with given card number and pin is not found, then the method returns None. Note: In Python None means NULL Object, Accordingly it will display the message "No account Exists" (without the quotes) Create another method inside the ATM class. This method takes an account type as input and filters all Accounts for the given account type and returns a dictionary of cardNumber as key and balance as value in ascending order of their balance. Note: All searches should be case insensitive. If no account of given account type is found display "No match Found" (without the quotes) Instructions to write main section of the code: a. You would require to write the main section completely, hence please follow the below instructions for the same. b. You would require to write the main program which is inline to the "sample input description section" mentioned below and to read the data in the same sequence. c. Create the respective objects (Account and ATM) with the given sequence of arguments as per the requirements defined in the respective classes referring to the below instructions. i. Create a list of Account objects. To create the list, 1. Take a number as input representing the count of Account objects to be created and added to the list. 2. Create an Account object after reading the data related to it i.e. cardNumber, pin, balance, withdrawalAmount, accountType (in this sequence) and add the object to the list of Account objects which will be provided to the ATM object. This point repeats for the number of Account objects (considered in the first line of input, point #c.i.1) to be created. Refer to sample input-outputs for more clarity. ii. Create ATM object by passing the list of Account objects (created in point #c.i) as the argument. d. Next take three input values one after another representing the card number, pin and withdrawalAmount which will be used to validate the account and withdraw amount from it. e. Using the values read in point #d, call the required method of the ATM class to withdraw money from the account and update the balance and withdrawalAmount of the account as required. If the withdrawal was successful, display the cardNumber, updated balance and updated withdrawalAmount of the account in a single line separated by space (refer to sample output below). If None was returned from the method display the relevant message as described in method description. f. Next, read an input value representing the account type to search/filter with. g. Using the value read in point #f, call the method of the ATM class to find the card number and balance of the accounts of the given account type and display the card Numbers and balances returned in the format specified in sample output below. Note, account records (card number and balance) in output should be printed in the same order as they were taken in input. If no account of given type is found relevant message should be displayed as per method description. Note: Balance and withdrawalAmount values in outputs should be printed up to one decimal place. Refer to sample output below for clarity. You may refer to the sample output to have better clarity on the display formats. You can use/refer the below given sample input and output to verify your solution. Input Format for Custom Testing: The 1st input taken in the main section is the number of account objects to be added to the list of Accounts. The next set of inputs are the card number, pin, balance, withdrawal amount and account type for each account taken one after other and is repeated for number of Account objects given in the first line of input. The next three lines of input refer to the card number, pin and withdrawalAmount to call the required method of the ATM class to withdraw money from the account. The last line of input represents the input account type to call the required method of the ATM class to filter all Accounts for the given account type. Sample Input: 2 12345 12 30.0 10.0 salary 45678 98 400.0 200.0 salary 45678 98 100.0 salary Output: 45678 300.0 100.0 12345 30.0 45678 300.0
🧠 Simple Explanation
- 👉 Create Account class to store account details
- 👉 Create ATM class to manage accounts
- 👉 Perform withdrawal using validation
- 👉 Filter accounts by type (case-insensitive)
- 👉 Sort results by balance
💻 Python Solution Code
class Account:
def __init__(self, cardNumber, pin, balance, withdrawalAmount, accountType):
self.cardNumber = cardNumber
self.pin = pin
self.balance = balance
self.withdrawalAmount = withdrawalAmount
self.accountType = accountType
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
self.withdrawalAmount = amount
return self
return None
class ATM:
def __init__(self, branch_name, accountList):
self.branch_name = branch_name
self.accountList = accountList
def withdrawMoney(self, cardNumber, pin, amount):
for acc in self.accountList:
if acc.cardNumber == cardNumber and acc.pin == pin:
return acc.withdraw(amount)
return None
def filterByAccountType(self, accType):
result = {}
for acc in self.accountList:
if acc.accountType.lower() == accType.lower():
result[acc.cardNumber] = acc.balance
if not result:
return None
return dict(sorted(result.items(), key=lambda x: x[1]))
# MAIN PROGRAM
n = int(input())
accounts = []
for _ in range(n):
cardNumber = int(input())
pin = int(input())
balance = float(input())
withdrawalAmount = float(input())
accountType = input()
accounts.append(Account(cardNumber, pin, balance, withdrawalAmount, accountType))
atm = ATM("MainBranch", accounts)
cardNumber = int(input())
pin = int(input())
withdrawAmount = float(input())
res1 = atm.withdrawMoney(cardNumber, pin, withdrawAmount)
if res1:
print(res1.cardNumber, format(res1.balance, ".1f"), format(res1.withdrawalAmount, ".1f"))
else:
print("No account Exists")
accType = input()
res2 = atm.filterByAccountType(accType)
if res2:
for k, v in res2.items():
print(k, format(v, ".1f"))
else:
print("No match Found")
🎯 Key Concepts Covered
- ✔ Object-Oriented Programming (OOP)
- ✔ Classes and Objects
- ✔ List Handling
- ✔ Dictionary Sorting
- ✔ Case-insensitive comparison
🚀 Final Tips
This question is very important for TCS PRA. Practice similar problems to improve your confidence.
Stay connected with JustNK.in for more updates 🔥