Advertisement
dimipan80

Order of Products (using Read and Write from Text Files)

Aug 20th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. /* Read from a text file named "Products.txt" a list of product with prices
  2.  * and keep them in a list of products.
  3.  * Each product will be in format name + space + price.
  4.  * You should hold the products in objects of class Product.
  5.  * Read from a text file named "Order.txt" an order of products with quantities.
  6.  * Each order line will be in format product + space + quantity.
  7.  * Calculate and print in a text file "Output.txt" the total price of the order.
  8.  * Ensure you close correctly all used resources. */
  9.  
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.io.PrintStream;
  14. import java.math.BigDecimal;
  15. import java.util.ArrayList;
  16. import java.util.Scanner;
  17.  
  18. public class _10_OrderOfProducts {
  19.  
  20.     public static void main(String[] args) {
  21.         // TODO Auto-generated method stub
  22.         ArrayList<Product> productList = readInputProductFileAndCreateListOfProducts();
  23.         if (productList.size() > 0) {
  24.             ArrayList<String> orderList = readInputOrderFileAndCreateOrderList();
  25.             BigDecimal totalPrice = calculateTotalPriceOfAllProductsInOrderList(
  26.                     productList, orderList);
  27.             printTheTotalPriceInTextFile(totalPrice);
  28.         } else {
  29.             System.out.println("Error! - The Products Price List is Empty!!!");
  30.         }
  31.     }
  32.  
  33.     private static ArrayList<Product> readInputProductFileAndCreateListOfProducts() {
  34.         ArrayList<Product> products = new ArrayList<>();
  35.         File file = new File("Products.txt");
  36.         try (Scanner scan = new Scanner(file);) {
  37.             while (scan.hasNextLine()) {
  38.                 String[] inputs = scan.nextLine().split("[ ]+");
  39.                 Product product = new Product(inputs[0], inputs[1]);
  40.                 products.add(product);
  41.             }
  42.         } catch (FileNotFoundException e) {
  43.             System.out.println("Error! - The Product text File is Not Found!!!");
  44.             e.printStackTrace();
  45.         }
  46.  
  47.         return products;
  48.     }
  49.  
  50.     private static ArrayList<String> readInputOrderFileAndCreateOrderList() {
  51.         ArrayList<String> orders = new ArrayList<>();
  52.         File file = new File("Order.txt");
  53.         try (Scanner scan = new Scanner(file);) {
  54.             while (scan.hasNextLine()) {
  55.                 String orderLine = scan.nextLine();
  56.                 orders.add(orderLine);
  57.             }
  58.         } catch (FileNotFoundException e) {
  59.             System.out.println("Error! - The Order text File is Not Found!!!");
  60.             e.printStackTrace();
  61.         }
  62.  
  63.         return orders;
  64.     }
  65.  
  66.     private static BigDecimal calculateTotalPriceOfAllProductsInOrderList(
  67.             ArrayList<Product> products, ArrayList<String> orders) {
  68.         BigDecimal result = BigDecimal.ZERO;
  69.         if (orders.size() == 0) {
  70.             System.out.println("The List of Orders is Empty!");
  71.             return result;
  72.         }
  73.  
  74.         for (String order : orders) {
  75.             String[] splitted = order.split("[ ]+");
  76.             BigDecimal quantity = new BigDecimal(splitted[0]);
  77.             String orderName = splitted[1];
  78.             for (Product product : products) {
  79.                 String productName = product.getName();
  80.                 if (productName.equals(orderName)) {
  81.                     quantity = quantity.multiply(product.getPrice());
  82.                     break;
  83.                 }
  84.             }
  85.  
  86.             result = result.add(quantity);
  87.         }
  88.  
  89.         return result;
  90.     }
  91.  
  92.     private static void printTheTotalPriceInTextFile(BigDecimal price) {
  93.         File file = new File("Output.txt");
  94.         try (PrintStream writer = new PrintStream(file);) {
  95.             writer.printf("The Total Price of the all Order products is: %.2f",
  96.                     price);
  97.         } catch (IOException e) {
  98.             System.out.println("Error! - Has a Problem with Output text File!!!");
  99.             e.printStackTrace();
  100.         }
  101.     }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement