Question 1. Write a class called RestaurantCheck. It should have the following: (use OOPs concepts)
• Fields called check_number, sales_tax_percent, subtotal, table_number, and server_name representing an identification for the check, the bill without tax added, the sales tax percentage, the table number, and the name of the server. • A constructor that sets the values of all four fields • A method called calculate_total that takes no arguments (besides self) and returns the total bill including sales tax. • A method called print_check that writes to a file called check###.txt, where ### is the check number and writes information about the check to that file, formatted like below:
Check Number: 443
Sales tax: 6.0%
Subtotal: $23.14
Total: $24.53
Table Number: 17
Server: Sonic the Hedgehog
Test the class by creating a RestaurantCheck object and calling the print_check() method.
class RestaurantCheck:
def __init__(self, check_number, sales_tax_percent, subtotal, table_number, server_name):
self.check_number = check_number
self.sales_tax_percent = sales_tax_percent
self.subtotal = subtotal
self.table_number = table_number
self.server_name = server_name
def calculate_total(self):
total = self.subtotal + (self.subtotal * self.sales_tax_percent / 100)
return total
def print_check(self):
filename = f"check{self.check_number}.txt"
with open(filename, "w") as file:
file.write(f"Check Number: {self.check_number}\n")
file.write(f"Sales tax: {self.sales_tax_percent}%\n")
file.write(f"Subtotal: ${self.subtotal:.2f}\n")
file.write(f"Total: ${self.calculate_total():.2f}\n")
file.write(f"Table Number: {self.table_number}\n")
file.write(f"Server: {self.server_name}\n")
check = RestaurantCheck(443, 6.0, 23.14, 17, "Sonic the Hedgehog")
check.print_check()
Question 2. Write a Regular Expression Python function to Validate Phone No, (Must be 10 digits) Name, (first Char must be uppercase) E-Mail, (abc@abc.com) Date .(DD-MM-YYYY)
import re
def validate_input(phone_number, name, email, date):
# Phone number validation (10 digits)
if not re.match(r'^\d{10}$', phone_number):
return False
# Name validation (first character uppercase)
if not re.match(r'^[A-Z][a-zA-Z\s]*$', name):
return False
# Email validation (abc@abc.com)
if not re.match(r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$', email):
return False
# Date validation (DD-MM-YYYY)
if not re.match(r'^(0[1-9]|1[0-9]|2[0-9]|3[01])-(0[1-9]|1[0-2])-\d{4}$', date):
return False
# All validations passed
return True
phone_number = '1234767890'
name = 'John Doe'
email = 'johndoe@example.com'
date = '01-01-2023'
if validate_input(phone_number, name, email, date):
print("All inputs are valid.")
else:
print("Invalid input detected.")
All inputs are valid.