|
| 1 | +/* |
| 2 | + * EJERCICIO: |
| 3 | + * - Muestra ejemplos de creación de todas las estructuras soportadas por defecto en tu lenguaje. |
| 4 | + * - Utiliza operaciones de inserción, borrado, actualización y ordenación. |
| 5 | + * |
| 6 | + * DIFICULTAD EXTRA (opcional): |
| 7 | + * Crea una agenda de contactos por terminal. |
| 8 | + * - Debes implementar funcionalidades de búsqueda, inserción, actualización |
| 9 | + * y eliminación de contactos. |
| 10 | + * - Cada contacto debe tener un nombre y un número de teléfono. |
| 11 | + * - El programa solicita en primer lugar cuál es la operación que se quiere realizar, |
| 12 | + * y a continuación los datos necesarios para llevarla a cabo. |
| 13 | + * - El programa no puede dejar introducir números de teléfono no númericos y con más |
| 14 | + * de 11 dígitos (o el número de dígitos que quieras). |
| 15 | + * - También se debe proponer una operación de finalización del programa. |
| 16 | + */ |
| 17 | + |
| 18 | +// Estructuras de datos soportadas por defecto en Dart: |
| 19 | +// - List |
| 20 | +// - Set |
| 21 | +// - Map |
| 22 | + |
| 23 | +import 'dart:io'; |
| 24 | + |
| 25 | +void main() { |
| 26 | + listExample(); |
| 27 | + setExample(); |
| 28 | + mapExample(); |
| 29 | + |
| 30 | + // Agenda de contactos |
| 31 | + agendaContactos(); |
| 32 | +} |
| 33 | + |
| 34 | +// List - example insert, delete, update, sort |
| 35 | +void listExample() { |
| 36 | + List<int> list = [2, 5, 3, 2, 1]; |
| 37 | + print('\n----- LIST -----:'); |
| 38 | + print('Initial list:' + list.toString()); |
| 39 | + |
| 40 | + // Insert |
| 41 | + list.add(6); |
| 42 | + print('Insert result:' + list.toString()); |
| 43 | + |
| 44 | + // Delete |
| 45 | + list.remove(6); |
| 46 | + print('Delete result:' + list.toString()); |
| 47 | + |
| 48 | + // Update |
| 49 | + list[0] = 0; |
| 50 | + print('Update result:' + list.toString()); |
| 51 | + |
| 52 | + // Sort |
| 53 | + list.sort(); |
| 54 | + print('Sort list result:' + list.toString()); |
| 55 | +} |
| 56 | + |
| 57 | +//Set - example insert, delete, update, sort |
| 58 | +void setExample() { |
| 59 | + Set<int> set = {2, 5, 3, 4, 1}; |
| 60 | + print('\n----- SET -----:'); |
| 61 | + print('Initial set:' + set.toString()); |
| 62 | + |
| 63 | + // Insert |
| 64 | + set.add(6); |
| 65 | + print('Insert result:' + set.toString()); |
| 66 | + |
| 67 | + // Delete |
| 68 | + set.remove(6); |
| 69 | + print('Delete result:' + set.toString()); |
| 70 | + |
| 71 | + // Update |
| 72 | + // set[0] = 0; // Error: Unhandled exception: |
| 73 | + // Unsupported operation: Cannot modify an unmodifiable set |
| 74 | + print('Update result: Cannot modify an unmodifiable set'); |
| 75 | + |
| 76 | + // Sort |
| 77 | + // set.sort(); // Error: Unhandled exception: |
| 78 | + // Unsupported operation: Cannot sort an unmodifiable set |
| 79 | + print('Sort set result: Cannot sort an unmodifiable set'); |
| 80 | +} |
| 81 | + |
| 82 | +//Map - example insert, delete, update, sort |
| 83 | +void mapExample() { |
| 84 | + Map<int, String> map = {1: 'one', 2: 'two', 3: 'three'}; |
| 85 | + print('\n----- MAP -----:'); |
| 86 | + print('Initial map:' + map.toString()); |
| 87 | + |
| 88 | + // Insert |
| 89 | + map[4] = 'four'; |
| 90 | + print('Insert result:' + map.toString()); |
| 91 | + |
| 92 | + // Delete |
| 93 | + map.remove(2); |
| 94 | + print('Delete result:' + map.toString()); |
| 95 | + |
| 96 | + // Update |
| 97 | + map.update(1, (value) => 'uno'); |
| 98 | + print('Update result:' + map.toString()); |
| 99 | + |
| 100 | + // Sort |
| 101 | + // map.sort(); // Error: Unhandled exception: |
| 102 | + // Unsupported operation: Cannot sort an unmodifiable map |
| 103 | + print('Sort map result: Cannot sort an unmodifiable map'); |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +//Agenda de contactos |
| 108 | +void agendaContactos() { |
| 109 | + Map<String, BigInt> contacts = {}; |
| 110 | + int? option = 0; |
| 111 | + |
| 112 | + do { |
| 113 | + print(''' |
| 114 | + |
| 115 | + -------------------- |
| 116 | + 1. Add contact |
| 117 | + 2. Remove contact |
| 118 | + 3. Search contact |
| 119 | + 4. List contacts |
| 120 | + 5. Exit |
| 121 | + -------------------- |
| 122 | + '''); |
| 123 | + option = int.tryParse(stdin.readLineSync()!); |
| 124 | + |
| 125 | + switch (option) { |
| 126 | + case 1: |
| 127 | + contacts.addAll(addContact()); |
| 128 | + case 2: |
| 129 | + contacts = removeContact(contacts); |
| 130 | + case 3: |
| 131 | + searchContact(contacts); |
| 132 | + case 4: |
| 133 | + listContacts(contacts); |
| 134 | + case 5: |
| 135 | + print('Program finished'); |
| 136 | + } |
| 137 | + } while (option != 5); |
| 138 | + |
| 139 | + listContacts(contacts); |
| 140 | +} |
| 141 | + |
| 142 | +Map<String, BigInt> addContact() { |
| 143 | + try { |
| 144 | + print('Insert name:'); |
| 145 | + String name = stdin.readLineSync()!; |
| 146 | + |
| 147 | + print('Insert phone:'); |
| 148 | + BigInt? phone = BigInt.tryParse(stdin.readLineSync()!); |
| 149 | + |
| 150 | + if (phone == null) { |
| 151 | + print('Incorrect phone'); |
| 152 | + return {}; |
| 153 | + } |
| 154 | + |
| 155 | + return {name: phone}; |
| 156 | + } on Exception catch (e) { |
| 157 | + e.toString(); |
| 158 | + return {}; |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +Map<String, BigInt> removeContact(Map<String, BigInt> contacts) { |
| 163 | + listContacts(contacts); |
| 164 | + print('Insert name to remove:'); |
| 165 | + |
| 166 | + try { |
| 167 | + String name = stdin.readLineSync()!; |
| 168 | + |
| 169 | + if (!contacts.containsKey(name)) { |
| 170 | + print('Contact not found'); |
| 171 | + return contacts; |
| 172 | + } |
| 173 | + |
| 174 | + contacts.remove(name); |
| 175 | + |
| 176 | + print('Contact removed'); |
| 177 | + return contacts; |
| 178 | + |
| 179 | + } on Exception catch (e) { |
| 180 | + e.toString(); |
| 181 | + return contacts; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +void searchContact(Map<String, BigInt> contacts) { |
| 186 | + print('Insert name to search:'); |
| 187 | + |
| 188 | + try { |
| 189 | + String name = stdin.readLineSync()!; |
| 190 | + |
| 191 | + if (!contacts.containsKey(name)) { |
| 192 | + print('Contact not found'); |
| 193 | + return; |
| 194 | + } |
| 195 | + |
| 196 | + print('phone: ' + contacts[name].toString()); |
| 197 | + |
| 198 | + return; |
| 199 | + |
| 200 | +} on Exception catch (e) { |
| 201 | + e.toString(); |
| 202 | + return; |
| 203 | +} |
| 204 | +} |
| 205 | + |
| 206 | +void listContacts(Map<String, BigInt> contacts) { |
| 207 | + print('Contacts:' + contacts.toString()); |
| 208 | +} |
0 commit comments