ellesehc

Biz

Oct 20th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. import java.util.Arrays;
  2. public class biz
  3. {
  4. //System.out.println(Arrays.deepToString(commodities)); --> prints out everything in 2D
  5.     public static void main(String[] args)
  6.     {
  7.          String cheapestItem = "";
  8.         String[][] categories = new String [5][3];
  9.         String [] argsx = {"toothpaste1-150, toothpaste2-100, toothpaste3-135",
  10.                 "soap1-80, soap2-135, soap3-50",
  11.                 "detergent1-280, detergent2-635, detergent3-350",
  12.                 "chips1-20, chips2-60, chips3-35",
  13.                 "candies1-35, candies2-25, candies2-60"};
  14.        
  15.         for(int x = 0 ; x < categories.length ; x++){
  16.             categories[x] = argsx[x].split(", ");    
  17.         }
  18.         int price = 0;
  19.         int[] cheapest = new int [5];
  20.        
  21.         for(int x = 0 ; x < 5 ; x++){
  22.             int priceA = Integer.parseInt(categories[x][0].substring(categories[x][0].indexOf("-") + 1));
  23.             String nameA = categories[x][0].substring(0, categories[x][0].indexOf("-"));
  24.             int priceB = Integer.parseInt(categories[x][1].substring(categories[x][1].indexOf("-") + 1));
  25.             String nameB = categories[x][1].substring(0, categories[x][0].indexOf("-"));
  26.             int priceC = Integer.parseInt(categories[x][2].substring(categories[x][2].indexOf("-") + 1));  
  27.             String nameC = categories[x][2].substring(0, categories[x][0].indexOf("-"));
  28.            
  29.            
  30.            
  31.             int firstSet = Math.min(priceA,priceB);
  32.             int secondSet = Math.min(priceB,priceC);
  33.             int finalSet = Math.min(firstSet, secondSet);
  34.             price += finalSet;
  35.            
  36.             if(finalSet == priceA)
  37.                 cheapestItem += nameA + ", ";
  38.             else if(finalSet == priceB)
  39.                 cheapestItem += nameB + ", ";
  40.             else
  41.                 cheapestItem += nameC + ", ";
  42.             }
  43.        
  44.        
  45.         cheapestItem = cheapestItem.substring(0, cheapestItem.length()-2);
  46.         System.out.println("Cheapest: " + cheapestItem);
  47.         System.out.println("Total: " + price);
  48.         }
  49.        
  50.  
  51.        
  52.         }
Advertisement
Add Comment
Please, Sign In to add comment