From 1bd8db01a0522d4017b7f62df082cfedb8268827 Mon Sep 17 00:00:00 2001 From: Joey Eamigh <55670930+JoeyEamigh@users.noreply.github.com> Date: Mon, 18 Sep 2023 14:33:24 -0400 Subject: [PATCH] assn02 --- Justfile | 2 +- src/assn02/JavaWarmUp.java | 135 +++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/assn02/JavaWarmUp.java diff --git a/Justfile b/Justfile index 131bf79..bc56e5a 100644 --- a/Justfile +++ b/Justfile @@ -6,7 +6,7 @@ run file=(`find src -type f -printf '%T@ %p\n' | sort -n | cut -d " " -f 2- | ta all: for file in `find src -type f -printf '%T@ %p\n' | sort -n | cut -d " " -f 2-`; do just run $file; done -zip folder=(`find src -type d -printf '%T@ %p\n' | sort -n | cut -d' ' -f 2- | sed 's/src\///' | head -n -1 | tail -1`): +zip folder=(`find src -type d -printf '%T@ %p\n' | sort -n | cut -d' ' -f 2- | sed 's/src\///' | tail -1`): rm -rf ./zips/{{folder}}.zip zip -r ./zips/{{folder}}.zip ./src/{{folder}} diff --git a/src/assn02/JavaWarmUp.java b/src/assn02/JavaWarmUp.java new file mode 100644 index 0000000..cc96230 --- /dev/null +++ b/src/assn02/JavaWarmUp.java @@ -0,0 +1,135 @@ +package assn02; + +import java.util.Scanner; + +// Here is a starter code that you may optionally use for this assignment. +// TODO: You need to complete these sections + +public class JavaWarmUp { + public static void main(String[] args) { + Scanner s = new Scanner(System.in); + + String[] categoriesList = { "phone", "laptop", "smart_watch" }; + + int n = s.nextInt(); + // MM/DD/YY, HH:MM, Name, Price, Quantity, Rating, Duration + + // create corresponding size arrays + String dateT[] = new String[n]; + String timeT[] = new String[n]; + String categoryT[] = new String[n]; + double Assembling_fee[] = new double[n]; + int quantityT[] = new int[n]; + double Assembling_Time[] = new double[n]; + double Total_Cost[] = new double[n]; + + // TODO: Fill in the above arrays with data entered from the console. + // Your code starts here: + for (int i = 0; i < n; i++) { + dateT[i] = s.next(); + timeT[i] = s.next(); + categoryT[i] = s.next(); + Assembling_fee[i] = s.nextDouble(); + quantityT[i] = s.nextInt(); + Assembling_Time[i] = s.nextDouble(); + Total_Cost[i] = s.nextDouble(); + } + // Your code ends here. + + // Find items with highest and lowest price per unit + int highestItemIndex = getMaxPriceIndex(Assembling_fee); + int lowestItemIndex = getMinPriceIndex(Assembling_fee); + + // TODO: Print items with highest and lowest price per unit. + // Your code starts here: + System.out.println(dateT[highestItemIndex]); + System.out.println(timeT[highestItemIndex]); + System.out.println(categoryT[highestItemIndex]); + System.out.println(Assembling_fee[highestItemIndex]); + + System.out.println(dateT[lowestItemIndex]); + System.out.println(timeT[lowestItemIndex]); + System.out.println(categoryT[lowestItemIndex]); + System.out.println(Assembling_fee[lowestItemIndex]); + // Your code ends here. + + // Calculate the average price, rating and duration of sales by category. + // Maintain following category-wise stats in Arrays + int[] numOfCategoriesC = new int[categoriesList.length];// so numOfCategoriesC[0] = # of categories of type + // categoriesList[0] + double[] totPriceC = new double[categoriesList.length]; // total price of each category = sum(price x qty) + int[] totQuantityC = new int[categoriesList.length]; // total qty of each category = sum (qty) + double[] totAssembling_TimeC = new double[categoriesList.length]; // total Rating of each category = sum(price x + // qty) + double[] tot_CostC = new double[categoriesList.length]; // total Duration of each category = + // sum(price x qty) + + // TODO: set the value of catIndex for each i to be such that categoryT[i] == + // categoriesList[i]. + // Your code starts here: + int[] catIndex = new int[n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < categoriesList.length; j++) { + if (categoryT[i].equals(categoriesList[j])) { + catIndex[i] = j; + } + } + } + // Your code ends here. + + // TODO: Calculate & Print Category-wise Statistics + // Your code starts here: + for (int i = 0; i < n; i++) { + numOfCategoriesC[catIndex[i]]++; + totPriceC[catIndex[i]] += Assembling_fee[i] * quantityT[i]; + totQuantityC[catIndex[i]] += quantityT[i]; + totAssembling_TimeC[catIndex[i]] += Assembling_Time[i]; + tot_CostC[catIndex[i]] += Total_Cost[i]; + } + + double workerHourlyRate = 16.0; + + for (int i = 0; i < categoriesList.length; i++) { + System.out.println(categoriesList[i]); + System.out.println(totQuantityC[i]); + System.out.printf("%.2f\n", totPriceC[i] / totQuantityC[i]); + System.out + .printf("%.2f\n", (totPriceC[i] - totAssembling_TimeC[i] * workerHourlyRate - tot_CostC[i]) + / totQuantityC[i]); + } + // Your code ends here. + } + + // TODO: Find index of item with the highest price per unit. + static int getMaxPriceIndex(double[] priceT) { + // Your code starts here: + int maxIndex = 0; + double max = priceT[0]; + + for (int i = 1; i < priceT.length; i++) { + if (priceT[i] > max) { + max = priceT[i]; + maxIndex = i; + } + } + return maxIndex; + // Your code ends here. + } + + // TODO: Find index of item with the lowest price per unit. + static int getMinPriceIndex(double[] priceT) { + // Your code starts here: + int minIndex = 0; + double min = priceT[0]; + + for (int i = 1; i < priceT.length; i++) { + if (priceT[i] < min) { + min = priceT[i]; + minIndex = i; + } + } + + return minIndex; + // Your code ends here. + } +}