OneCompiler

transactions

129

Htmlfile

<!DOCTYPEhtml> <html> <head> <titl e>VoterDetail s</titl e> <scriptsrc=https: //ajax. googleapis. com/ajax/li bs/jquery/3. 5.1/jquery. min.js></scri pt> </head> <body> <h2>VoterDetail s</h2> <formid=”voterForm”> <labelfor=”name”>Name:</label> <inputtype=”text”id=”name”name=”name”required><br><br> <labelfor=”age”>Age:</label> <inputtype=”number”id=”age”name=”age”required><br><br> <labelfor=”nationality”>Nationality:</label> <inputtype=”text”id=”nationality”name=”nationality”required><br><br> <inputtype=”submit”value=”Submit”> </form> <divid=”response”></div> <script> $(document).ready(function(){ $(‘#voterForm’).submit(function(event){ Event.preventDefault(); Varname=$(‘#name’).val().toUpperCase(); Varage=$(‘#age’).val(); Varnationality=$(‘#nationality’).val(); $.ajax({ url:‘voter.php’, method:‘POST’, data:{name:name,age:age,nationality:nationality}, success:function(response){ $(‘#response’).html(response); } }); }); }); </script> </body> </html>

Voter.phpfi
le

<?php $name=$_POST[‘name’]; $age=$_POST[‘age’ $national ]; ity=$_POST[‘ national If(preg_match(‘ /[^A-Z] ity’ ]; /’,$name)){ Echo‘Nameshouldbeinuppercaselettersonly.’ }elseif($age<18){ Echo‘Ageshouldnotbelessthan18years.’ }elseif(st rcasecmp($nati onality,‘ Echo‘National Indian’ ityshouldbeIndi }else{ Echo‘Validati onsuccessful.Voterdetai ‘.$age. ’<br>Nati onality:‘ } ?>

Importrandom
Importcsv
#Generaterandomtransactiondata
Transactions=[]
Foriinrange(1,101):
Transaction_id=i
Transaction_date=f”2022-05-{random.randint(1,31):02d}”
Customer_id=random.randint(1,10)
Item_id=random.choice([“A”,“B”,“C”])
Item_price=round(random.uniform(10.0,100.0),2)
Quantity=random.randint(1,10)
Transactions.append([transaction_id,transaction_date,customer_id,item_id,item_price,
quantity])
#SavethedatatoaCSVfile
Withopen(‘transactions.csv’,‘w’,newline=’’)ascsvfile:
Writer=csv.writer(csvfile)
Writer.writerow([“TransactionID”,“TransactionDate”,“CustomerID”,“ItemID”,“ItemPrice”,
“Quantity”])
Fortransactionintransactions:
Writer.writerow(transaction)
Importpandasaspd
#ReadtheCSVfileintoaPandasDataFrame
Df=pd.read_csv(‘transactions.csv’)
#Convertthe“ItemPrice”columntonumerictype
Df[‘ItemPrice’]=pd.to_numeric(df[‘ItemPrice’])
#Calculatethesalesamountforeachtransaction
Df[‘Sales’]=df[‘ItemPrice’]*df[‘Quantity’]
#GroupthetransactionsbycustomerIDandcalculatethetotalsal
esforeachcustomer
Total_sal
es=df.groupby(‘
CustomerID’)[‘
#Printtheresults
Print(tot
al_sal
es