0% found this document useful (0 votes)
5 views17 pages

Java Insurance Management System Code

The document outlines a Java coding challenge for an Insurance Management System, detailing the directory structure and key classes such as User, Client, Claim, and Payment. It includes methods for creating, viewing, updating, and deleting insurance policies, as well as handling database connections and exceptions. The main module provides a user interface for interacting with the system through a console menu.

Uploaded by

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

Java Insurance Management System Code

The document outlines a Java coding challenge for an Insurance Management System, detailing the directory structure and key classes such as User, Client, Claim, and Payment. It includes methods for creating, viewing, updating, and deleting insurance policies, as well as handling database connections and exceptions. The main module provides a user interface for interacting with the system through a console menu.

Uploaded by

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

Java Coding Challenge: Insurance Management System

– J220 [Link]

Directory structure:

Entity/[Link]:

package entity;
public class User {
private int userId;
private String username;
private String password;
private String role;
public User() {}
public User(int userId, String username, String password, String role)
[Link] = userId;
[Link] = username;
[Link] = password;
[Link] = role;
}
public int getUserId() { return userId; }
public void setUserId(int userId) { [Link] = userId; }
public String getUsername() { return username; }
public void setUsername(String username) { [Link] = username; }
public String getPassword() { return password; }
public void setPassword(String password) { [Link] = password; }
public String getRole() { return role; }
public void setRole(String role) { [Link] = role; }
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", username='" + username + '\'' +
", role='" + role + '\'' +
'}';
}
}

Entity/[Link]:
package entity;
public class Client {
private int clientId;
private String clientName;
private String contactInfo;
private Policy policy;
public Client() {}
public Client(int clientId, String clientName, String contactInfo, Policy policy) {
[Link] = clientId;
[Link] = clientName;
[Link] = contactInfo;
[Link] = policy;
}

public int getClientId() { return clientId; }


public void setClientId(int clientId) { [Link] = clientId; }
public String getClientName() { return clientName; }
public void setClientName(String clientName) { [Link] = clientName; }
public String getContactInfo() { return contactInfo; }
public void setContactInfo(String contactInfo) { [Link] = contactInfo; }
public Policy getPolicy() { return policy; }
public void setPolicy(Policy policy) { [Link] = policy; }

@Override
public String toString() {
return "Client{" +
"clientId=" + clientId +
", clientName='" + clientName + '\'' +
", contactInfo='" + contactInfo + '\'' +
", policy=" + policy +
'}';
}

Entity/[Link]:
package entity;
import [Link];
public class Claim {
private int claimId;
private String claimNumber;
private Date dateFiled;
private double claimAmount;
private String status;
private Policy policy;
private Client client;
public Claim() {}
public Claim(int claimId, String claimNumber, Date dateFiled,
double claimAmount, String status, Policy policy, Client client) {
[Link] = claimId;
[Link] = claimNumber;
[Link] = dateFiled;
[Link] = claimAmount;
[Link] = status;
[Link] = policy;
[Link] = client;
}
public int getClaimId() { return claimId; }
public void setClaimId(int claimId) { [Link] = claimId; }
public String getClaimNumber() { return claimNumber; }
public void setClaimNumber(String claimNumber) { [Link] = claimNumber; }
public Date getDateFiled() { return dateFiled; }
public void setDateFiled(Date dateFiled) { [Link] = dateFiled; }
public double getClaimAmount() { return claimAmount; }
public void setClaimAmount(double claimAmount) { [Link] = claimAmount; }
public String getStatus() { return status; }
public void setStatus(String status) { [Link] = status; }
public Policy getPolicy() { return policy; }
public void setPolicy(Policy policy) { [Link] = policy; }
public Client getClient() { return client; }
public void setClient(Client client) { [Link] = client; }

@Override
public String toString() {
return "Claim{" +
"claimId=" + claimId +
", claimNumber='" + claimNumber + '\'' +
", dateFiled=" + dateFiled +
", claimAmount=" + claimAmount +
", status='" + status + '\'' +
", policy=" + policy +
", client=" + client +
'}';
}
}

Entity/Payment:
package entity;
import [Link];
public class Payment {
private int paymentId;
private Date paymentDate;
private double paymentAmount;
private Client client
public Payment() {}
public Payment(int paymentId, Date paymentDate, double paymentAmount, Client client)
{
[Link] = paymentId;
[Link] = paymentDate;
[Link] = paymentAmount;
[Link] = client;
}
public int getPaymentId() { return paymentId; }
public void setPaymentId(int paymentId) { [Link] = paymentId; }
public Date getPaymentDate() { return paymentDate; }
public void setPaymentDate(Date paymentDate) { [Link] = paymentDate; }
public double getPaymentAmount() { return paymentAmount; }
public void setPaymentAmount(double paymentAmount) { [Link] =
paymentAmount; }
public Client getClient() { return client; }
public void setClient(Client client) { [Link] = client;
@Override
public String toString() {
return "Payment{" +
"paymentId=" + paymentId +
", paymentDate=" + paymentDate +
", paymentAmount=" + paymentAmount +
", client=" + client +
}
}
}

Exception and Util:

package util;
import [Link];
import [Link];
import [Link];
public class DBConnection {
private static Connection connection;
public static Connection getConnection() throws SQLException {
if (connection == null || [Link]()) {
String connectionString = [Link]();
connection = [Link](connectionString);
}
return connection;
}
}
package util;
import [Link];
import [Link];
import [Link];
public class PropertyUtil {
private static final String PROPERTY_FILE = "[Link]";
public static String getPropertyString() {
Properties props = new Properties();
try (FileInputStream fis = new FileInputStream(PROPERTY_FILE)) {
[Link](fis);
String host = [Link]("[Link]");
String port = [Link]("[Link]");
String dbName = [Link]("[Link]");
String username = [Link]("[Link]");
String password = [Link]("[Link]");
Return; [Link]("jdbc:mysql://%s:%s/%s?user=%s&password=%s", host, port,
dbName, username, password);
} catch (IOException e) {
[Link]();
throw new RuntimeException("Failed to load database properties", e);
}
}
}
package myexceptions;
public class PolicyNotFoundException extends Exception {
public PolicyNotFoundException(String message) {
super(message); }}
Main:
package mainmod;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainModule {


public static void main(String[] args) {
IPolicyService policyService = new InsuranceServiceImpl();

Try{
Policy newPolicy = new Policy();
boolean created = [Link](newPolicy);
[Link]("Policy created: " + created);
try {
Policy policy = [Link](1);
[Link]("Retrieved policy: " + policy);
} catch (PolicyNotFoundException e) {
[Link]("Error: " + [Link]());
}
Collection<Policy> policies = [Link]();
[Link]("All policies: " + policies);
try {
Policy updatePolicy = new Policy();
// Set updated policy details
boolean updated = [Link](updatePolicy);
[Link]("Policy updated: " + updated);
} catch (PolicyNotFoundException e) {
[Link]("Error: " + [Link]());
}
try {
boolean deleted = [Link](1);
[Link]("Policy deleted: " + deleted);
} catch (PolicyNotFoundException e) {
[Link]("Error: " + [Link]())

} catch (Exception e) {
[Link]("An unexpected error occurred: " + [Link]());
[Link]();
}
}
}
package mainmod;
import [Link];
import [Link];
import entity.*;
import [Link];
import [Link];
import [Link];
import [Link];

public class MainModule {


private static IPolicyService policyService = new InsuranceServiceImpl();
private static Scanner scanner = new Scanner([Link]);

public static void main(String[] args) {


while (true) {
try {
displayMenu();
int choice = [Link]();
[Link](); // Consume newline

switch (choice) {
case 1:
createNewPolicy();
break;
case 2:
viewPolicy();
break;
case 3:
viewAllPolicies();
break;
case 4:
updateExistingPolicy();
break;
case 5:
deleteExistingPolicy();
break;
case 6:
[Link]("Exiting the application. Goodbye!");
[Link]();
[Link](0);
default:
[Link]("Invalid choice. Please try again.");
}
} catch (Exception e) {
[Link]("An error occurred: " + [Link]());
[Link]();
}

[Link]("\nPress Enter to continue...");


[Link]();
}
}

private static void displayMenu() {


[Link]("\n=== Insurance Management System ===");
[Link]("1. Create New Policy");
[Link]("2. View Policy");
[Link]("3. View All Policies");
[Link]("4. Update Policy");
[Link]("5. Delete Policy");
[Link]("6. Exit");
[Link]("Enter your choice: ");
}

private static void createNewPolicy() {


try {
[Link]("\n=== Create New Policy ===");

// Get policy details from user


Policy newPolicy = getPolicyDetailsFromUser();

// Create policy
boolean created = [Link](newPolicy);

if (created) {
[Link]("Policy created successfully!");
} else {
[Link]("Failed to create policy.");
}
} catch (Exception e) {
[Link]("Error creating policy: " + [Link]());
}
}

private static void viewPolicy() {


try {
[Link]("\n=== View Policy ===");
[Link]("Enter Policy ID: ");
int policyId = [Link]();
[Link](); // Consume newline

try {
Policy policy = [Link](policyId);
displayPolicyDetails(policy);
} catch (PolicyNotFoundException e) {
[Link]("Error: " + [Link]());
}
} catch (Exception e) {
[Link]("Error retrieving policy: " + [Link]());
}
}

private static void viewAllPolicies() {


try {
[Link]("\n=== All Policies ===");
Collection<Policy> policies = [Link]();
if ([Link]()) {
[Link]("No policies found.");
return;
}

for (Policy policy : policies) {


displayPolicyDetails(policy);
[Link]("------------------------");
}
} catch (Exception e) {
[Link]("Error retrieving policies: " + [Link]());
}
}

private static void updateExistingPolicy() {


try {
[Link]("\n=== Update Policy ===");
[Link]("Enter Policy ID to update: ");
int policyId = [Link]();
[Link](); // Consume newline

try {
Policy existingPolicy = [Link](policyId);
[Link]("Current Policy Details:");
displayPolicyDetails(existingPolicy);
Policy updatedPolicy = getPolicyDetailsFromUser();
[Link](policyId);

boolean updated = [Link](updatedPolicy);


if (updated) {
[Link]("Policy updated successfully!");
} else {
[Link]("Failed to update policy.");
}
} catch (PolicyNotFoundException e) {
[Link]("Error: " + [Link]());
}
} catch (Exception e) {
[Link]("Error updating policy: " + [Link]());
}
}

private static void deleteExistingPolicy() {


try {
[Link]("\n=== Delete Policy ===");
[Link]("Enter Policy ID to delete: ");
int policyId = [Link]();
[Link](); // Consume newline

try {
boolean deleted = [Link](policyId);
if (deleted) {
[Link]("Policy deleted successfully!");
} else {
[Link]("Failed to delete policy.");
}
} catch (PolicyNotFoundException e) {
[Link]("Error: " + [Link]());
}
} catch (Exception e) {
[Link]("Error deleting policy: " + [Link]());
}
}

private static Policy getPolicyDetailsFromUser() {


Policy policy = new Policy();

[Link]("Enter Policy Type: ");


[Link]([Link]());

[Link]("Enter Coverage Amount: ");


[Link]([Link]());
[Link]();

[Link]("Enter Premium Amount: ");


[Link]([Link]());
[Link]();
[Link](new Date());
Date endDate = new Date();
[Link]([Link]() + 1);
[Link](endDate);

return policy;
}

private static void displayPolicyDetails(Policy policy) {


[Link]("Policy ID: " + [Link]());
[Link]("Type: " + [Link]());
[Link]("Coverage Amount: $" + [Link]());
[Link]("Premium Amount: $" + [Link]());
[Link]("Start Date: " + [Link]());
[Link]("End Date: " + [Link]());
}
}

Outputs:

You might also like