User Management Program in Java

This program enters users details (name, address, city, date of birth, phone number, email Id). It then maps phone numbers to names with the help of HashMap. Finally, it tells the name of the user which is searched by entering the phone number.

Prerequisite

Java is an object-oriented programming language. It allows you to divide complex problems into smaller sets by creating objects and objects having similar behavior and properties are grouped together in a single unit called class.

Java class

Before you create objects in Java, you need to define a class. A class is a user defined blueprint or prototype from which objects are created.  It represents the set of properties or methods that are common to all objects of one type.

Java Object

An object  is a basic unit of Object Oriented Programming and represents the real life entities. An object is an instance of a class. A class is a template or blueprint from which objects are created. So, an object is the instance(result) of a class. A typical Java program creates many objects, which as you know, interact by invoking methods. 

Code

package com.company;
import java.util.HashMap;
import java.util.Map;
import java.util.HashMap;
public class Application {
static Map<String, User> userStorage = new HashMap<>();
public static void main(String[] args) {
System.out.println("****************Diamond Store************");
UserService userService = new UserService();
User user = userService.getUserDetails();
userStorage.put(user.getPhone_no(), user);
String mobileNumberToSearch = userService.findUserDetails();
User userFromStore = userStorage.get(mobileNumberToSearch);
userService.printUserDetails(userFromStore);
}
}
package com.company;
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.util.HashMap;
import java.util.Map;
public class User {
private String name;
private String address;
private String city;
private String dob;
private String phone_no;
private String emailId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getPhone_no() {
return phone_no;
}
public void setPhone_no(String phone_no) {
this.phone_no = phone_no;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}
view raw User.java hosted with ❤ by GitHub
package com.company;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class UserService {
public User getUserDetails() {
User user = new User();
Scanner scanner = new Scanner(System.in);
System.out.println("==============User Details Input=================");
System.out.println("Please enter you name : ");
String name = scanner.nextLine();
if (name == null || name.isEmpty()) {
System.out.println("Please enter a valid name");
}
user.setName(name);
System.out.println("enter your address ");
String address = scanner.nextLine();
user.setAddress(address);
System.out.println("enter your city ");
String city = scanner.nextLine();
user.setCity(city);
System.out.println("enter your date of birth ");
String dob = scanner.nextLine();
user.setDob(dob);
System.out.println("enter phone number");
String phone_no = scanner.nextLine();
user.setPhone_no(phone_no);
System.out.println("enter email");
String emailID = scanner.nextLine();
user.setEmailId(emailID);
return user;
}
public String findUserDetails() {
System.out.println("Mobile Number to search : ");
Scanner scanner = new Scanner(System.in);
String phone = scanner.nextLine();
return phone;
}
public void printUserDetails(User userFromStore) {
System.out.println("user searched is : "+userFromStore.getName());
}
}

Explanation

Java Class Types

Model class- Model class is a class which represents data object which can be used for transferring data in java application. It encapsulates direct access to data in object and ensures all data in object is accessed via getter methods. Here class User is a model class.

Service class- A Service class is used by a client to interact with some functionality in your application.The general pattern for a service method is to extract information from the request, access external resources, and then populate the response, based on that information. In this program, UserService is the service class.

Application class- application class acts as a coordinator which calls the service class so that it is able to perform its task by invoking the model class.

HashMap

Java HashMap is a hash table based implementation of Java’s Map interface. A Map, as you might know, is a collection of key-value pairs. It maps keys to values.

Running the code

Feel free to post queries.

2 thoughts on “User Management Program in Java

Leave a comment

Design a site like this with WordPress.com
Get started