svetlozar_kirkov

Order of Products (Homework)

Jan 24th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.PrintWriter;
  4. import java.io.UnsupportedEncodingException;
  5. import java.math.BigDecimal;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.Scanner;
  10.  
  11. public class Problem10_OrderOfProducts {
  12.    
  13.     public static class Product { //creating object "Product"
  14.         private String name;  // creating property "name"
  15.         private BigDecimal price; //creating property "price"
  16.        
  17.         public Product(String name, BigDecimal price){ //constructor with parameters
  18.             this.name=name;
  19.             this.price=price;
  20.         }
  21.         public String getName(){ //method to return the name of the product
  22.             return name;
  23.         }
  24.         public BigDecimal getPrice(){ //method to return the price of the product
  25.             return price;
  26.         }
  27.     }
  28.    
  29.     public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException {
  30.         ArrayList<Product> products = new ArrayList<Product>(); //list to keep the products from input file
  31.         try {
  32.             Scanner productsTXT = new Scanner(new File("Products.txt")); //opening the Products.txt file
  33.             while (productsTXT.hasNextLine()){ //checking if the file has more lines
  34.                 Product temp = new Product(productsTXT.next(),productsTXT.nextBigDecimal()); //creating temporary product
  35.                 products.add(temp); //adding the temporary product to the list
  36.             }
  37.             productsTXT.close();
  38.         } catch (FileNotFoundException e) { //catching the exception if the file does not exist
  39.             System.out.println("Error: Products.txt not found!"); //printing error message
  40.             System.exit(-1); //program exit
  41.         }
  42.         Map<BigDecimal, String> orders = new HashMap<BigDecimal, String>(); //creating hashmap to hold the orders
  43.         try{
  44.             Scanner orderTXT = new Scanner(new File("Order.txt")); //opening the Order.txt file
  45.             while (orderTXT.hasNextLine()){ //checking if the file has more lines
  46.             orders.put(orderTXT.nextBigDecimal(), orderTXT.next()); // putting the temporary order in the hashmap
  47.             }                                                       // using "quantity" + " " + "product" from Order.txt
  48.             orderTXT.close();
  49.         }
  50.         catch (FileNotFoundException e) { //catching the exception if the file does not exist
  51.             System.out.println("Error: Order.txt not found!"); //printing error message
  52.             System.exit(-1); //program exit
  53.         }
  54.         BigDecimal totalPrice = new BigDecimal("0"); // initialize totalPrice
  55.         for (Map.Entry<BigDecimal, String> entry : orders.entrySet()) { //iterating the orders hashmap
  56.             BigDecimal quantity = entry.getKey(); // getting the quantity
  57.             String key = entry.getValue(); // getting the name
  58.             for (Product product: products){ // iterating and checking the products list for the key
  59.                 if (product.getName().equals(key.toString())){ // if the product is found
  60.                     BigDecimal itemCost = product.getPrice().multiply(quantity); // calculate item cost
  61.                     totalPrice = totalPrice.add(itemCost); //adding item cost to the total price
  62.                 }
  63.             }
  64.         }
  65.         PrintWriter writer = new PrintWriter("Output.txt", "UTF-8"); //writing the output file
  66.         writer.printf("%.2f",totalPrice); //writing the total price to the output text file
  67.         writer.close(); // closing the writer
  68.         System.out.printf("%.2f",totalPrice); //print totalprice to console
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment