[go: up one dir, main page]

0% found this document useful (0 votes)
20 views7 pages

Ooaddddddddd

code

Uploaded by

poornimaepy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Ooaddddddddd

code

Uploaded by

poornimaepy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

package javaapplication2;

import java.sql.*;
import java.util.Scanner;

public class ContactManagementSystem {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
UserService userService = new UserService();
AdminService adminService = new AdminService();
ContactService contactService = new ContactService();
Connection connection = null;

try {
// Establish database connection
connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/phonebook", "root", "");
System.out.println("Connected to database successfully!");
} catch (SQLException ex) {
System.err.println("Error connecting to the database: " +
ex.getMessage());
return;
}

while (true) {
System.out.println("Welcome to Contact Management System");
System.out.println("1. User Registration");
System.out.println("2. Admin Registration");
System.out.println("3. Login");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline

switch (choice) {
case 1 -> userService.registerUser(connection);
case 2 -> adminService.registerAdmin(connection);
case 3 -> {
if (login(connection, userService, adminService,
contactService)) {
return;
}
}
case 4 -> {
System.out.println("Exiting system...");
return;
}
default -> System.out.println("Invalid choice!");
}
}
}

private static boolean login(Connection connection, UserService userService,


AdminService adminService, ContactService contactService) {
Scanner scanner = new Scanner(System.in);
System.out.println("Login");
System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
if (!username.isEmpty() && !password.isEmpty()) {
if (adminService.loginAdmin(connection, username, password)) {
System.out.println("Login successful!");
// Call admin menu
adminMenu(connection, contactService);
return true;
} else if (userService.loginUser(connection, username, password)) {
System.out.println("Login successful!");
// Call user menu
userMenu(connection, username, contactService);
return true;
} else {
System.out.println("Login failed! Invalid username or password.");
}
} else {
System.out.println("Both username and password are required.");
}
return false;
}

private static void userMenu(Connection connection, String username,


ContactService contactService) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("User Menu");
System.out.println("1. Add Contact");
System.out.println("2. Delete Contact");
System.out.println("3. Search Contact");
System.out.println("4. Edit Contact");
System.out.println("5. Logout");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline

switch (choice) {
case 1 -> contactService.addContact(connection, username);
case 2 -> contactService.deleteContact(connection, username);
case 3 -> contactService.searchContact(connection, username);
case 4 -> contactService.editContact(connection, username);
case 5 -> {
System.out.println("Logged out successfully!");
return;
}
default -> System.out.println("Invalid choice!");
}
}
}

private static void adminMenu(Connection connection, ContactService


contactService) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Admin Menu");
System.out.println("1. Retrieve Contacts");
System.out.println("2. Logout");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline

switch (choice) {
case 1 -> contactService.retrieveContacts(connection);
case 2 -> {
System.out.println("Logged out successfully!");
return;
}
default -> System.out.println("Invalid choice!");
}
}
}
}

class UserService {
public void registerUser(Connection connection) {
Scanner scanner = new Scanner(System.in);
try {
System.out.println("User Registration");
System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
if (!username.isEmpty() && !password.isEmpty()) {
PreparedStatement insertStatement =
connection.prepareStatement("INSERT INTO user (username, password) VALUES (?, ?)");
insertStatement.setString(1, username);
insertStatement.setString(2, password);
insertStatement.executeUpdate();
System.out.println("Registration successful!");
} else {
System.out.println("Error: Both username and password are required
for registration.");
}
} catch (SQLException e) {
System.err.println("Error registering user: " + e.getMessage());
}
}

public boolean loginUser(Connection connection, String username, String


password) {
try {
PreparedStatement statement = connection.prepareStatement("SELECT *
FROM user WHERE username = ? AND password = ?");
statement.setString(1, username);
statement.setString(2, password);
ResultSet resultSet = statement.executeQuery();
return resultSet.next();
} catch (SQLException e) {
System.err.println("Error logging in: " + e.getMessage());
return false;
}
}
}

class AdminService {
public void registerAdmin(Connection connection) {
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Admin Registration");
System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
if (!username.isEmpty() && !password.isEmpty()) {
PreparedStatement insertStatement =
connection.prepareStatement("INSERT INTO admin (username, password) VALUES
(?, ?)");
insertStatement.setString(1, username);
insertStatement.setString(2, password);
insertStatement.executeUpdate();
System.out.println("Registration successful!");
} else {
System.out.println("Error: Both username and password are required
for registration.");
}
} catch (SQLException e) {
System.err.println("Error registering admin: " + e.getMessage());
}
}

public boolean loginAdmin(Connection connection, String username, String


password) {
try {
PreparedStatement statement = connection.prepareStatement("SELECT *
FROM admin WHERE username = ? AND password = ?");
statement.setString(1, username);
statement.setString(2, password);
ResultSet resultSet = statement.executeQuery();
return resultSet.next();
} catch (SQLException e) {
System.err.println("Error logging in as admin: " + e.getMessage());
return false;
}
}
}

class ContactService {
public void addContact(Connection connection, String username) {
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Add Contact");
System.out.print("Enter contact name: ");
String name = scanner.nextLine();
System.out.print("Enter contact dob: ");
String dob = scanner.nextLine();
System.out.print("Enter contact city: ");
String city = scanner.nextLine();

// Gather new contact information


System.out.print("Enter contact phone number: ");
String phoneNumber = scanner.nextLine();
if (!isValidPhoneNumber(phoneNumber)) {
throw new IllegalArgumentException("Invalid phone number. Phone number
must have 10 digits.");
}

System.out.print("Enter contact email: ");


String email = scanner.nextLine();
if (!isValidEmail(email)) {
throw new IllegalArgumentException("Invalid email format. Email must
contain '@'.");
}

System.out.print("Enter contact address (optional): ");


String address = scanner.nextLine();

PreparedStatement insertStatement = connection.prepareStatement("INSERT


INTO contact (user_id, name,dob,city ,phone_number, email, address) VALUES
(?,?,? ,?, ?, ?, ?)");
insertStatement.setInt(1, getUserId(connection, username));
insertStatement.setString(2, name);
insertStatement.setString(3, dob);
insertStatement.setString(4, city);
insertStatement.setString(5, phoneNumber);
insertStatement.setString(6, email);
insertStatement.setString(7, address);
insertStatement.executeUpdate();

System.out.println("Contact added successfully!");


} catch (SQLException e) {
System.err.println("Error adding contact: " + e.getMessage());
}
}

public void deleteContact(Connection connection, String username) {


Scanner scanner = new Scanner(System.in);
try {
System.out.println("Delete Contact");
System.out.print("Enter contact name to delete: ");
String name = scanner.nextLine();
PreparedStatement deleteStatement = connection.prepareStatement("DELETE
FROM contact WHERE user_id = ? AND name = ?");
deleteStatement.setInt(1, getUserId(connection, username));
deleteStatement.setString(2, name);
int rowsAffected = deleteStatement.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Contact deleted successfully!");
} else {
System.out.println("Contact not found!");
}
} catch (SQLException e) {
System.err.println("Error deleting contact: " + e.getMessage());
}
}

public void searchContact(Connection connection, String username) {


Scanner scanner = new Scanner(System.in);
try {
System.out.println("Search Contact");
System.out.print("Enter contact name to search: ");
String name = scanner.nextLine();
PreparedStatement searchStatement = connection.prepareStatement("SELECT
* FROM contact WHERE user_id = ? AND name = ?");
searchStatement.setInt(1, getUserId(connection, username));
searchStatement.setString(2, name);
ResultSet resultSet = searchStatement.executeQuery();
if (resultSet.next()) {
System.out.println("Name: " + resultSet.getString("name"));
System.out.println("DOB: " + resultSet.getString("dob"));
System.out.println("City: " + resultSet.getString("city"));
System.out.println("Phone Number: " +
resultSet.getString("phone_number"));
System.out.println("Email: " + resultSet.getString("email"));
System.out.println("Address: " + resultSet.getString("address"));
} else {
System.out.println("Contact not found!");
}
} catch (SQLException e) {
System.err.println("Error searching contact: " + e.getMessage());
}
}

public void editContact(Connection connection, String username) {


Scanner scanner = new Scanner(System.in);
try {
System.out.println("Edit Contact");
System.out.print("Enter contact name to edit: ");
String name = scanner.nextLine();
System.out.print("Enter contact dob: ");
String newdob = scanner.nextLine();
System.out.print("Enter contact city: ");
String newcity = scanner.nextLine();

// Gather new contact information


System.out.print("Enter new phone number: ");
String newPhoneNumber = scanner.nextLine();
if (!isValidPhoneNumber(newPhoneNumber)) {
throw new IllegalArgumentException("Invalid phone number. Phone number
must have 10 digits.");
}

System.out.print("Enter new email: ");


String newEmail = scanner.nextLine();
if (!isValidEmail(newEmail)) {
throw new IllegalArgumentException("Invalid email format. Email must
contain '@'.");
}
System.out.print("Enter new address: ");
String newaddress = scanner.nextLine();

PreparedStatement editStatement = connection.prepareStatement("UPDATE


contact SET dob=? ,city=?, phone_number = ?, email = ?,address=? WHERE user_id = ?
AND name = ?");
editStatement.setString(1, newdob);
editStatement.setString(2, newcity);
editStatement.setString(3, newPhoneNumber);
editStatement.setString(4, newEmail);
editStatement.setString(5, newaddress);
editStatement.setInt(6, getUserId(connection, username));
editStatement.setString(7, name);

int rowsAffected = editStatement.executeUpdate();


if (rowsAffected > 0) {
System.out.println("Contact updated successfully!");
} else {
System.out.println("Contact not found!");
}
} catch (SQLException e) {
System.err.println("Error editing contact: " + e.getMessage());
}
}

private boolean isValidPhoneNumber(String phoneNumber) {


return phoneNumber.matches("\\d{10}");
}

private boolean isValidEmail(String email) {


return email.contains("@");
}

public void retrieveContacts(Connection connection) {


try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM contact");
while (resultSet.next()) {
System.out.println("Name: " + resultSet.getString("name"));
System.out.println("DOB: " + resultSet.getString("dob"));
System.out.println("City: " + resultSet.getString("city"));
System.out.println("Phone Number: " +
resultSet.getString("phone_number"));
System.out.println("----------------------------------");
}
} catch (SQLException e) {
System.err.println("Error retrieving contacts: " + e.getMessage());
}
}

private int getUserId(Connection connection, String username) throws


SQLException {
PreparedStatement statement = connection.prepareStatement("SELECT id FROM
user WHERE username = ?");
statement.setString(1, username);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return resultSet.getInt("id");
} else {
throw new RuntimeException("User not found");
}
}
}

You might also like