Question

I need the following written in Java please, thank you
We want you to implement a java class that will show details on users and throw exceptions where needed. The following requir '- A private String userID that specifics the user ID used for login - A private String phoneNum that specifies the users pho

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

Thanks for the question, here is the code you will be needing, Both User.java and the main methods() has been implemented.

Let me know for any questions or help, no need to post the same question again, just comment your query and i will revert back ASAP.

thank you

=====================================================================

public class User {

    private String firstName;
    private String lastName;
    private int age;
    private String userID;
    private String phoneNum;

    public User() {
        firstName = "";
        lastName = "";
        age = 21;
        userID = "";
        phoneNum = "";
    }

    public User(String firstName, String lastName, int age, String userID, String phoneNum) {

        if (age < 0 || age > 99) throw new IllegalArgumentException("Error: Invalid age provided.");
        if (phoneNum.trim().length() > 10)
            throw new IllegalArgumentException("Error: Phone number length is greater than 10");
        isValidUserID(userID);
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.userID = userID;
        this.phoneNum = phoneNum;
    }

    public User(String firstName, String lastName, String userID, String phoneNum) {

        if (phoneNum.trim().length() > 10)
            throw new IllegalArgumentException("Error: Phone number length is greater than 10");
        isValidUserID(userID);
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = 21;
        this.userID = userID;
        this.phoneNum = phoneNum;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getAge() {
        return age;
    }

    public String getUserID() {
        return userID;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setAge(int age) {
        if (age < 0 || age > 99) throw new IllegalArgumentException("Error: Invalid age provided.");
        this.age = age;
    }

    public void setUserID(String userID) {
        isValidUserID(userID);
        this.userID = userID;
    }

    public void setPhoneNum(String phoneNum) {
        if (phoneNum.trim().length() > 10)
            throw new IllegalArgumentException("Error: Phone number length is greater than 10");
        this.phoneNum = phoneNum;
    }

    private boolean isValidUserID(String userID) {
        if (userID.trim().length() < 8)
            throw new IllegalArgumentException("Error: User ID is not having minimum 8 characters.");

        for (int i = 0; i < userID.length(); i++) {
            if (Character.isDigit(userID.charAt(i))) {
                return true;
            }
        }
        throw new IllegalArgumentException("Error: User ID does not contains any number.");

    }

    private String deconstructPhone() {

        return "Area Code: " + phoneNum.substring(0, 3) + "\nPhone Number: " + phoneNum.substring(3);
    }

    public void display() {
        System.out.println("Name: " + getFirstName() + " " + getLastName());
        System.out.println("Age: " + getAge());
        System.out.println("UserID: " + getUserID());
        System.out.println("Phone Number: " + getPhoneNum());
        System.out.println(deconstructPhone());
    }


    public static void main(String[] args) {

        User user1 = new User();
        user1.setFirstName("Sarah");
        user1.setLastName("Holt");
        user1.setUserID("sarahholt26!");
        user1.setPhoneNum("9548926728");

        user1.display();


        User user2 = new User("Kevin", "James", 25, "kjames2552", "8019023100");
        user2.display();

        try {

            User user3 = new User("Dante", "Counto", "dantetezza", "95000");
            user3.display();
        }catch (IllegalArgumentException e){
            System.out.println(e.getMessage());
        }
    }
}

===========================================================================

Add a comment
Know the answer?
Add Answer to:
I need the following written in Java please, thank you ' We want you to implement...
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
  • Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so...

    Please answer the following question with Java Modify the Employee and ProductionWorker classes provided below so they throw exceptions when the following errors occur. - The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. - The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. - The ProductionWorker class should thrown anexception named InvalidPayRate when it receives a negative number...

  • Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you...

    Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you are expected to implement in your Shape class; - A private String color that specifies the color of the shape - A private boolean filled that specifies whether the shape is filled - A private date (java.util.date) field dateCreated that specifies the date the shape was created Your Shape class will provide the following constructors; - A two-arg constructor that creates a shape with...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • please write in Java and include the two classes Design a class named Fan (Fan.java) to...

    please write in Java and include the two classes Design a class named Fan (Fan.java) to represent a fan. The class contains: An int field named speed that specifies the speed of the fan. A boolean field named fanStatus that specifies whether the fan is on (default false). A double field named radius that specifies the radius of the fan (default 5). A string field named color that specifies the color of the fan (default blue). A no-arg (default) constructor...

  • Create a simple Java class for a Password with the following requirements:  This program will...

    Create a simple Java class for a Password with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  One String property: password (protected to only allow secure passwords) o A secure password must be at least 8 characters in length o A secure password must have three of the following four requirements:  A lower case letter ...

  • With Java Language: In this assignment, you are to create a class named Payroll. In the...

    With Java Language: In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) .İd: String (5 pts) hours: int (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

  • I need help writing the following java Java Method below. I just need a method to...

    I need help writing the following java Java Method below. I just need a method to take a user inputted First or Last Name and return the associated phone number, thank you! import java.util.*; import java.io.*; class PhoneList { public static void main(String[] args) throws Exception { boolean loop = true; File file = new File("PhoneList_Data.txt"); FileWriter fw = new FileWriter(file, true); fw.write("PhoneList Data"); fw.write("\n" + "Last, First: 6041337567, 2181731566");    // do { } (loop)    fw.close(); } //...

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