public class Triangle extends Polygon {
private double side1;
private double side2;
private double side3;
public Triangle(int numberOfSides, double side1, double side2, double side3) {
super(numberOfSides);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
@Override
public double area() {
double p = side1 + side2 + side3;
return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
}
}
public class Main {
static Scanner sc = new Scanner(System.in);
static List<Polygon> polygons = new ArrayList<>();
public static void main(String[] args) {
fillPolygon();
showData();
}
public static void fillPolygon() {
int opcion = 0;
do {
System.out.println("Digite que poligono desea:");
System.out.println("1. Triangulo");
System.out.println("2. Rectangulo");
System.out.println("3. Salir");
System.out.print("Opcion: ");
opcion = sc.nextInt();
switch (opcion) {
case 1:
fillTriangle();
break;
case 2:
fillRectangle();
break;
case 3:
break;
}
} while (opcion != 3);
}
public static void fillTriangle() {
double side1, side2, side3;
System.out.println("\nDigite el lado1 del triangulo: ");
side1 = sc.nextDouble();
System.out.println("Digite el lado2 del triangulo: ");
side2 = sc.nextDouble();
System.out.print("Digite el lado3 del triangulo: ");
side3 = sc.nextDouble();
Polygon t = new Triangle(3, side1, side2, side3);
polygons.add(t);
}
public static void fillRectangle() {
double side1, side2;
System.out.print("\nDigite el lado1 del Rectangle: ");
side1 = sc.nextDouble();
System.out.print("Digite el lado2 del Rectangle: ");
side2 = sc.nextDouble();
Polygon r = new Rectangle(2, side1, side2);
polygons.add(r);
}
public static void showData() {
for (Polygon p : polygons) {
System.out.println(p);
}
}
}
2. Diseñe una clase abstracta de nombre Empleado que tenga atributos protegidos para el nombre, apellidos, dni, métodos get-set, métodos abstractos para ingresos(), bonificaciones(), descuentos().
Un método no abstracto que retorne el sueldoNeto() y otro método no abstracto que retorne la información correspondiente al nombre, apellidos, dni, ingresos, bonificaciones, descuentos, sueldo neto de cualquier empleado.
Aplique polimorfismo y diseñe una clase hija de nombre EmpleadoVendedor que tenga atributos para monto vendido y porcentaje de comisión, constructor, métodos get-set, desarrollo de métodos abstractos.
Luego, diseñe una clase hija de nombre EmpleadoPermanente que tenga atributos para sueldo basico y afiliación (afp ó snp), constructor, métodos get-set, desarrollo de métodos abstractos.
Diseñe una clase administradora de nombre ColeccionEmpleados que permita la administración de objetos de tipo EmpleadoVendedor y/o EmpleadoPermanente a la vez, utilizando un objeto de la clase ArrayList.
public abstract class Employee {
private String name;
private String lastName;
private String dni;
public Employee(String name, String lastName, String dni) {
this.name = name;
this.lastName = lastName;
this.dni = dni;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDni() {
return dni;
}
public void setDni(String dni) {
this.dni = dni;
}
public abstract double income();
public abstract double bonuses();
public abstract double discounts();
@Override
public String toString() {
return "Empleado{" +
"name='" + name + '\'' +
", lastName='" + lastName + '\'' +
", dni='" + dni + '\'' +
", income='" + income() + '\'' +
", bonuses='" + bonuses() + '\'' +
", discounts='" + discounts() + '\'' +
'}';
}
}
public class SellerEmployee extends Employee {
private double amountSold;
private double commissionPercentage;
public SellerEmployee(String name, String lastName, String dni, double amountSold, double commissionPercentage) {
super(name, lastName, dni);
this.amountSold = amountSold;
this.commissionPercentage = commissionPercentage;
}
public double getAmountSold() {
return amountSold;
}
public void setAmountSold(double amountSold) {
this.amountSold = amountSold;
}
public double getCommissionPercentage() {
return commissionPercentage;
}
public void setCommissionPercentage(double commissionPercentage) {
this.commissionPercentage = commissionPercentage;
}
@Override
public double income() {
return amountSold * commissionPercentage;
}
@Override
public double bonuses() {
return amountSold * 0.15;
}
@Override
public double discounts() {
return income() * 0.22;
}
}
public class PermanentEmployee extends Employee {
private double basicSalary;
private int membership;
public PermanentEmployee(String name, String lastName, String dni, double basicSalary, int membership) {
super(name, lastName, dni);
this.basicSalary = basicSalary;
this.membership = membership;
}
public double getBasicSalary() {
return basicSalary;
}
public void setBasicSalary(double basicSalary) {
this.basicSalary = basicSalary;
}
public int getMembership() {
return membership;
}
public void setMembership(int membership) {
this.membership = membership;
}
@Override
public double income() {
return basicSalary;
}
@Override
public double bonuses() {
if (membership == 1)
return basicSalary * 0.13;
else
return basicSalary * 0.20;
}
@Override
public double discounts() {
return bonuses() * 0.10;
}
}
public class EmployeesCollection {
List<Employee> employees;
public EmployeesCollection() {
employees = new ArrayList<>();
}
public int size() {
return employees.size();
}
public void add(Employee e) {
employees.add(e);
}
public void delete(Employee e) {
employees.remove(e);
}
public Employee get(int i) {
return employees.get(i);
}
}
public static void main(String[] args) {
Employee a = new SellerEmployee("Juan", "Perez", "78767574", 300, 5);
Employee b = new SellerEmployee("Luis", "Garcia", "75747372", 500, 10);
Employee c = new PermanentEmployee("Raul", "Romero", "54213356", 600, 150);
EmployeesCollection ec = new EmployeesCollection();
ec.add(a);
ec.add(b);
ec.add(c);
listar(ec);
}
public static void listar(EmployeesCollection x) {
for (int i = 0; i < x.size(); i++) {
Employee e = x.get(i);
System.out.println(e);
if (e instanceof SellerEmployee) {
System.out.println(((SellerEmployee) e).getAmountSold());
System.out.println(((SellerEmployee) e).getCommissionPercentage());
} else {
System.out.println(((PermanentEmployee) e).getBasicSalary());
System.out.println(((PermanentEmployee) e).getMembership());
}
}
}
No hay comentarios:
Publicar un comentario