Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Read from a text file named "Products.txt" a list of product with prices
- * and keep them in a list of products.
- * Each product will be in format name + space + price.
- * You should hold the products in objects of class Product.
- * Read from a text file named "Order.txt" an order of products with quantities.
- * Each order line will be in format product + space + quantity.
- * Calculate and print in a text file "Output.txt" the total price of the order.
- * Ensure you close correctly all used resources. */
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class _10_OrderOfProducts {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ArrayList<Product> productList = readInputProductFileAndCreateListOfProducts();
- if (productList.size() > 0) {
- ArrayList<String> orderList = readInputOrderFileAndCreateOrderList();
- BigDecimal totalPrice = calculateTotalPriceOfAllProductsInOrderList(
- productList, orderList);
- printTheTotalPriceInTextFile(totalPrice);
- } else {
- System.out.println("Error! - The Products Price List is Empty!!!");
- }
- }
- private static ArrayList<Product> readInputProductFileAndCreateListOfProducts() {
- ArrayList<Product> products = new ArrayList<>();
- File file = new File("Products.txt");
- try (Scanner scan = new Scanner(file);) {
- while (scan.hasNextLine()) {
- String[] inputs = scan.nextLine().split("[ ]+");
- Product product = new Product(inputs[0], inputs[1]);
- products.add(product);
- }
- } catch (FileNotFoundException e) {
- System.out.println("Error! - The Product text File is Not Found!!!");
- e.printStackTrace();
- }
- return products;
- }
- private static ArrayList<String> readInputOrderFileAndCreateOrderList() {
- ArrayList<String> orders = new ArrayList<>();
- File file = new File("Order.txt");
- try (Scanner scan = new Scanner(file);) {
- while (scan.hasNextLine()) {
- String orderLine = scan.nextLine();
- orders.add(orderLine);
- }
- } catch (FileNotFoundException e) {
- System.out.println("Error! - The Order text File is Not Found!!!");
- e.printStackTrace();
- }
- return orders;
- }
- private static BigDecimal calculateTotalPriceOfAllProductsInOrderList(
- ArrayList<Product> products, ArrayList<String> orders) {
- BigDecimal result = BigDecimal.ZERO;
- if (orders.size() == 0) {
- System.out.println("The List of Orders is Empty!");
- return result;
- }
- for (String order : orders) {
- String[] splitted = order.split("[ ]+");
- BigDecimal quantity = new BigDecimal(splitted[0]);
- String orderName = splitted[1];
- for (Product product : products) {
- String productName = product.getName();
- if (productName.equals(orderName)) {
- quantity = quantity.multiply(product.getPrice());
- break;
- }
- }
- result = result.add(quantity);
- }
- return result;
- }
- private static void printTheTotalPriceInTextFile(BigDecimal price) {
- File file = new File("Output.txt");
- try (PrintStream writer = new PrintStream(file);) {
- writer.printf("The Total Price of the all Order products is: %.2f",
- price);
- } catch (IOException e) {
- System.out.println("Error! - Has a Problem with Output text File!!!");
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement