Create a class named as Customer under package :package1, which contains following private variables/ attributes,
Member Field name
Type
id
Long
name
String
gender
Character (M/F)
String
contactNumber
String
Mark all the attributes as private
Create / Generate appropriate Getters & Setters.
When the "customer" object is printed, it should display the following details: (override to String method)
. Customer: name
. Customer contact details: contact Number, email
Refer Sample Input and Outputs.
HINT:
Please print customer id in the Main class.
The toString method should print as specified in the problem statement [Name and contact details alone]
Sample Input:
Enter Customer Details:
ID: 45
Name: John
Gender: M
Email: john@a.com
Contact number: +997-4854-7485965123
Sample Output:
Customer id 45
Customer: John
Customer contact details:+997-4854-7485965123,
import java.util.*;
class Customer {
private long id;
private String name;
private Character gender;
private String email;
private String contactNumber;
public Customer() {
}
public Customer(long id, String name, Character gender, String email, String contactNumber) {
this.id = id;
this.name = name;
this.gender = gender;
this.email = email;
this.contactNumber = contactNumber;
}
public String toString() {
return "ID: " + id + "\n" + "Name: " + name + "\n" + "Gender: " + gender + "\n" + "Email: " + email + "\n"
+ "Contact number: " + contactNumber;
}
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the gender
*/
public Character getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(Character gender) {
this.gender = gender;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the contactNumber
*/
public String getContactNumber() {
return contactNumber;
}
/**
* @param contactNumber the contactNumber to set
*/
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
}
class App {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Customer Details:");
System.out.print("ID: ");
long id = keyboard.nextLong();
keyboard.nextLine();
System.out.print("Name: ");
String name = keyboard.nextLine();
System.out.print("Gender: ");
Character gender = keyboard.next().charAt(0);
keyboard.nextLine();
System.out.print("Email: ");
String email = keyboard.nextLine();
System.out.print("Contact number: ");
String contactNumber = keyboard.nextLine();
Customer c = new Customer(id, name, gender, email, contactNumber);
System.out.println(c.toString());
keyboard.close();
}
}
Comments
Leave a comment