Question

In python class Customer: def __init__(self, customer_id, last_name, first_name, phone_number, address): self._customer_id = int(customer_id) self._last_name =...

In python

class Customer:
    def __init__(self, customer_id, last_name, first_name, phone_number, address):
        self._customer_id = int(customer_id)
        self._last_name = str(last_name)
        self._first_name = str(first_name)
        self._phone_number = str(phone_number)
        self._address = str(address)


    def display (self):
        return str(self._customer_id) + ", " + self._first_name + self._last_name + "\n" + self._phone_number + "\n" + self._address


customer_one = Customer("694", "Abby", "Boat", "515-555-4289", "123 Bobby Rd", )
print(customer_one.display())

customer_two = Customer ("456AB", "Scott", "James", "515-875-3099", "23 Sesame Street Brooklyn, NY 11213")
print(customer_two.display())

Use Customer class remove the address attribute.Place the code in Module12 project package custom_exceptions, file customer_exceptions.py. Include your directory for test_custom_exceptions and test_customer_exceptions.py Make exception for the following attributes: customer_id -a number between 1000-9999, InvalidCustomerIdException last_name - alpha characters, InvalidNameException first_name -alpha characters, InvalidNameException phone_number - 123-123-1234 format, InvalidPhoneNumberFormat Include driver code that tests the exceptions with try/except include calls for constructor with invalid customer_id constructor with invalid last_name constructor with invalid first_name constructor with invalid phone_number str() Include unit test to test the code setUp() tearDown() constructor with invalid customer_id constructor with invalid last_name constructor with invalid first_name constructor with invalid phone_number str() test(s)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You will have to use regular expressions to validate the scenarios.

  1. ^[0-9]+$ to validate that customer id contain only digits. You can create complex regular expression to validate whether the number is between 1000-9999
  2. ^[A-Za-z]+$ to validate that the name contain only alphabets, not even blank space
  3. ^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$ to verify the phone number contains only numbers and is in proper format. This will allow user to enter a phone number "000-000-0000". A lengthy, but not so complex, regular expression can be written to validate this too.
import re

class Customer:
    def __init__(self, customer_id, last_name, first_name, phone_number):

        if re.match("^[0-9]+$",customer_id):
            if not int(customer_id) in range(1000,10000):
                raise InvalidCustomerIdException
            else:
                self._customer_id = int(customer_id)
        else:
            raise InvalidCustomerIdException
        if re.match("^[A-Za-z]+$",last_name):
            self._last_name = str(last_name)
        else:
            raise InvalidNameException
        if re.match("^[A-Za-z]+$",first_name):
            self._first_name = str(first_name)
        else:
            raise InvalidNameException
        if re.match("^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$",phone_number):
            self._phone_number = str(phone_number)
        else:
            raise InvalidPhoneNumberFormat



    def display (self):
        return str(self._customer_id) + ", " + self._first_name + self._last_name + "\n" + self._phone_number




class InvalidCustomerIdException(Exception):
    pass
class InvalidNameException(Exception):
    pass
class InvalidPhoneNumberFormat(Exception):
    pass

if __name__ == "__main__":
    # Invalid Cusotmer ID
    # customer_one = Customer("690", "Abby", "Boat", "515-555-4289" )
    # print(customer_one.display())
    # customer_one = Customer("690AB", "Abby", "Boat", "515-555-4289" )
    # print(customer_one.display())
    # Valid Cusotmer ID
    customer_one = Customer("6900", "Abby", "Boat", "105-505-4280" )
    print(customer_one.display())
Add a comment
Know the answer?
Add Answer to:
In python class Customer: def __init__(self, customer_id, last_name, first_name, phone_number, address): self._customer_id = int(customer_id) self._last_name =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Take the class Person. class Person: """Person class""" def __init__(self, lname, fname, addy=''): self._last_name = lname...

    Take the class Person. class Person: """Person class""" def __init__(self, lname, fname, addy=''): self._last_name = lname self._first_name = fname self._address = addy def display(self): return self._last_name + ", " + self._first_name + ":" + self._address Implement derived class Student In the constructor Add attribute major, default value 'Computer Science' Add attribute gpa, default value '0.0' Add attribute student_id, not optional Consider all private Override method display() Test your code with the following driver: # Driver my_student = Student(900111111, 'Song', 'River')...

  • Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None de...

    Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None def search(self, find_data): if self.data == find_data: return self elif find_data < self.data and self.left != None: return self.left.search(find_data) elif find_data > self.data and self.right != None: return self.right.search(find_data) else: return None    def get_left(self): return self.left def get_right(self): return self.right def set_left(self, tree): self.left = tree def set_right(self, tree): self.right = tree def set_data(self, data): self.data = data def get_data(self): return self.data def traverse(root,order):...

  • Hi, can someone offer input on how to address these 4 remain parts the zybook python...

    Hi, can someone offer input on how to address these 4 remain parts the zybook python questions?   4: Tests that get_num_items_in_cart() returns 6 (ShoppingCart) Your output ADD ITEM TO CART Enter the item name: Traceback (most recent call last): File "zyLabsUnitTestRunner.py", line 10, in <module> passed = test_passed(test_passed_output_file) File "/home/runner/local/submission/unit_test_student_code/zyLabsUnitTest.py", line 8, in test_passed cart.add_item(item1) File "/home/runner/local/submission/unit_test_student_code/main.py", line 30, in add_item item_name = str(input('Enter the item name:\n')) EOFError: EOF when reading a line 5: Test that get_cost_of_cart() returns 10 (ShoppingCart)...

  • Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk()...

    Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk() self.main.title("mine sweeper") self.define_widgets() self.mines = random.sample( [(i,j) for i in range(25) for j in range(50) ],self.CustomizeNumberOfMines()) print(self.mines) self.main.mainloop() self.CustomizeNumberOfMines() def define_widgets(self): """ Define a canvas object, populate it with squares and possible texts """ self.canvas = tk.Canvas(self.main, width = 1002, height=502, bg="#f0f0f0") self.canvas.grid(row=0, column=0) self.boxes =...

  • Python only please, thanks in advance. Type up the GeometricObject class (code given on the back...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • employee.h ---------- #include <stdio.h> #include <iostream> #include <fstream> class Employee { private: int employeeNum; std::string name;...

    employee.h ---------- #include <stdio.h> #include <iostream> #include <fstream> class Employee { private: int employeeNum; std::string name; std::string address; std::string phoneNum; double hrWage, hrWorked; public: Employee(int en, std::string n, std::string a, std::string pn, double hw, double hwo); std::string getName(); void setName(std::string n); int getENum(); std::string getAdd(); void setAdd(std::string a); std::string getPhone(); void setPhone(std::string p); double getWage(); void setWage(double w); double getHours(); void setHours(double h); double calcPay(double a, double b); static Employee read(std::ifstream& in); void write(std::ofstream& out); }; employee.cpp ---------- //employee.cpp #include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT