Question
Java. Project Description Project Description Implement the following operations. Starter code is provided. Do not change the name of the class. Do not change the
Java.
Project Description
Project Description
Implement the following operations. Starter code is provided. Do not change the name of the class. Do not change the signatures of public methods in the starter code. Multi-dimensional search: Consider the web site of a seller like Amazon. They carry tens of thousands of products, and each product has many attributes (Name, Size, Description, Keywords, Manufacturer, Price, etc.). The search engine allows users to specify attributes of products that they are seeking, and shows products that have most of those attributes. To make search efficient, the data is organized using appropriate data structures, such as balanced trees. But, if products are organized by Name, how can search by price implemented efficiently? The solution, called indexing in databases, is to create a new set of references to the objects for each search field, and organize them to implement search operations on that field efficiently. As the objects change, these access structures have to be kept consistent.
In this project, each object has 3 attributes: id (int), description (zero or more ints), and price (int). The following operations are supported: a. Insert(id,price,list): insert a new item whose description is given in the list. If an entry with the same id already exists, then its description and price are replaced by the new values, unless list is null or empty, in which case, just the price is updated. Returns 1 if the item is new, and 0 otherwise. b. Find(id): return price of item with given id (or 0, if not found). c. Delete(id): delete item from storage. Returns the sum of the ints that are in the description of the item deleted (or 0, if such an id did not exist). d. FindMinPrice(n): given an integer, find items whose description contains that number (exact match with one of the ints in the item's description), and return lowest price of those items. Return 0 if there is no such item. e. FindMaxPrice(n): given an integer, find items whose description contains that number, and return highest price of those items. Return 0 if there is no such item. f. FindPriceRange(n,low,high): given int n, find the number of items whose description contains n, and in addition, their prices fall within the given range, [low, high]. g. RemoveNames(id, list): Remove elements of list from the description of id. It is possible that some of the items in the list are not in the id's description. Return the sum of the numbers that are actually deleted from the description of id. Return 0 if there is no such id.
Sample input: Insert 22 19 475 1238 9742 0 # New item with id=22, price="$19.97", name="475 1238 9742" # Return: 1 # Insert 12 96 44 109 0 # Second item with id=12, price="96.92", name="44 109" # Return: 1 # Insert 37 47 109 475 694 88 0 # Another item with id=37, price="47.44", name="109 475 694 88" # Return: 1 # FindMaxPrice 475 # Return: 47 (id of items considered: 22, 37) # Delete 37 # Return: 1366 (=109+475+694+88) # FindMaxPrice 475 # Return: 19 (id of items considered: 22) # Output: 1435
--------------------------------------------------------
// If you want to create additional classes, place them in this file as subclasses of MDS public class MDS { // Add fields of MDS here // Constructors public MDS() { } /* Public methods of MDS. Do not change their signatures. a. Insert(id,price,list): insert a new item whose description is given in the list. If an entry with the same id already exists, then its description and price are replaced by the new values, unless list is null or empty, in which case, just the price is updated. Returns 1 if the item is new, and 0 otherwise. */ public int insert(int id, int price, java.util.List list) { return 0; } // b. Find(id): return price of item with given id (or 0, if not found). public int find(int id) { return 0; } /* c. Delete(id): delete item from storage. Returns the sum of the ints that are in the description of the item deleted, or 0, if such an id did not exist. */ public int delete(int id) { return 0; } /* d. FindMinPrice(n): given an integer, find items whose description contains that number (exact match with one of the ints in the item's description), and return lowest price of those items. Return 0 if there is no such item. */ public int findMinPrice(int n) { return 0; } /* e. FindMaxPrice(n): given an integer, find items whose description contains that number, and return highest price of those items. Return 0 if there is no such item. */ public int findMaxPrice(int n) { return 0; } /* f. FindPriceRange(n,low,high): given int n, find the number of items whose description contains n, and in addition, their prices fall within the given range, [low, high]. */ public int findPriceRange(int n, int low, int high) { return 0; } /* g. RemoveNames(id, list): Remove elements of list from the description of id. It is possible that some of the items in the list are not in the id's description. Return the sum of the numbers that are actually deleted from the description of id. Return 0 if there is no such id. */ public int removeNames(int id, java.util.List list) { return 0; } }
-------------------------------------------------------
import java.io.File; import java.util.List; import java.util.LinkedList; import java.util.Scanner; public class P3Driver { public static void main(String[] args) throws Exception { Scanner in; if (args.length > 0 && !args[0].equals("-")) { File file = new File(args[0]); in = new Scanner(file); } else { in = new Scanner(System.in); } boolean VERBOSE = false; if (args.length > 1) { VERBOSE = Boolean.parseBoolean(args[1]); } String operation = ""; int lineno = 0; MDS mds = new MDS(); Timer timer = new Timer(); int id, result, total = 0, price; List name = new LinkedList<>(); whileloop: while (in.hasNext()) { lineno++; result = 0; operation = in.next(); if(operation.charAt(0) == '#') { in.nextLine(); continue; } switch (operation) { case "End": break whileloop; case "Insert": id = in.nextInt(); price = in.nextInt(); name.clear(); while(true) { int val = in.nextInt(); if(val == 0) { break; } else { name.add(val); } } result = mds.insert(id, price, name); break; case "Find": id = in.nextInt(); result = mds.find(id); break; case "Delete": id = in.nextInt(); result = mds.delete(id); break; case "FindMinPrice": result = mds.findMinPrice(in.nextInt()); break; case "FindMaxPrice": result = mds.findMaxPrice(in.nextInt()); break; case "FindPriceRange": result = mds.findPriceRange(in.nextInt(), in.nextInt(), in.nextInt()); break; case "RemoveNames": id = in.nextInt(); name.clear(); while(true) { int val = in.nextInt(); if(val == 0) { break; } else { name.add(val); } } result = mds.removeNames(id, name); break; default: System.out.println("Unknown operation: " + operation); } total += result; if(VERBOSE) { System.out.println(lineno + "\t" + operation + "\t" + result + "\t" + total); } } System.out.println(total); System.out.println(timer.end()); } public static class Timer { long startTime, endTime, elapsedTime, memAvailable, memUsed; public Timer() { startTime = System.currentTimeMillis(); } public void start() { startTime = System.currentTimeMillis(); } public Timer end() { endTime = System.currentTimeMillis(); elapsedTime = endTime-startTime; memAvailable = Runtime.getRuntime().totalMemory(); memUsed = memAvailable - Runtime.getRuntime().freeMemory(); return this; } public String toString() { return "Time: " + elapsedTime + " msec. " + "Memory: " + (memUsed/1048576) + " MB / " + (memAvailable/1048576) + " MB."; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started