Changed around line 1
+ // Data Model Class
+ class DatosModel {
+ constructor(codigo, nombre, apellido, departamento, fecha_nacimiento) {
+ this.codigo = codigo;
+ this.nombre = nombre;
+ this.apellido = apellido;
+ this.departamento = departamento;
+ this.fecha_nacimiento = fecha_nacimiento;
+ }
+ }
+
+ // DAO (Data Access Object) Class
+ class DatosDAO {
+ constructor() {
+ this.storage = window.localStorage;
+ this.STORAGE_KEY = 'datos_personales';
+ }
+
+ getAll() {
+ const data = this.storage.getItem(this.STORAGE_KEY);
+ return data ? JSON.parse(data) : [];
+ }
+
+ save(datos) {
+ const allData = this.getAll();
+ datos.codigo = this.generateId(allData);
+ allData.push(datos);
+ this.storage.setItem(this.STORAGE_KEY, JSON.stringify(allData));
+ return datos;
+ }
+
+ update(datos) {
+ const allData = this.getAll();
+ const index = allData.findIndex(item => item.codigo === datos.codigo);
+ if (index !== -1) {
+ allData[index] = datos;
+ this.storage.setItem(this.STORAGE_KEY, JSON.stringify(allData));
+ return true;
+ }
+ return false;
+ }
+
+ delete(codigo) {
+ const allData = this.getAll();
+ const filtered = allData.filter(item => item.codigo !== codigo);
+ this.storage.setItem(this.STORAGE_KEY, JSON.stringify(filtered));
+ }
+
+ generateId(allData) {
+ return allData.length > 0 ? Math.max(...allData.map(item => item.codigo)) + 1 : 1;
+ }
+ }
+
+ // Service Class
+ class DatosService {
+ constructor() {
+ this.dao = new DatosDAO();
+ }
+
+ getAllDatos() {
+ return this.dao.getAll();
+ }
+
+ saveDatos(datosModel) {
+ return this.dao.save(datosModel);
+ }
+
+ updateDatos(datosModel) {
+ return this.dao.update(datosModel);
+ }
+
+ deleteDatos(codigo) {
+ this.dao.delete(codigo);
+ }
+ }
+
+ // UI Controller
+ document.addEventListener('DOMContentLoaded', () => {
+ const service = new DatosService();
+ const form = document.getElementById('dataForm');
+ const addNewBtn = document.getElementById('addNew');
+ const viewAllBtn = document.getElementById('viewAll');
+ const formSection = document.getElementById('formSection');
+ const tableSection = document.getElementById('tableSection');
+
+ function refreshTable() {
+ const tableBody = document.getElementById('dataTableBody');
+ tableBody.innerHTML = '';
+
+ service.getAllDatos().forEach(dato => {
+ const row = document.createElement('tr');
+ row.innerHTML = `
+
${dato.codigo} | +
${dato.nombre} | +
${dato.apellido} | +
${dato.departamento} | +
${dato.fecha_nacimiento} | + `;
+ tableBody.appendChild(row);
+ });
+ }
+
+ addNewBtn.addEventListener('click', () => {
+ formSection.classList.remove('hidden');
+ tableSection.classList.add('hidden');
+ });
+
+ viewAllBtn.addEventListener('click', () => {
+ formSection.classList.add('hidden');
+ tableSection.classList.remove('hidden');
+ refreshTable();
+ });
+
+ form.addEventListener('submit', (e) => {
+ e.preventDefault();
+
+ const datosModel = new DatosModel(
+ null,
+ form.nombre.value,
+ form.apellido.value,
+ form.departamento.value,
+ form.fecha_nacimiento.value
+ );
+
+ service.saveDatos(datosModel);
+ form.reset();
+ formSection.classList.add('hidden');
+ tableSection.classList.remove('hidden');
+ refreshTable();
+ });
+
+ window.deleteDatos = (codigo) => {
+ if (confirm('Are you sure you want to delete this record?')) {
+ service.deleteDatos(codigo);
+ refreshTable();
+ }
+ };
+
+ // Initial load
+ refreshTable();
+ });