Looking for aPython code for below problem. Python version 2.7.3 would be ok. --
ID: 3910985 • Letter: L
Question
Looking for aPython code for below problem. Python version 2.7.3 would be ok.
---------------------------------------------------------------------------------------------
Creative door logistics is a successful name in the e-commerce vertical. The company management wanted to go an extra mile in their service offerings by designing a simple payment information system. The application is menu driven where the customers are capable of listing the pending invoices and make payment for a specific invoice.
The application should have a login page where customers can login with valid username and password. If the customer gives invalid credentials, display “Invalid Username” in the main. If the customer’s login is successful then print “Login successful” in the main, and the menu displays 3 operations as given below:
1)List all pending invoices for Cargo
2)Make payment to Cargo service
3)Exit
For ‘List all pending invoices for Cargo’ Operation:
1)List all the pending invoices of the logged in user
2)For every pending invoice, display the cargo name, invoice number and the balance amount for payment.
For ‘Make payment to Cargo service’ operation:
1)User has to input a pending invoice number. If the number that the user gives is not present in the file, display a message “Invalid Invoice Number” and terminate the application.
2)If the user gives a valid invoice number, display the original balance amount for the given invoice and read the payable amount for the user.
If(balance-payable amount = 0),
i.e., if the balance amount is 0, the invoice amount is fully paid and the invoice should not be displayed in the pending list and its status is changed to completed in the cargo.xml file. Then return “Status Completed”.
If the balance-payable amount is a positive value, the invoice is still pending, so subtract the payable amount given by the user from the original balance amount and store it as balance. Then return “Balance Updated”.
For ‘Exit’ operation, terminate the application.
Note: All terminations should happen only from the main and not from the methods.
Method Description:
Method Name
Arguments
Return Type
Description
validateUsernamePassword()
Users file name(userFile), userName, password
userFile- string
username- string
password- string
String
This function validates the username and password.
Return True, if valid user, else return False.
listPendingInvoice()
Cargo File name(cargoFile), username.
cargoFile- string
username- string
String List
This function will return a list of strings that contains the details of the pending invoices of the logged in user.
Each string contains cargoName, invoiceNo and balance separated by hyphen(-).
makePayment()
Cargo File name(cargoFile), invoiceNo, username, payment.
cargoFile-string
username- string
invoiceNo- integer
payment-string
String
This function has to return “Invalid Invoice Number”, if the invoiceNois not present in the file. Display the original balance for the given invoice number.
This function will update the balance amount.
If the final balance equals 0, return “Status Completed”, else return “Balance updated”.
The changes in the balance should be updated in the cargo.xml also.
Two XML input files are, userpass.xml and cargo.xml
userpass.xml
<userList>
<user>
<username>aradya</username>
<password>adya</password>
</user>
<user>
<username>sumant</username>
<password>sumu</password>
</user>
<user>
<username>karthik</username>
<password>kar</password>
</user>
</userList>
cargo.xml
<cargoList>
<cargo>
<name>Aradya Shetty</name>
<username>aradya</username>
<status>pending</status>
<invoiceNo>201</invoiceNo>
<balance>3000</balance>
</cargo>
<cargo>
<name>Sumant Kumar</name>
<username>sumant</username>
<status>completed</status>
<invoiceNo>202</invoiceNo>
<balance>0</balance>
</cargo>
<cargo>
<name>Karthik Selvan</name>
<username>karthik</username>
<status>pending</status>
<invoiceNo>203</invoiceNo>
<balance>1000</balance>
</cargo>
</cargoList>
Sample input and output 1:
Enter username:
Vikk
Enter password:
abc
Invalid Username
Sample input output 2:
Enter username:
aradya
Enter password:
adya
Login Successful
1.List all pending invoices for Cargo
2.Make payment to Cargo service
3.Exit
Enter your choice:
1
[‘Aradya Shetty – 201 - 3000’]
Sample input output 3:
Enter username:
sumant
Enter password:
sumu
Login Successful
1.List all pending invoices for Cargo
2.Make payment to Cargo service
3.Exit
Enter your choice:
2
Enter Invoice Number:
105
Invalid Invoice Number
Sample input output 4:
Enter username:
aradya
Enter password:
adya
Login Successful
1.List all pending invoices for Cargo
2.Make payment to Cargo service
3.Exit
Enter your choice:
2
Enter Invoice Number:
202
Your balance is: 0
Enter the Payment:
3000
Status Completed
Sample input output 5:
Enter username:
karthik
Enter password:
kar
Login Successful
1.List all pending invoices for Cargo
2.Make payment to Cargo service
3.Exit
Enter your choice:
2
Enter Invoice Number:
203
Your balance is: 1000
Enter the Payment:
300
Balance Updated
--------------------------------------------------------
Thanks in advance.
Method Name
Arguments
Return Type
Description
validateUsernamePassword()
Users file name(userFile), userName, password
userFile- string
username- string
password- string
String
This function validates the username and password.
Return True, if valid user, else return False.
listPendingInvoice()
Cargo File name(cargoFile), username.
cargoFile- string
username- string
String List
This function will return a list of strings that contains the details of the pending invoices of the logged in user.
Each string contains cargoName, invoiceNo and balance separated by hyphen(-).
makePayment()
Cargo File name(cargoFile), invoiceNo, username, payment.
cargoFile-string
username- string
invoiceNo- integer
payment-string
String
This function has to return “Invalid Invoice Number”, if the invoiceNois not present in the file. Display the original balance for the given invoice number.
This function will update the balance amount.
If the final balance equals 0, return “Status Completed”, else return “Balance updated”.
The changes in the balance should be updated in the cargo.xml also.
Explanation / Answer
This is the working Python Code (I have used Python 3, you can convert it to python 2 using the library 3 to 2).
import xml.etree.ElementTree as ET
def validateUsernamePassword(userpass,userName,password):
tree = ET.parse(userpass+".xml")
root = tree.getroot()
for i in range(0,3):
if root[i][0].text==userName and root[i][1].text==password:
return True
return False
def listPendingInvoice(cargoFile,username):
tree = ET.parse(cargoFile+".xml")
root = tree.getroot()
for i in range(0,3):
if root[i][1].text==username:
return root[i][0].text+"-"+root[i][3].text+"-"+root[i][4].text
def makePayment(cargoFile,invoiceNo,username,payment):
tree = ET.parse(cargoFile+".xml")
root = tree.getroot()
for i in range(0,3):
rem_balance=int(root[i][4].text)-int(payment)
root[i][4].text=str(rem_balance)
tree.write("cargo.xml")
if rem_balance==0:
return "Status Completed"
else:
return "Balance updated"
def main():
user=input("Enter username: ")
passw=input("Enter password: ")
if validateUsernamePassword("userpass",user,passw):
print ("Login successful")
print ("1)List all pending invoices for Cargo")
print ("2)Make payment to Cargo service")
print ("Exit")
print ("Enter your choice:")
choice=input()
if choice=="1":
print (listPendingInvoice("cargo",user))
elif choice=="2":
print ("Enter Invoice Number:")
invoice=input()
tree = ET.parse("cargo.xml")
root = tree.getroot()
flag=0
for i in range(0,3):
if root[i][3].text==invoice:
print ("Your balance is: "+ root[i][4].text)
print ("Enter the Payment: ")
payment=input()
print (makePayment("cargo",invoice,user,payment))
else:
flag=flag+1
if (flag==3):
print ("Invalid Invoice Number")
else:
print ("Invalid Username")
if __name__ == '__main__':
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.