import java.io.*; import java.util.ArrayList; import java.util.HashMap; import…
- User
import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
public class BaseballCardProfitCalculator {
private static Map<String, Integer> marketPrices = new HashMap<>(); private static List purchasedCards;
public static void main(String[] args) { String marketPriceFile = “market-price.txt”; String priceListFile = “price-list.txt”; String outputFile = “output.txt”;
for (int i = 0; i < args.length; i++) { if (args[i].equals(“-m”)) { marketPriceFile = args[i + 1]; i++; } else if (args[i].equals(“-p”)) { priceListFile = args[i + 1]; i++; } else if (args[i].equals(“-o”)) { outputFile = args[i + 1]; i++; } }
try { loadMarketPrices(marketPriceFile); } catch (IOException e) { e.printStackTrace(); return; }
try { computeProfits(priceListFile, outputFile); } catch (IOException e) { e.printStackTrace(); } }
private static void loadMarketPrices(String filename) throws IOException { try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { int itemCount = Integer.parseInt(reader.readLine().trim()); for (int i = 0; i < itemCount; i++) { String[] line = reader.readLine().split(" “); marketPrices.put(line[0], Integer.parseInt(line[1])); } } }
private static void computeProfits(String priceListFile, String outputFile) throws IOException { try (BufferedReader reader = new BufferedReader(new FileReader(priceListFile)); PrintWriter writer = new PrintWriter(new FileWriter(outputFile))) {
String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (line.isEmpty()) { continue; } String[] firstLine = line.split(”\s+“); int n = Integer.parseInt(firstLine[0]); int w = Integer.parseInt(firstLine[1]);
List cards = new ArrayList<>(); List prices = new ArrayList<>(); boolean skipEntry = false;
for (int i = 0; i < n; i++) { String cardLineStr = reader.readLine().trim(); String[] cardLineParts = cardLineStr.split(”\s+“);
if (cardLineParts.length != 2 || !marketPrices.containsKey(cardLineParts[0])) { writer.println(“Card: " + cardLineParts[0] + " is not found in market-price.txt”); skipEntry = true; for (int j = i + 1; j < n; j++) { reader.readLine(); } break; }
cards.add(cardLineParts[0]); prices.add(Integer.parseInt(cardLineParts[1])); }
if (skipEntry) { continue; }
long startTime = System.nanoTime(); int maxProfit = calculateMaxProfit(cards, prices, w); long endTime = System.nanoTime();
double timeTaken = (endTime - startTime) / 1_000_000_000.0; String formattedTime = String.format(”%.3f", timeTaken);
writer.println(n + " " + maxProfit + " " + purchasedCards.size() + " " + formattedTime); for (String card : purchasedCards) { writer.println(card); } } } }
private static int calculateMaxProfit(List cards, List prices, int w) { int maxProfit = 0; int numberOfCombinations = 1 << cards.size(); purchasedCards = new ArrayList<>();
- Assistant
To run the
BaseballCardProfitCalculatorJava program from the terminal in Visual Studio Code (VS Code), you must ensure that you have the following: