Skip to content

Commit 2bdaf6b

Browse files
committed
Adding Actividad32_Ampliación
1 parent bfe33d0 commit 2bdaf6b

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package unit11.Actividad32_Ampliacion;
2+
3+
import Functions.GetData;
4+
5+
import java.io.*;
6+
import java.time.LocalDateTime;
7+
import java.util.ArrayList;
8+
9+
/*
10+
Con la clase Llamada de la Actividad 31 del Tema 9 crea un registro de las llamadas efectuada en una centralita,
11+
que se guardarán en el archivo binario centralita.dat. Al arrancar la aplicación se leerán los datos del archivo
12+
y a continuación se mostrará el menú:
13+
1. Nuevo registro de Llamada
14+
2. Listar las llamadas de un número de teléfono
15+
3. Listas todas las llamadas
16+
4. Salir
17+
18+
En el apartado 1 las fechas y horas se introducirán como cadenas en formato dd/MM/yyyy y hh:mm respectivamente.
19+
En los apartados 2 y 3 mostrará el número de teléfono del titular el del interlocutor, la fecha y hora de inicio
20+
y la duración en minutos de cada llamada. Los registros se insertarán en una tabla por su orden natural.
21+
Al terminar la aplicación se guardarán la tabla actualizada en el mismo archivo.
22+
*/
23+
public class Actividad32 {
24+
private static final String FICHERO = "src/unit11/Actividad32_Ampliacion/centralita.dat";
25+
private static ArrayList<Llamada> llamadas = new ArrayList<>();
26+
public static void main(String[] args) {
27+
cargarLlamadas();
28+
29+
boolean exit = false;
30+
int option = 0;
31+
32+
while (!exit) {
33+
menu();
34+
option = GetData.getInt("Introduce una opción: ", 1, 4);
35+
switch (option) {
36+
case 1 -> llamadas.add(getLlamada());
37+
case 2 -> {
38+
String numero = GetData.getString("Introduce el número de teléfono: ");
39+
mostrarLlamadasDeNumero(numero);
40+
}
41+
case 3 -> llamadas.forEach(System.out::println);
42+
case 4 -> {
43+
exit = true;
44+
guardarLlamadas();
45+
}
46+
default -> System.out.println("Opción no válida");
47+
}
48+
}
49+
}
50+
51+
private static Llamada getLlamada() {
52+
String numero = GetData.getString("Introduce el número de teléfono: ");
53+
String numeroInterlocutor = GetData.getString("Introduce el número de teléfono del interlocutor: ");
54+
boolean esSaliente = GetData.getBoolean("¿Es saliente? (si/no): ");
55+
LocalDateTime fechaInicio = GetData.getDateTime("Introduce la fecha de la llamada: ");
56+
LocalDateTime fechaFin = GetData.getDateTime("Introduce la fecha de la llamada: ");
57+
ZonaTarifa zonaTarifa = getZonaTarifa();
58+
59+
return new Llamada(numero, numeroInterlocutor, esSaliente, fechaInicio, fechaFin, zonaTarifa);
60+
}
61+
62+
private static ZonaTarifa getZonaTarifa() {
63+
for (ZonaTarifa zonaTarifa : ZonaTarifa.values()) {
64+
System.out.println(zonaTarifa.ordinal() + ". " + zonaTarifa);
65+
}
66+
return ZonaTarifa.values()[GetData.getInt("Introduce la zona tarifa: ", 0, ZonaTarifa.values().length - 1)];
67+
}
68+
69+
private static void mostrarLlamadasDeNumero(String numero) {
70+
ArrayList<Llamada> llamadasRealizadas = new ArrayList<>();
71+
for (Llamada llamada : llamadas) {
72+
if (llamada.getNumeroTelefonoCliente().equals(numero)) {
73+
llamadasRealizadas.add(llamada);
74+
}
75+
}
76+
77+
if (llamadasRealizadas.isEmpty()) {
78+
System.out.println("No hay llamadas realizadas");
79+
} else {
80+
llamadasRealizadas.forEach(System.out::println);
81+
}
82+
}
83+
84+
private static void menu() {
85+
System.out.println("""
86+
1. Nuevo registro de Llamada
87+
2. Listar las llamadas de un número de teléfono
88+
3. Listas todas las llamadas
89+
4. Salir""");
90+
}
91+
92+
private static void cargarLlamadas() {
93+
if (new File(FICHERO).exists()) {
94+
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FICHERO))) {
95+
llamadas = (ArrayList<Llamada>) ois.readObject();
96+
} catch (IOException | ClassNotFoundException e) {
97+
System.out.println("Error al cargar las llamadas");
98+
}
99+
}
100+
}
101+
102+
private static void guardarLlamadas() {
103+
try (ObjectOutputStream ois = new ObjectOutputStream(new FileOutputStream(FICHERO))) {
104+
ois.writeObject(llamadas);
105+
} catch (IOException e) {
106+
System.out.println("Error al guardar las llamadas");
107+
}
108+
}
109+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package unit11.Actividad32_Ampliacion;
2+
3+
import java.io.Serializable;
4+
import java.time.LocalDateTime;
5+
6+
public class Llamada implements Comparable<Llamada>, Serializable {
7+
private String numeroTelefonoCliente;
8+
private String numeroInterlocutor;
9+
private boolean esSaliente;
10+
private LocalDateTime fechaHoraInicio;
11+
private LocalDateTime fechaHoraFin;
12+
private ZonaTarifa zonaInterlocutor;
13+
private int duracionLlamada;
14+
private double costeLlamada;
15+
16+
public Llamada(String numeroTelefonoCliente, String numeroInterlocutor, boolean esSaliente, LocalDateTime fechaHoraInicio, LocalDateTime fechaHoraFin, ZonaTarifa zonaInterlocutor) {
17+
if (numeroTelefonoCliente == null || numeroInterlocutor == null || fechaHoraInicio == null || fechaHoraFin == null || zonaInterlocutor == null) {
18+
throw new IllegalArgumentException("Error: ninguno de los parámetros puede ser nulo");
19+
}
20+
if (numeroTelefonoCliente.isEmpty() || numeroInterlocutor.isEmpty()) {
21+
throw new IllegalArgumentException("Error: ninguno de los parámetros puede ser una cadena vacía");
22+
}
23+
if (fechaHoraInicio.isAfter(fechaHoraFin)) {
24+
throw new IllegalArgumentException("Error: la fecha y hora de inicio no puede ser posterior a la fecha y hora de fin");
25+
}
26+
27+
this.numeroTelefonoCliente = numeroTelefonoCliente;
28+
this.numeroInterlocutor = numeroInterlocutor;
29+
this.esSaliente = esSaliente;
30+
this.fechaHoraInicio = fechaHoraInicio;
31+
this.fechaHoraFin = fechaHoraFin;
32+
this.zonaInterlocutor = zonaInterlocutor;
33+
this.duracionLlamada = (int) fechaHoraInicio.until(fechaHoraFin, java.time.temporal.ChronoUnit.MINUTES);
34+
this.costeLlamada = zonaInterlocutor.getCoste() * duracionLlamada;
35+
}
36+
37+
38+
39+
public int getDuracionLlamada() {
40+
return duracionLlamada;
41+
}
42+
43+
public double getCosteLlamada() {
44+
return costeLlamada;
45+
}
46+
47+
public boolean isEsSaliente() {
48+
return esSaliente;
49+
}
50+
51+
public String getNumeroTelefonoCliente() {
52+
return numeroTelefonoCliente;
53+
}
54+
55+
@Override
56+
public int compareTo(Llamada o) {
57+
int resultado = this.numeroTelefonoCliente.compareTo(o.numeroTelefonoCliente);
58+
if (resultado == 0) {
59+
resultado = this.fechaHoraInicio.compareTo(o.fechaHoraInicio);
60+
}
61+
return resultado;
62+
}
63+
64+
@Override
65+
public String toString() {
66+
return "Numero de teléfono del cliente: " + numeroTelefonoCliente + "\n"
67+
+ "Numero del interlocutor: " + numeroInterlocutor + "\n"
68+
+ "Fecha y hora de inicio: " + fechaHoraInicio + "\n"
69+
+ "Fecha y hora de fin: " + fechaHoraFin + "\n"
70+
+ "Duración de la llamada: " + duracionLlamada + " minutos\n"
71+
+ "Coste de la llamada: " + costeLlamada + " euros\n";
72+
}
73+
}
74+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package unit11.Actividad32_Ampliacion;
2+
3+
public class OrdenarLlamadaPorCoste implements java.util.Comparator<Llamada>{
4+
@Override
5+
public int compare(Llamada o1, Llamada o2) {
6+
return Double.compare(o1.getCosteLlamada(), o2.getCosteLlamada());
7+
}
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package unit11.Actividad32_Ampliacion;
2+
3+
import java.io.Serializable;
4+
5+
public enum ZonaTarifa implements Serializable {
6+
ZONA_A(0.10),
7+
ZONA_B(0.15),
8+
ZONA_C(0.20),
9+
ZONA_D(0.25),
10+
ZONA_E(0.30);
11+
12+
private final double costo;
13+
14+
ZonaTarifa(double costo) {
15+
this.costo = costo;
16+
}
17+
18+
public double getCoste() {
19+
return costo;
20+
}
21+
}
Binary file not shown.

0 commit comments

Comments
 (0)
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