Core Java_OOPS Concept_Problem Statement1 Benjamin started a Mobile Accessories
ID: 3906564 • Letter: C
Question
Core Java_OOPS Concept_Problem Statement1
Benjamin started a Mobile Accessories and Gadgets Shop in his City. Youngsters today are more attracted towards gadgets, the reason why Benjamin chose to invest in this industry. Undoubtedly he had a decent customer base visiting his shop everyday. To ease his job in maintaining the details of his Users, he wantedan automated system that will search a specific User detail based on two search types - one search using Username and the other search using Firstname and Lastname and display the details of that User.
One of the key benefits of using Object-Oriented Programming is Method Overloading. Method Overloading is the ability to create multiple methods of the same name in the same class with different implementations.
Write a program using Method Overloading concept using two methods that will help Benjamin to search User details, one search using Username and the other using Firstname and Lastname.
Create a class named User with the following private member variables / attributes.
String userName
String firstName
String lastName
String contact
Include a 4-argument argument constructor with following order userName,firstName,lastName,contact.
Include appropriate getters,setters and default constructor
This class includes the following methods:
No
Method Name
Description
1
User findUser(User userArray[],String userName)
In this method, find the appropriate user by username and return the user object, otherwise return null.
User findUser(User userArray[],StringfirstName,String lastName)
Create a Main class to access the above class methods and display the user details.
[Note :Strictly adhere to the object oriented specifications given as a part of the problem statement.Use the same class names, attribute names and method names.]
Input and Output Format:
If the user is not present display 'User not found'
Refer sample input and output for formatting specification.
[All text in bold corresponds to input and the rest corresponds to output.]
Sample Input and Output 1:
Enter the total number of users
2
Enter user details
peeter,peeter,mark,9965000001
john,john,sam,9500484444
1)Search user by user name
2)Search user by first name and last name
Enter your option
1
Enter the user name to search
peeter
User details :
Username :peeter
FirstName :peeter
LastName :mark
Contact :9965000001
Sample Input and Output 2:
Enter the total number of users
1
Enter user details
suriya,suriya,sivakumar,9966554433
1)Search user by user name
2)Search user by first name and last name
Enter your option
1
Enter the user name to search
sivakumar
User not found
Here I am providing the piece of code. Please update in that format and send me back.
User.Java
public class User {
private String userName;
private String firstName;
private String lastName;
private String contact;
public User(String userName, String firstName, String lastName, String contact) {
this.userName = userName;
this.firstName = firstName;
this.lastName = lastName;
this.contact = contact;
}
User() {}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
//fill your code
}
Main.Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
//fill your code
}
}
No
Method Name
Description
1
User findUser(User userArray[],String userName)
In this method, find the appropriate user by username and return the user object, otherwise return null.
2User findUser(User userArray[],StringfirstName,String lastName)
In this method, find the appropriate user by firstname and lastname and return the user object, otherwise return null.Explanation / Answer
Given below is the code for the question. Since your start code for Main had BufferedReader class imported, I used the same to read values from console. Scanner class is not used.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static User findUser(User userArray[],String userName)
{
for(int i = 0 ; i < userArray.length; i++)
{
if(userArray[i].getUserName().equalsIgnoreCase(userName))
return userArray[i];
}
return null;
}
static User findUser(User userArray[],String firstName,String lastName)
{
for(int i = 0 ; i < userArray.length; i++)
{
if(userArray[i].getFirstName().equalsIgnoreCase(firstName) &&
userArray[i].getLastName().equalsIgnoreCase(lastName))
return userArray[i];
}
return null;
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n, choice;
User[] userArray;
System.out.println("Enter the total number of users");
n = Integer.parseInt(reader.readLine());
System.out.println("Enter user details");
userArray = new User[n];
for(int i = 0; i < n; i++)
{
String line = reader.readLine();
String[] values = line.split(",");
userArray[i] = new User(values[0], values[1], values[2], values[3]);
}
System.out.println("1)Search user by user name");
System.out.println("2)Search user by first name and last name");
System.out.println("Enter your option");
choice = Integer.parseInt(reader.readLine());
User u = null;
if(choice == 1)
{
System.out.println("Enter the user name to search");
String userName = reader.readLine();
u = findUser(userArray, userName);
}
else
{
String firstName, lastName;
System.out.println("Enter the first name");
firstName = reader.readLine();
System.out.println("Enter the last name");
lastName = reader.readLine();
u = findUser(userArray, firstName, lastName);
}
if(u == null)
System.out.println("User not found");
else
{
System.out.println("User details:");
System.out.println("Username:" + u.getUserName());
System.out.println("Firstname:" +u.getFirstName());
System.out.println("Lastname:" + u.getLastName());
System.out.println("Contact:" +u.getContact());
}
}
}
output
====
Enter the total number of users
2
Enter user details
peeter,peeter,mark,9965000001
john,john,sam,9500484444
1)Search user by user name
2)Search user by first name and last name
Enter your option
1
Enter the user name to search
john
User details:
Username:john
Firstname:john
Lastname:sam
Contact:9500484444
=====
Enter the total number of users
2
Enter user details
peeter,peeter,mark,9965000001
john,john,sam,9500484444
1)Search user by user name
2)Search user by first name and last name
Enter your option
2
Enter the first name
peeter
Enter the last name
mark
User details:
Username:peeter
Firstname:peeter
Lastname:mark
Contact:9965000001
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.