ns22
Q1]
import java.util.Scanner;
public class EmployeeMenu {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
EmployeeDatabase database = new EmployeeDatabase();
int choice;
do {
System.out.println("Menu:");
System.out.println("1. Insert");
System.out.println("2. Update");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
database.insertEmployee();
break;
case 2:
database.updateEmployee();
break;
case 3:
database.displayEmployees();
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 4);
scanner.close();
}
}
class EmployeeDatabase {
private int ENo = 1; // Initial employee number
private Employee[] employees = new Employee[10]; // Assuming maximum of 10 employees
public void insertEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter employee name: ");
String EName = scanner.nextLine();
System.out.print("Enter salary: ");
double salary = scanner.nextDouble();
Employee employee = new Employee(ENo++, EName, salary);
for (int i = 0; i < employees.length; i++) {
if (employees[i] == null) {
employees[i] = employee;
System.out.println("Employee inserted successfully!");
return;
}
}
System.out.println("Employee database is full!");
}
public void updateEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter employee number to update: ");
int searchENo = scanner.nextInt();
for (int i = 0; i < employees.length; i++) {
if (employees[i] != null && employees[i].ENo == searchENo) {
System.out.print("Enter new employee name: ");
String newEName = scanner.nextLine(); // Consume newline
newEName = scanner.nextLine(); // Read new employee name
System.out.print("Enter new salary: ");
double newSalary = scanner.nextDouble();
employees[i].EName = newEName;
employees[i].Salary = newSalary;
System.out.println("Employee updated successfully!");
return;
}
}
System.out.println("Employee not found!");
}
public void displayEmployees() {
System.out.println("Employee details:");
for (Employee employee : employees) {
if (employee != null) {
System.out.println(employee);
}
}
}
}
class Employee {
int ENo;
String EName;
double Salary;
public Employee(int ENo, String EName, double Salary) {
this.ENo = ENo;
this.EName = EName;
this.Salary = Salary;
}
@Override
public String toString() {
return "ENo: " + ENo + ", EName: " + EName + ", Salary: " + Salary;
}
}
Q2]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
GreetUserServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalTime;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/GreetUserServlet")
public class GreetUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
LocalTime currentTime = LocalTime.now();
int hour = currentTime.getHour();
String greeting;
if (hour >= 4 && hour < 12) {
greeting = "Good morning";
} else if (hour >= 12 && hour < 18) {
greeting = "Good afternoon";
} else {
greeting = "Good evening";
}
out.println("<h2>" + greeting + ", " + username + "!</h2>");
}
}