Java Case Study VehcileIns Code

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

import java.util.

ArrayList;
import java.util.Date;
import java.util.Scanner;

public class StarProtectVehicleApp {


static Scanner scanner = new Scanner(System.in);
static ArrayList<Underwriter> underwriters = new ArrayList<>();
static int underwriterIdCounter = 1; // For auto-incrementing Underwriter ID
static int policyIdCounter = 1; // For auto-incrementing Policy ID
static Admin admin = new Admin("admin", "admin@123");

public static void main(String[] args) {


while (true) {
System.out.println("Welcome to Star Protect Vehicle System");
System.out.println("1. Admin Login");
System.out.println("2. Underwriter Login");
System.out.print("Choose your role (1/2): ");
int roleChoice = scanner.nextInt();
scanner.nextLine(); // Consume newline

switch (roleChoice) {
case 1:
adminLogin();
break;
case 2:
underwriterLogin();
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}

private static void adminLogin() {


System.out.print("Enter Admin User ID: ");
String userId = scanner.nextLine();
System.out.print("Enter Admin Password: ");
String password = scanner.nextLine();

if (admin.validateAdmin(userId, password)) {
adminMenu();
} else {
System.out.println("Invalid credentials. Please try again.");
}
}

private static void adminMenu() {


boolean exit = false;
while (!exit) {
System.out.println("\nAdmin Menu:");
System.out.println("1. Register Underwriter");
System.out.println("2. Search Underwriter");
System.out.println("3. Update Underwriter Password");
System.out.println("4. Delete Underwriter");
System.out.println("5. View All Underwriters");
System.out.println("6. View Vehicles by Underwriter ID");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
int adminChoice = scanner.nextInt();
scanner.nextLine(); // Consume newline

switch (adminChoice) {
case 1:
admin.registerUnderwriter();
break;
case 2:
admin.searchUnderwriter();
break;
case 3:
admin.updateUnderwriterPassword();
break;
case 4:
admin.deleteUnderwriter();
break;
case 5:
admin.viewAllUnderwriters();
break;
case 6:
admin.viewVehiclesByUnderwriterId();
break;
case 7:
exit = true;
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}

private static void underwriterLogin() {


System.out.print("Enter Underwriter ID: ");
int underwriterId = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter Underwriter Password: ");
String password = scanner.nextLine();

for (Underwriter underwriter : underwriters) {


if (underwriter.id == underwriterId &&
underwriter.password.equals(password)) {
underwriterMenu(underwriter);
return;
}
}
System.out.println("Invalid Underwriter ID or password.");
}

private static void underwriterMenu(Underwriter underwriter) {


boolean exit = false;
while (!exit) {
System.out.println("\nUnderwriter Menu:");
System.out.println("1. Create a new Vehicle Insurance");
System.out.println("2. Renew a Policy");
System.out.println("3. Change Policy Type");
System.out.println("4. View Policies");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int underwriterChoice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (underwriterChoice) {
case 1:
underwriter.createVehicleInsurance();
break;
case 2:
underwriter.renewPolicy();
break;
case 3:
underwriter.changePolicyType();
break;
case 4:
underwriter.viewPolicies();
break;
case 5:
exit = true;
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}

class Admin {
private String adminUserId;
private String adminPassword;

public Admin(String adminUserId, String adminPassword) {


this.adminUserId = adminUserId;
this.adminPassword = adminPassword;
}

public boolean validateAdmin(String userId, String password) {


return adminUserId.equals(userId) && adminPassword.equals(password);
}

public void registerUnderwriter() {


System.out.print("Enter Underwriter Name: ");
String name = StarProtectVehicleApp.scanner.nextLine();
System.out.print("Enter Date of Birth (dd/MM/yyyy): ");
String dob = StarProtectVehicleApp.scanner.nextLine();
System.out.print("Enter Joining Date (dd/MM/yyyy): ");
String joiningDate = StarProtectVehicleApp.scanner.nextLine();

// Ask for password


System.out.print("Enter Underwriter Password: ");
String password = StarProtectVehicleApp.scanner.nextLine();

Underwriter underwriter = new


Underwriter(StarProtectVehicleApp.underwriterIdCounter++, name, dob, joiningDate,
password);
StarProtectVehicleApp.underwriters.add(underwriter);
System.out.println("Underwriter Registered Successfully. Generated ID: " +
underwriter.id);
}

public void searchUnderwriter() {


System.out.print("Enter Underwriter ID to search: ");
int id = StarProtectVehicleApp.scanner.nextInt();
StarProtectVehicleApp.scanner.nextLine(); // consume newline
for (Underwriter underwriter : StarProtectVehicleApp.underwriters) {
if (underwriter.id == id) {
System.out.println(underwriter);
return;
}
}
System.out.println("Underwriter not found.");
}

public void updateUnderwriterPassword() {


System.out.print("Enter Underwriter ID: ");
int id = StarProtectVehicleApp.scanner.nextInt();
StarProtectVehicleApp.scanner.nextLine(); // consume newline
for (Underwriter underwriter : StarProtectVehicleApp.underwriters) {
if (underwriter.id == id) {
System.out.print("Enter new password: ");
underwriter.password = StarProtectVehicleApp.scanner.nextLine();
System.out.println("Password updated successfully.");
return;
}
}
System.out.println("Underwriter not found.");
}

public void deleteUnderwriter() {


System.out.print("Enter Underwriter ID: ");
int id = StarProtectVehicleApp.scanner.nextInt();
StarProtectVehicleApp.scanner.nextLine(); // consume newline
for (Underwriter underwriter : StarProtectVehicleApp.underwriters) {
if (underwriter.id == id) {
StarProtectVehicleApp.underwriters.remove(underwriter);
System.out.println("Underwriter deleted successfully.");
return;
}
}
System.out.println("Underwriter not found.");
}

public void viewAllUnderwriters() {


if (StarProtectVehicleApp.underwriters.isEmpty()) {
System.out.println("No underwriters registered.");
} else {
for (Underwriter underwriter : StarProtectVehicleApp.underwriters) {
System.out.println(underwriter);
}
}
}

public void viewVehiclesByUnderwriterId() {


System.out.print("Enter Underwriter ID: ");
int id = StarProtectVehicleApp.scanner.nextInt();
StarProtectVehicleApp.scanner.nextLine(); // consume newline
for (Underwriter underwriter : StarProtectVehicleApp.underwriters) {
if (underwriter.id == id) {
underwriter.viewPolicies();
return;
}
}
System.out.println("Underwriter not found.");
}
}

class Underwriter {
int id;
String name;
String dob;
String joiningDate;
String password;
ArrayList<VehicleInsurance> vehicles = new ArrayList<>();

public Underwriter(int id, String name, String dob, String joiningDate, String
password) {
this.id = id;
this.name = name;
this.dob = dob;
this.joiningDate = joiningDate;
this.password = password;
}

public void createVehicleInsurance() {


System.out.print("Enter Vehicle Number: ");
String vehicleNo = StarProtectVehicleApp.scanner.nextLine();
System.out.print("Enter Vehicle Type (2-wheeler/4-wheeler): ");
String vehicleType = StarProtectVehicleApp.scanner.nextLine();
System.out.print("Enter Customer Name: ");
String customerName = StarProtectVehicleApp.scanner.nextLine();
System.out.print("Enter Engine Number: ");
int engineNo = StarProtectVehicleApp.scanner.nextInt();
System.out.print("Enter Chassis Number: ");
int chassisNo = StarProtectVehicleApp.scanner.nextInt();
System.out.print("Enter Phone Number: ");
long phoneNo = StarProtectVehicleApp.scanner.nextLong();
StarProtectVehicleApp.scanner.nextLine(); // Consume newline
System.out.print("Enter Insurance Type (Full/ThirdParty): ");
String insuranceType = StarProtectVehicleApp.scanner.nextLine();
double premiumAmt = calculatePremium(insuranceType);

Date fromDate = new Date(); // Current date


Date toDate = new Date(fromDate.getTime() + (365L * 24 * 60 * 60 *
1000)); // 1 year later

VehicleInsurance insurance = new


VehicleInsurance(StarProtectVehicleApp.policyIdCounter++, vehicleNo,
vehicleType, customerName, engineNo, chassisNo, phoneNo,
insuranceType, premiumAmt, fromDate, toDate);
vehicles.add(insurance);
System.out.println("Vehicle Insurance created successfully. Policy ID: " +
insurance.policyId);
}

private double calculatePremium(String insuranceType) {


switch (insuranceType) {
case "Full":
return 1000; // Example premium amount for full insurance
case "ThirdParty":
return 500; // Example premium amount for third-party insurance
default:
return 0;
}
}

public void renewPolicy() {


System.out.print("Enter Policy ID to renew: ");
int policyId = StarProtectVehicleApp.scanner.nextInt();
for (VehicleInsurance insurance : vehicles) {
if (insurance.policyId == policyId) {
insurance.renew();
System.out.println("Policy renewed successfully.");
return;
}
}
System.out.println("Policy not found.");
}

public void changePolicyType() {


System.out.print("Enter Policy ID to change type: ");
int policyId = StarProtectVehicleApp.scanner.nextInt();
StarProtectVehicleApp.scanner.nextLine(); // consume newline
for (VehicleInsurance insurance : vehicles) {
if (insurance.policyId == policyId) {
System.out.print("Enter new Insurance Type (Full/ThirdParty): ");
String newType = StarProtectVehicleApp.scanner.nextLine();
insurance.insuranceType = newType;
insurance.premiumAmount = calculatePremium(newType); // Recalculate
premium
System.out.println("Policy type changed successfully.");
return;
}
}
System.out.println("Policy not found.");
}

public void viewPolicies() {


if (vehicles.isEmpty()) {
System.out.println("No policies available for this underwriter.");
} else {
for (VehicleInsurance insurance : vehicles) {
System.out.println(insurance);
}
}
}

@Override
public String toString() {
return "Underwriter ID: " + id + ", Name: " + name + ", DOB: " + dob + ",
Joining Date: " + joiningDate;
}
}

class VehicleInsurance {
int policyId;
String vehicleNumber;
String vehicleType;
String customerName;
int engineNumber;
int chassisNumber;
long phoneNumber;
String insuranceType;
double premiumAmount;
Date fromDate;
Date toDate;

public VehicleInsurance(int policyId, String vehicleNumber, String vehicleType,


String customerName,
int engineNumber,
int chassisNumber, long phoneNumber, String insuranceType, double
premiumAmount, Date fromDate,
Date toDate) {
this.policyId = policyId;
this.vehicleNumber = vehicleNumber;
this.vehicleType = vehicleType;
this.customerName = customerName;
this.engineNumber = engineNumber;
this.chassisNumber = chassisNumber;
this.phoneNumber = phoneNumber;
this.insuranceType = insuranceType;
this.premiumAmount = premiumAmount;
this.fromDate = fromDate;
this.toDate = toDate;
}

public void renew() {


this.fromDate = new Date();
this.toDate = new Date(fromDate.getTime() + (365L * 24 * 60 * 60 *
1000)); // Extend for 1 year
}

@Override
public String toString() {
return "Policy ID: " + policyId + ", Vehicle No: " + vehicleNumber + ",
Type: " + vehicleType +
", Customer: " + customerName + ", Premium: " + premiumAmount + ",
Validity: " + fromDate + " to "
+ toDate;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy