Eac6
public
Mar 25, 2025
1 days
41
1 public class EAC6S22425 { 2 3 public static void main(String[] args) { 4 EAC6S22425 program = new EAC6S22425(); 5 program.start(); 6 } 7 8 UtilsIO utilsIO; 9 FileUtils fileUtils; 10 SupermarketList supermarketList; 11 12 public void start() { 13 // 1 - Crear instància de UtilsIO i FileUtils 14 utilsIO = new UtilsIO(); 15 fileUtils = new FileUtils("data"); 16 17 // 2 - Crear instància de SupermarketList 18 supermarketList = new SupermarketList(); 19 20 // booleà per el while 21 boolean running = true; 22 23 do { 24 // 3 - Iniciar menu 25 utilsIO.showMenu(Constants.START_MENU); 26 27 // 3.1 - Demanar opcions per teclat i comprovar valorOpcio 28 int valorOpcio = utilsIO.askForInteger(Constants.MESSAGE_ASK_OPTION_VALUE, Constants.ERROR_NO_INTEGER); 29 30 if (valorOpcio > 5 || valorOpcio < 0) { 31 utilsIO.showError(Constants.MESSAGE_NOT_VALID_OPTION); 32 } 33 34 switch (valorOpcio) { 35 case 0: 36 running = false; 37 break; 38 39 case 1: // 4- Crear supermercat 40 afegirUnSupermercat(); 41 break; 42 43 case 2: // 5- Afegir producte 44 afegirProducte(); 45 break; 46 47 case 3: // 6- Veure supermercats 48 mostrarElsSupermercats(); 49 break; 50 51 case 4: // 7- Extreure llistat(array) al .txt 52 exportarSupermercats(); 53 break; 54 55 case 5: // 8- Importar supermercats (llegir els arxius de text) 56 importarSupermercats(); 57 break; 58 59 } 60 } while (running); 61 } 62 63 void afegirUnSupermercat() { 64 // 4.1 - Preguntar a l'usuari les dades 65 String name = utilsIO.askForString(Constants.ENTER_SUPERMARKET_NAME, 66 Constants.MESSAGE_DEFAULT_ERROR_STRING); 67 String city = utilsIO.askForString(Constants.ENTER_SUPERMARKET_CITY, 68 Constants.MESSAGE_DEFAULT_ERROR_STRING); 69 Float latitude = utilsIO.askForFloat(Constants.ENTER_SUPERMARKET_LONG, 70 Constants.MESSAGE_DEFAULT_ERROR_FLOAT); 71 Float longitude = utilsIO.askForFloat(Constants.ENTER_SUPERMARKET_LAT, 72 Constants.MESSAGE_DEFAULT_ERROR_FLOAT); 73 74 if (supermarketList.lookForSupermarket(name, city) == null) { 75 // crear objecte 76 Supermarket supermarket = new Supermarket(name, city, longitude, latitude); 77 78 // afegir amb el mètode addSupermarket() 79 supermarketList.addSupermarket(supermarket); 80 81 utilsIO.showInfo(Constants.SUPERMARKET_ADDED_SUCCESSFULLY); 82 } else { 83 utilsIO.showError(Constants.SUPERMARKET_ALREADY_EXISTS); 84 } 85 86 } 87 88 void afegirProducte() { 89 // 5.1 - Demanar a l'usuari supermarketName i cityName 90 String supermarketName = utilsIO.askForString(Constants.ENTER_SUPERMARKET_NAME, 91 Constants.MESSAGE_DEFAULT_ERROR_STRING); 92 String cityName = utilsIO.askForString(Constants.ENTER_SUPERMARKET_CITY, 93 Constants.MESSAGE_DEFAULT_ERROR_STRING); 94 95 // 5.2 - Buscar el supermercat a la llista 96 Supermarket foundSupermarket = supermarketList.lookForSupermarket(supermarketName, cityName); 97 98 if (foundSupermarket == null) { 99 utilsIO.showError(Constants.SUPERMARKET_NO_EXISTS); 100 101 } else { 102 // 5.3 En cas positiu, demanar info de producte a l'usuari 103 String nameProduct = utilsIO.askForString(Constants.ENTER_SUPERMARKET_PRODUCT, 104 Constants.MESSAGE_DEFAULT_ERROR_STRING); 105 Float priceProduct = utilsIO.askForFloat(Constants.ENTER_SUPERMARKET_PRODUCT_PRICE, 106 Constants.MESSAGE_DEFAULT_ERROR_FLOAT); 107 108 // 5.3.1 Mirar si ja existeix el producte (hasProduct) i afegirlo si no existeix 109 if (foundSupermarket.hasProduct(nameProduct)) { 110 utilsIO.showError(Constants.PRODUCT_ALREADY_EXISTS); 111 } else { 112 foundSupermarket.addProduct(nameProduct, priceProduct); 113 utilsIO.showInfo(Constants.ADDED_SUCCESSFULLY); 114 } 115 } 116 } 117 118 void mostrarElsSupermercats() { 119 // L'opció de veure els supermercats ha de mostrar els que hi ha a memoria 120 if (supermarketList.supermarketList.isEmpty()) { 121 utilsIO.showError(Constants.ERROR_LIST_EMPTY); 122 } else { 123 utilsIO.showSupermarkets(supermarketList.listToString()); 124 } 125 } 126 127 void exportarSupermercats() { 128 129 String supermarketData = ""; 130 String supermarketProductData = ""; 131 132 boolean exportOKsuper = true; 133 boolean exportOKproduct = true; 134 135 // Exportar (escriure) a supermarkets.txt i products.txt 136 137 for (int i = 0; i < supermarketList.supermarketList.size(); i++) { 138 139 Supermarket supermarket = supermarketList.supermarketList.get(i); 140 141 supermarketData = supermarketData + supermarket.supermarketToString(); 142 143 supermarketProductData = supermarketProductData + supermarket.productsToString(); 144 } 145 // controla el error de si esta buit o empty 146 try { 147 fileUtils.createFileFromString("supermarkets.txt", supermarketData); 148 } catch (IllegalArgumentException e) { 149 utilsIO.showError(Constants.ERROR_LIST_EMPTY); 150 exportOKsuper = false; 151 } 152 153 // controla el error de si esta buit o empty 154 try { 155 fileUtils.createFileFromString("products.txt", supermarketProductData); 156 } catch (IllegalArgumentException e) { 157 utilsIO.showError(Constants.ERROR_MAP_EMPTY); 158 exportOKproduct = false; 159 } 160 161 /* 162 * en cas de no haver passat per el o els catch direm que s'ha exportat 163 * pot ser que el supermarket.txt tingui contingut pero products no, 164 * llança error de products pero també llança info de que s'ha exportat algo 165 * (supermarkets.txt) 166 */ 167 if (exportOKsuper || exportOKproduct) { 168 utilsIO.showInfo(Constants.EXPORT_OK); 169 } 170 171 } 172 173 void importarSupermercats() { // Aquest mètode llegeix el contingut dels fitxers .txt 174 175 // 1- CONTROL ERRORS 176 // 1.1- Comprovem si els dos fitxers existeixen abans d'extreure info 177 if (!fileUtils.fileExists("supermarkets.txt") 178 || !fileUtils.fileExists("products.txt")) { 179 utilsIO.showError(Constants.ERROR_FILES_NOT_FOUND); 180 } 181 182 // s'extrau info a les variables dels dos arxius 183 String fileDataSupermarket = fileUtils.extractFileIntoString("supermarkets.txt"); 184 String fileDataProducts = fileUtils.extractFileIntoString("products.txt"); 185 186 // 1.2- comprovem error null o empty file als dos cassos 187 if (fileDataSupermarket == null 188 || fileDataSupermarket.isEmpty() 189 || fileDataProducts == null 190 || fileDataProducts.isEmpty()) { 191 utilsIO.showError(Constants.ERROR_CONTENT_NULL); 192 } 193 194 /* 195 * 2- fileDataSupermarket ho separem per "\n" i "," en un try catch (en cas de 196 * que sigui buit donaria exepcio) ja que si es buit o empty nomes ens informa 197 * pero el programa segueix 198 */ 199 String[] supermarketLines = { "" }; 200 201 try { 202 supermarketLines = fileDataSupermarket.split("\n"); 203 } catch (Exception e) { 204 utilsIO.showError(Constants.ERROR_CONTENT_NULL); 205 } 206 207 for (int i = 0; i < supermarketLines.length; i++) { 208 209 String[] supermarketItems = supermarketLines[i].split(","); 210 211 Supermarket supermarket = new Supermarket(supermarketItems[0], supermarketItems[1], 212 Float.parseFloat(supermarketItems[2]), Float.parseFloat(supermarketItems[3])); 213 214 if (supermarketList.lookForSupermarket(supermarketItems[0], supermarketItems[1]) == null) { 215 supermarketList.addSupermarket(supermarket); 216 } 217 } 218 219 /* 220 * 2.1- fileDataProducts ho separem per "\n" i "," en un try catch (en cas de 221 * que sigui buit donaria exepcio) 222 */ 223 String[] productLines = { "" }; 224 225 try { 226 productLines = fileDataProducts.split("\n"); 227 } catch (Exception e) { 228 utilsIO.showError(Constants.ERROR_CONTENT_NULL); 229 } 230 231 for (int i = 0; i < productLines.length; i++) { 232 233 String[] productItems = productLines[i].split(","); 234 235 // extraiem els detalls que volem de la linea 236 String supermarketName = productItems[0]; 237 String supermarketCity = productItems[1]; 238 String productName = productItems[2]; 239 Float productPrice = Float.parseFloat(productItems[3]); 240 241 Supermarket supermarket = supermarketList.lookForSupermarket(supermarketName, supermarketCity); 242 243 // comprovar si existeix el supermercat 244 if (supermarket != null) { 245 if (!supermarket.hasProduct(productName)) { 246 supermarket.addProduct(productName, productPrice); 247 } 248 } 249 } 250 utilsIO.showInfo(Constants.MESSAGE_FILES_IMPORTED); 251 } 252 }