[Solved] XML Parsing Problem with Python

XML Parsing: Create class CreditCard with the following abstract method:

def credit_points(self)

The above abstract method derived in every child class, and print the respective credit points for a selected credit card.

Create four classes VisaMasterPlatinumTitanium.
Override Abstract Method of the base class i.e CreditCard.

Create a program and write the main block to test the above class. 
Print the menu of the Credit Card list, assume that all the credit cards are for HDFC bank. Then select your choice from the defined menu, and print the credit points
for the selected credit cards, after reading the XML file (“creditCard.xml”).

Note:
1) Assume that all the credit points on spend of every 200 Rs.
2) XML file name= “creditCard.xml”.

Note:
The XML file format:
<CreditCard>
    <card>
        <name>creditCard1</name>    
        <creditPoints>2</creditPoints>
    </card>
    <card>
        <name>creditCard1</name>
            <creditPoints>7</creditPoints>
        </card>
<CreditCard>


Input and Output Format:
Refer to sample input and output for formatting specifications. 
All text in bold corresponds to the input and the rest corresponds to output.  

Sample Input and Output 1:
HDFC Credit Cards:
1.Visa
2.Master
3.Platinum
4.Titanium
Enter your card choice:
1
5 Reward Points on every Rs. 200 spent for VISA Credit Card

Sample Input and Output  2: 
HDFC Credit Cards:
1.Visa
2.Master
3.Platinum
4.Titanium
Enter your card choice:
8
Wrong Choice

Solution

from abc import abstractmethod, ABCMeta
import xml.etree.ElementTree as ET

#sauravhathi
tree = ET.parse('creditCard.xml')
root = tree.getroot()


class CreditCard:

    @abstractmethod
    def creditPoints(self):
        # fill your code
        pass

class Visa(CreditCard):
    def creditPoints(self):
        # fill your code
        for card in root.findall('card'):
            if card.find('name').text == 'Visa':
                print("{} Reward Points on every Rs. 200 spent for VISA Credit Card".format(card.find('creditPoints').text))

class Master(CreditCard):
    def creditPoints(self):
        # fill your code
        for card in root.findall('card'):
            if card.find('name').text == 'Master':
                print("{} Reward Points on every Rs. 200 spent for Master Credit Card".format(card.find('creditPoints').text))

class Platinum(CreditCard):
    def creditPoints(self):
        # fill your code
        for card in root.findall('card'):
            if card.find('name').text == 'Platinum':
                print("{} Reward Points on every Rs. 200 spent for Platinum Credit Card".format(card.find('creditPoints').text))


class Titanium(CreditCard):
    def creditPoints(self):
        # fill your code
        for card in root.findall('card'):
            if card.find('name').text == 'Titanium':
                print("{} Reward Points on every Rs. 200 spent for Titanium Credit Card".format(card.find('creditPoints').text))

        # Main
print("HDFC Credit Cards:")
print("1.Visa\n2.Master\n3.Platinum\n4.Titanium")
ch = int(input("Enter your card choice:\n"))
# fill your code
if ch == 1:
    visa = Visa()
    visa.creditPoints()
elif ch == 2:
    master = Master()
    master.creditPoints()
elif ch == 3:
    platinum = Platinum()
    platinum.creditPoints()
elif ch == 4:
    titanium = Titanium()
    titanium.creditPoints()
else:
    print("Wrong Choice")

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saransh Saurav

Saransh Saurav

Articles: 67

Leave a Reply

Your email address will not be published. Required fields are marked *