Use the inventoryNew.txt as input file to build a Max-Heap (java PriorityQueue).
ID: 3810742 • Letter: U
Question
Use the inventoryNew.txt as input file to build a Max-Heap (java PriorityQueue). The order of the priority queue is the reverse order of part number. At the end, print the Max-Heap in iterator order and then print the same Max-Heap in priority queue order.
Download Inventory.java and inventoryNew.txt
inventory.java
//********************************************************************
// Inventory.java
//
//********************************************************************
public class Inventory
{
protected String PartNo;
protected String Model;
protected String Description;
protected Double ListPrice;
//-----------------------------------------------------------------
// Constructor: Sets up this inventory using the specified
// information.
//-----------------------------------------------------------------
public Inventory() {
}
public String GetPartNo()
{
return PartNo;
}
public void SetInventory (String ePartNo, String eModel, String eDescription, Double eListPrice)
{
PartNo = ePartNo;
Model = eModel;
Description = eDescription;
ListPrice = eListPrice;
}
//-----------------------------------------------------------------
// Returns a string including the basic inventory information.
//-----------------------------------------------------------------
public String toString()
{
String result = null;
if (PartNo != null)
{
result = "Part Number: " + PartNo + " ";
result += "Model: " + Model + " ";
result += "List Price: "+ Double.toString(ListPrice) + " ";
result += "Description: " + Description + " ";
}
return result;
}
//-----------------------------------------------------------------
//
//-----------------------------------------------------------------
public String getPartNo()
{
return PartNo;
}
public String getModel()
{
return Model;
}
public String getDesc()
{
return Description;
}
public double getListPrice()
{
return ListPrice;
}
}
inventoryNew.txt
GT12C1068A,YUKON XL,07-14 GMC YUKON XL RT Front fender brace Lower Bracket Hinge,24.00
NI27E1251B,ALTIMA SDN,13-15 NISSAN ALTIMA SDN RT Front fender liner From 10-12 (CAPA),63.00
NI23H1297A,ALTIMA SDN,13-15 NISSAN ALTIMA SDN LT Front fender liner From 10-12 (CAPA),48.00
CV15F1067A,SILVERADO 1500 (NEW),07-13 CHEVY SILVERADO 1500 LT Front fender brace Lower Bracket Hinge,23.00
HY07E1288A,SONATA,15-16 HYUNDAI SONATA Front bumper cover 2.4L Std Type w/o Park Assist prime (CAPA),326.00
CV20B1225B,SILVERADO 1500 HYBRID,09-13 CHEVY SILVERADO 1500 HYBRID LT Front fender brace Lower Bracket Hinge,23.00
CV39A1251A,AVALANCHE,07-13 CHEVY AVALANCHE RT Front fender brace Lower Bracket Hinge,24.00
CV39A1250A,SUBURBAN,07-14 CHEVY SUBURBAN RT Front fender brace Lower Bracket Hinge,24.00
AC12C1250AQ,MDX,07-13 ACURA MDX LT Front fender liner (CAPA),68.00
AC12C1251AQ,MDX,07-13 ACURA MDX RT Front fender liner (CAPA),68.00
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TestDrive {
public static List<Inventory> readFromFile(String filePath) throws IOException {
List<Inventory> resultList = new ArrayList<Inventory>();
BufferedReader bufferedInput = new BufferedReader(new FileReader(filePath));
/*
* read each line from file and add it to the List.
*/
String inputLine;
while ((inputLine = bufferedInput.readLine()) != null) {
String[] splittedString = inputLine.split(",");
Inventory inventory = new Inventory();
inventory.SetInventory(splittedString[0], splittedString[1], splittedString[2], Double.valueOf(splittedString[3]));
resultList.add(inventory);
}
bufferedInput.close();
return resultList;
}
public static void main(String args[]) throws IOException {
List<Inventory> resultList = TestDrive.readFromFile("inventoryNew.txt");
MaxHeap maxHeap = new MaxHeap(resultList.size());
/* Add items to inventory */
for (Inventory inventory : resultList) {
maxHeap.insert(inventory);
}
/* Print iterator order */
System.out.println(" Max Heap Iterator Oder : ");
maxHeap.printHeapIterator();
/* Print priorityHeap order */
System.out.println(" Max Heap Priority Oder : ");
while (!maxHeap.isEmpty()) {
System.out.print(maxHeap.extractMax().toString());
}
}
}
class Inventory {
protected String PartNo;
protected String Model;
protected String Description;
protected Double ListPrice;
public Inventory() {
}
public void SetInventory(String ePartNo, String eModel, String eDescription, Double eListPrice) {
PartNo = ePartNo;
Model = eModel;
Description = eDescription;
ListPrice = eListPrice;
}
public String toString() {
String result = null;
if (PartNo != null) {
result = "Part Number: " + PartNo + ", ";
result += "Model: " + Model + ", ";
result += "List Price: " + Double.toString(ListPrice) + ", ";
result += "Description: " + Description + " ";
}
return result;
}
public String getPartNo() {
return PartNo;
}
public String getModel() {
return Model;
}
public String getDesc() {
return Description;
}
public double getListPrice() {
return ListPrice;
}
}
class MaxHeap {
private Inventory array[];
private int filledUp = 0;
public MaxHeap(int size) {
array = new Inventory[size+1];
}
public Inventory getMax() {
return array[1];
}
public void insert(Inventory elem) {
int i = ++filledUp;
array[i] = elem;
shiftUp(i);
}
public void shiftUp(int i) {
while (i > 1) {
if (compare(array[i/2], array[i]) < 0) {
Inventory temp = array[i/2];
array[i/2] = array[i];
array[i] = temp;
i=i/2;
}
else
break;
}
}
public Inventory extractMax() {
Inventory max = getMax();
array[1] = array[filledUp];
filledUp--;
heapify(1);
return max;
}
public void heapify(int i) {
int right = 2*i;
int left = 2*i + 1;
int maxIndex = i;
if (left <= filledUp) {
if (compare(array[left], array[maxIndex]) > 0) {
maxIndex = left;
}
}
if (right <= filledUp) {
if (compare(array[right], array[maxIndex]) < 0) {
maxIndex = right;
}
}
if (maxIndex != i) {
Inventory temp = array[maxIndex];
array[maxIndex] = array[i];
array[i] = temp;
heapify(maxIndex);
}
}
public boolean isEmpty() {
if (filledUp == 0) {
return true;
}
return false;
}
public void printHeapIterator() {
for (int i=1; i<=filledUp; i++) {
System.out.print(array[i].toString());
}
System.out.println(" ");
}
public int compare(Inventory objectOne, Inventory objectTwo) {
String stringOne = objectOne.getPartNo();
String stringTwo = objectTwo.getPartNo();
return stringOne.compareTo(stringTwo);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.