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

Java Employee and Student Management

The document contains Java code for three applications: a bonus calculator for employees, a student pass filter, and a sales analysis tool. Each application demonstrates the use of object-oriented programming, functional interfaces, and Java streams. The code includes classes for Employee, Student, and SalesRecord, along with their respective functionalities and output displays.

Uploaded by

street28gamer
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)
20 views8 pages

Java Employee and Student Management

The document contains Java code for three applications: a bonus calculator for employees, a student pass filter, and a sales analysis tool. Each application demonstrates the use of object-oriented programming, functional interfaces, and Java streams. The code includes classes for Employee, Student, and SalesRecord, along with their respective functionalities and output displays.

Uploaded by

street28gamer
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

NAME- RIYA DESHMANKAR

ROLL NO.-2300290100212
SECTION-C

Ques1.)
import [Link].*;
class Employee {
private String name;
private double salary;
private String type; // "Permanent" or "Contract"
public Employee(String name, double salary, String type) {
[Link] = name;
[Link] = salary;
[Link] = type;
}
public String getName() { return name; }
public double getSalary() { return salary; }
public String getType() { return type; }
}
@FunctionalInterface
interface BonusCalculator {
double calculateBonus(Employee e);
}

public class BonusApp {


public static void main(String[] args) {
List<Employee> employees = [Link](
new Employee("Neelakshi", 50000, "Permanent"),
new Employee("Sanvi", 40000, "Contract"),
new Employee("Vidhika", 60000, "Permanent")
);
BonusCalculator permanentBonus = e -> [Link]() * 0.20;
BonusCalculator contractBonus = e -> [Link]() * 0.10;

for (Employee e : employees) {


BonusCalculator calculator =
[Link]().equalsIgnoreCase("Permanent") ?
permanentBonus : contractBonus;
double bonus = [Link](e);
[Link]([Link]() + " (" + [Link]() + ") Bonus: "
+ bonus);}}}
Output
Ques2.)
import [Link].*;
import [Link].*;
class Student {
private String name;
private int marks;
public Student(String name, int marks) {
[Link] = name;
[Link] = marks;
}

public String getName() { return name; }

public boolean isPass() {


return marks >= 40;
}
}
public class StudentApp {
public static void main(String[] args) {
List<Student> students = [Link](
new Student("Riya", 85),
new Student("Sagar", 32),
new Student("Anjali", 47),
new Student("Aman", 20)
);
[Link]("Passed Students:");
[Link]()
.filter(Student::isPass)
.map(Student::getName)
.forEach([Link]::println);
}
}
Output

Ques3.)
import [Link].*;
import [Link].*;
import [Link];
class SalesRecord {
private String productName;
private String region;
private double salesAmount;

public SalesRecord(String productName, String region, double


salesAmount) {
[Link] = productName;
[Link] = region;
[Link] = salesAmount;
}

public String getRegion() { return region; }


public double getSalesAmount() { return salesAmount; }
}

public class SalesAnalysisApp {


public static void main(String[] args) {
List<SalesRecord> records = [Link](
new SalesRecord("Product A", "North", 6000),
new SalesRecord("Product B", "South", 3000),
new SalesRecord("Product C", "East", 7000),
new SalesRecord("Product D", "North", 8000),
new SalesRecord("Product E", "West", 2000),
new SalesRecord("Product F", "East", 10000)
);

Map<String, Double> totalSalesByRegion = [Link]()


.filter(r -> [Link]() >= 5000)
.collect([Link](
SalesRecord::getRegion,
[Link](SalesRecord::getSalesAmount)
));

[Link]("Total Sales by Region (Descending):");


[Link]().stream()
.sorted(Entry.<String, Double>comparingByValue().reversed())
.forEach(e -> [Link]([Link]() + ": " + [Link]()));
}
}
Output

Common questions

Powered by AI

To include more complex criteria for passing, the StudentApp class could incorporate lambda expressions or Predicate compositions to define multiple conditional checks. For instance, criteria could include passing specific subjects or achieving a particular average. Streams allow easy chaining of such conditions without altering the data source structure .

Both BonusApp and SalesAnalysisApp leverage Java streams for data processing, but with different complexity levels. BonusApp uses lambda expressions for simple, linear processing of a list. In contrast, SalesAnalysisApp involves grouping and sorting, making it more complex but efficient in operations on larger datasets. Streams enhance readability by reducing code verbosity and providing a functional approach to data operations .

The SalesAnalysisApp uses the Collectors.summingDouble method within Collectors.groupingBy to aggregate sales amounts per region. The results are then sorted in descending order by sales amount using a comparator and displayed by iterating over the sorted entries .

Immutability in the provided Java classes (Employee, Student, SalesRecord) is achieved by keeping fields private and not providing setters, ensuring object states cannot be changed externally after instantiation. This leads to thread-safe code, easier debugging, and reliability as immutable objects are inherently stable .

The bonus for permanent employees is calculated as 20% of their salary, while for contract employees, it is 10% of their salary. This differentiation is achieved using a functional interface, BonusCalculator, and lambda expressions that define different bonus calculation strategies based on the employee type .

Streams offer a high-level abstraction for processing elements in a collection in a declarative manner. Operations like filtering, mapping, and reduction allow concise manipulation of data. In the applications, streams enable operations such as filtering passed students and aggregating sales, which improve clarity and reduce boilerplate code .

The StudentApp uses stream operations to filter students who have passed by calling the isPass method, which checks if the marks are 40 or above. It then maps these students to their names and prints them. The output is a list of passed students: Riya and Anjali .

Lambda expressions in the BonusApp allow easy modifications of the bonus calculation logic without altering the structural code involving employees. Future enhancements could include different bonus percentages based on performance metrics or tenure. Adding extra conditions in lambda expressions for these parameters would easily extend functionality .

SalesRecord encapsulates data by having private fields for productName, region, and salesAmount, exposing only the getters. This design prevents unauthorized access and modification of the internal state, safeguarding data integrity. Encapsulation also enhances maintainability and flexibility in code evolution .

In a production environment, the use of lambda expressions and streams, as demonstrated, can significantly enhance code readability and maintainability, reducing the likelihood of errors. However, the performance might be impacted in large-scale data operations due to overheads associated with stream setups. Profiling and performance optimizations are crucial to ensure efficiency .

You might also like