Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class biz
- {
- //System.out.println(Arrays.deepToString(commodities)); --> prints out everything in 2D
- public static void main(String[] args)
- {
- String cheapestItem = "";
- String[][] categories = new String [5][3];
- String [] argsx = {"toothpaste1-150, toothpaste2-100, toothpaste3-135",
- "soap1-80, soap2-135, soap3-50",
- "detergent1-280, detergent2-635, detergent3-350",
- "chips1-20, chips2-60, chips3-35",
- "candies1-35, candies2-25, candies2-60"};
- for(int x = 0 ; x < categories.length ; x++){
- categories[x] = argsx[x].split(", ");
- }
- int price = 0;
- int[] cheapest = new int [5];
- for(int x = 0 ; x < 5 ; x++){
- int priceA = Integer.parseInt(categories[x][0].substring(categories[x][0].indexOf("-") + 1));
- String nameA = categories[x][0].substring(0, categories[x][0].indexOf("-"));
- int priceB = Integer.parseInt(categories[x][1].substring(categories[x][1].indexOf("-") + 1));
- String nameB = categories[x][1].substring(0, categories[x][0].indexOf("-"));
- int priceC = Integer.parseInt(categories[x][2].substring(categories[x][2].indexOf("-") + 1));
- String nameC = categories[x][2].substring(0, categories[x][0].indexOf("-"));
- int firstSet = Math.min(priceA,priceB);
- int secondSet = Math.min(priceB,priceC);
- int finalSet = Math.min(firstSet, secondSet);
- price += finalSet;
- if(finalSet == priceA)
- cheapestItem += nameA + ", ";
- else if(finalSet == priceB)
- cheapestItem += nameB + ", ";
- else
- cheapestItem += nameC + ", ";
- }
- cheapestItem = cheapestItem.substring(0, cheapestItem.length()-2);
- System.out.println("Cheapest: " + cheapestItem);
- System.out.println("Total: " + price);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment