|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.BufferedWriter; |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileReader; |
| 5 | +import java.io.FileWriter; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.PrintWriter; |
| 8 | +import java.util.Scanner; |
| 9 | + |
| 10 | +public class JimsimroDev { |
| 11 | + |
| 12 | + private final static String DIVLINE = "::::::::::::::::::::::::::::::::::::::::::::::::::::"; |
| 13 | + |
| 14 | + private static void menu() { |
| 15 | + String menu = """ |
| 16 | + 1 ~ Create product |
| 17 | + 2 ~ Consult product |
| 18 | + 3 ~ Update product |
| 19 | + 4 ~ Delete product |
| 20 | + 5 ~ List products |
| 21 | + 6 ~ Calculate total sales |
| 22 | + 7 ~ Calculate product sales |
| 23 | + 8 ~ Exit |
| 24 | + """; |
| 25 | + System.out.println(menu); |
| 26 | + } |
| 27 | + |
| 28 | + private static void actions() { |
| 29 | + Scanner in = new Scanner(System.in); |
| 30 | + int option = 0; |
| 31 | + while (option != 8) { |
| 32 | + menu(); |
| 33 | + System.out.println("Choose an option: "); |
| 34 | + option = in.nextInt(); |
| 35 | + in.nextLine(); |
| 36 | + switch (option) { |
| 37 | + case 1: |
| 38 | + System.out.print("Name: "); |
| 39 | + String name = in.nextLine(); |
| 40 | + System.out.print("Quantity: "); |
| 41 | + int quantity = in.nextInt(); |
| 42 | + System.out.print("Price: "); |
| 43 | + int price = in.nextInt(); |
| 44 | + try { |
| 45 | + createProduct(name, quantity, price); |
| 46 | + } catch (IOException e) { |
| 47 | + e.printStackTrace(); |
| 48 | + } |
| 49 | + break; |
| 50 | + case 2: |
| 51 | + System.out.print("Name of the product to search: "); |
| 52 | + String nameToSearch = in.nextLine(); |
| 53 | + consultProduct(nameToSearch); |
| 54 | + break; |
| 55 | + case 3: |
| 56 | + System.out.print("Name: "); |
| 57 | + String nameToUpdate = in.nextLine(); |
| 58 | + System.out.print("Quantity: "); |
| 59 | + int quantityToUpdate = in.nextInt(); |
| 60 | + System.out.print("Price: "); |
| 61 | + int priceToUpdate = in.nextInt(); |
| 62 | + updateProduct(nameToUpdate, quantityToUpdate, priceToUpdate); |
| 63 | + break; |
| 64 | + case 4: |
| 65 | + System.out.print("Name of the product to delete: "); |
| 66 | + String nameToDelete = in.nextLine(); |
| 67 | + deleteProduct(nameToDelete); |
| 68 | + break; |
| 69 | + case 5: |
| 70 | + listProducts(); |
| 71 | + break; |
| 72 | + case 6: |
| 73 | + calculateTotalSales(); |
| 74 | + break; |
| 75 | + case 7: |
| 76 | + System.out.print("Name of the product: "); |
| 77 | + String productName = in.nextLine(); |
| 78 | + calculateProductSales(productName); |
| 79 | + break; |
| 80 | + case 8: |
| 81 | + deleteFile(); |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + in.close(); // Close the scanner to avoid resource leak |
| 86 | + } |
| 87 | + |
| 88 | + private static void createProduct(String name, int quantity, int price) throws IOException { |
| 89 | + File file = getFileBasedOnOs(); |
| 90 | + |
| 91 | + try (PrintWriter printWriter = new PrintWriter(new FileWriter(file, true))) { // 'true' for appending to the file |
| 92 | + printWriter.print(name + ", "); |
| 93 | + printWriter.print(quantity + ", "); |
| 94 | + printWriter.println(price); |
| 95 | + } catch (IOException e) { |
| 96 | + e.printStackTrace(); |
| 97 | + } |
| 98 | + System.out.println("Created successfully"); |
| 99 | + } |
| 100 | + |
| 101 | + private static void consultProduct(String name) { |
| 102 | + File file = getFileBasedOnOs(); |
| 103 | + |
| 104 | + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { |
| 105 | + String line; |
| 106 | + System.out.println(DIVLINE); |
| 107 | + while ((line = bufferedReader.readLine()) != null) { |
| 108 | + if (line.contains(name)) { |
| 109 | + System.out.println(line); |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | + System.out.println(DIVLINE); |
| 114 | + } catch (IOException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static void updateProduct(String name, int quantity, int price) { |
| 120 | + File file = getFileBasedOnOs(); |
| 121 | + File tempFile = new File(file.getAbsolutePath() + ".tmp"); |
| 122 | + boolean productFound = false; |
| 123 | + |
| 124 | + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); |
| 125 | + BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile))) { |
| 126 | + |
| 127 | + String line; |
| 128 | + while ((line = bufferedReader.readLine()) != null) { |
| 129 | + if (!line.contains(name)) { |
| 130 | + bw.write(line); |
| 131 | + bw.newLine(); |
| 132 | + } else { |
| 133 | + bw.write(name + ", "); |
| 134 | + bw.write(quantity + ", "); |
| 135 | + bw.write(price + "\n"); |
| 136 | + System.out.println("Updated successfully"); |
| 137 | + productFound = true; |
| 138 | + } |
| 139 | + } |
| 140 | + } catch (IOException e) { |
| 141 | + System.out.println(e.getClass().getName() + " Error processing the file " + e.getMessage()); |
| 142 | + e.printStackTrace(); |
| 143 | + } |
| 144 | + if (!productFound) { |
| 145 | + System.err.println("Product " + name + " does not exist in the file " + file.getName()); |
| 146 | + tempFile.delete(); |
| 147 | + return; |
| 148 | + } |
| 149 | + if (!file.delete()) { |
| 150 | + return; |
| 151 | + } |
| 152 | + if (!tempFile.renameTo(file)) { |
| 153 | + System.err.println(file.getName()); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private static void deleteProduct(String name) { |
| 158 | + File file = getFileBasedOnOs(); |
| 159 | + File tempFile = new File(file.getAbsolutePath() + ".tmp"); |
| 160 | + |
| 161 | + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); |
| 162 | + BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile))) { |
| 163 | + |
| 164 | + String line; |
| 165 | + while ((line = bufferedReader.readLine()) != null) { |
| 166 | + if (!line.contains(name)) { |
| 167 | + bw.write(line); |
| 168 | + bw.newLine(); |
| 169 | + } |
| 170 | + } |
| 171 | + } catch (IOException e) { |
| 172 | + System.out.println(e.getClass().getName() + " Error processing the file " + e.getMessage()); |
| 173 | + e.printStackTrace(); |
| 174 | + } |
| 175 | + |
| 176 | + if (!file.delete()) { |
| 177 | + return; |
| 178 | + } |
| 179 | + if (!tempFile.renameTo(file)) { |
| 180 | + System.err.println(file.getName()); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private static void listProducts() { |
| 185 | + File file = getFileBasedOnOs(); |
| 186 | + |
| 187 | + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { |
| 188 | + String line; |
| 189 | + System.out.println(DIVLINE); |
| 190 | + while ((line = bufferedReader.readLine()) != null) { |
| 191 | + System.out.println(line); |
| 192 | + } |
| 193 | + System.out.println(DIVLINE); |
| 194 | + } catch (IOException e) { |
| 195 | + e.printStackTrace(); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private static void calculateTotalSales() { |
| 200 | + File file = getFileBasedOnOs(); |
| 201 | + |
| 202 | + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { |
| 203 | + String line; |
| 204 | + int totalSales = 0; |
| 205 | + System.out.println(DIVLINE); |
| 206 | + while ((line = bufferedReader.readLine()) != null) { |
| 207 | + String[] parts = line.split(", "); |
| 208 | + int quantity = Integer.parseInt(parts[1]); |
| 209 | + int price = Integer.parseInt(parts[2]); |
| 210 | + |
| 211 | + totalSales += (quantity * price); |
| 212 | + } |
| 213 | + System.out.printf("Total sales: %d\n", totalSales); |
| 214 | + System.out.println(DIVLINE); |
| 215 | + } catch (IOException e) { |
| 216 | + e.printStackTrace(); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + private static void calculateProductSales(String name) { |
| 221 | + File file = getFileBasedOnOs(); |
| 222 | + |
| 223 | + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { |
| 224 | + String line; |
| 225 | + int totalSales = 0; |
| 226 | + System.out.println(DIVLINE); |
| 227 | + while ((line = bufferedReader.readLine()) != null) { |
| 228 | + if (line.contains(name)) { |
| 229 | + String[] parts = line.split(", "); |
| 230 | + int quantity = Integer.parseInt(parts[1]); |
| 231 | + int price = Integer.parseInt(parts[2]); |
| 232 | + totalSales += (quantity * price); |
| 233 | + break; |
| 234 | + } |
| 235 | + } |
| 236 | + System.out.printf("Total sales of %s = %d\n", name, totalSales); |
| 237 | + System.out.println(DIVLINE); |
| 238 | + } catch (IOException e) { |
| 239 | + e.printStackTrace(); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + private static void deleteFile() { |
| 244 | + File file = getFileBasedOnOs(); |
| 245 | + file.delete(); |
| 246 | + } |
| 247 | + |
| 248 | + private static File getFileBasedOnOs() { |
| 249 | + String osName = System.getProperty("os.name").toLowerCase(); |
| 250 | + File file; |
| 251 | + |
| 252 | + if (osName.contains("win")) { |
| 253 | + file = new File("J:\\JimsimroDev.txt"); |
| 254 | + } else if (osName.contains("nix") || osName.contains("nux")) { |
| 255 | + file = new File("/mnt/j/JimsimroDev.txt"); |
| 256 | + } else if (osName.contains("mac")) { |
| 257 | + file = new File("/mnt/j/JimsimroDev.txt"); |
| 258 | + } else { |
| 259 | + file = new File("/mnt/j/JimsimroDev.txt"); // Operating system not recognized |
| 260 | + } |
| 261 | + return file; |
| 262 | + } |
| 263 | + |
| 264 | + public static void main(String[] args) throws IOException { |
| 265 | + actions(); |
| 266 | + } |
| 267 | +} |
0 commit comments