Use the inventoryNew.txt as input file to build a Max-Heap (java PriorityQueue).
ID: 3809946 • 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
This is Inventory Class:
package com.priorityQueue;
public class Inventory implements Comparable<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 Inventory(String partNo, String model, String description,
String listPrice) {
super();
this.PartNo = partNo;
this.Model = model;
this.Description = description;
this.ListPrice = Double.parseDouble(listPrice);
}
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;
}
@Override
public int compareTo(Inventory o) {
// TODO Auto-generated method stub
return 0;
}
}
This is Driver Class:
package com.priorityQueue;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Collections;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
public class TestPriorityQueue {
static Inventory inventory=null;
static Queue<Inventory> queue=new PriorityQueue<Inventory>();
public static void getMenuOption(){
System.out.println(" All Operations ");
System.out.println("Enter 1 for load ");
System.out.println("Enter 2 for print");
System.out.println("Enter 3 to Exit");
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
getMenuOption();
while(true){
System.out.print(" Enter your selection [1,2 & 3]: ");
int choice = in.nextInt();
switch (choice)
{
case 1 :
System.out.print("Input file: ");
//Give here your "inventoryNew.txt" path where you put
String csvFile = in.next();
String line = "";
String cvsSplitBy = ",";
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
String[] str = line.split(cvsSplitBy);
String partNo=str[0];
String model=str[1];
String description=str[2];
String listPrice=str[3];
inventory=new Inventory(partNo, model, description, listPrice);
queue.add(inventory);
//System.out.println(" " + str[0] +" "+str[1]+" "+str[2]+" "+str[3]);
}
}catch (Exception exception)
{
System.out.println("Error processing file: "+exception.getMessage());
}
break;
case 2 : try{
System.out.println(" Print Details");
System.out.println("===========================");
Collections.reverseOrder();
Iterator itr=queue.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}catch(Exception ee){
System.out.println("Queue is Empty");
}
break;
case 3 :
System.out.println("Bye....THANK YOU");
System.exit(0);
break;
default :
System.out.println("Wrong Entry ");
break;
}
}
}
}
Output:
All Operations
Enter 1 for load
Enter 2 for print
Enter 3 to Exit
Enter your selection [1,2 & 3]: 1
Input file: inventoryNew.txt
Enter your selection [1,2 & 3]: 2
Print Details
===========================
Part Number: GT12C1068A Model: YUKON XL List Price: 24.0
Description: 07-14 GMC YUKON XL RT Front fender brace Lower Bracket Hinge
Part Number: NI27E1251B Model: ALTIMA SDN List Price: 63.0
Description: 13-15 NISSAN ALTIMA SDN RT Front fender liner From 10-12 (CAPA)
Part Number: NI23H1297A Model: ALTIMA SDN List Price: 48.0
Description: 13-15 NISSAN ALTIMA SDN LT Front fender liner From 10-12 (CAPA)
Part Number: CV15F1067A Model: SILVERADO 1500 (NEW) List Price: 23.0
Description: 07-13 CHEVY SILVERADO 1500 LT Front fender brace Lower Bracket Hinge
Part Number: HY07E1288A Model: SONATA List Price: 326.0
Description: 15-16 HYUNDAI SONATA Front bumper cover 2.4L Std Type w/o Park Assist prime (CAPA)
Part Number: CV20B1225B Model: SILVERADO 1500 HYBRID List Price: 23.0
Description: 09-13 CHEVY SILVERADO 1500 HYBRID LT Front fender brace Lower Bracket Hinge
Part Number: CV39A1251A Model: AVALANCHE List Price: 24.0
Description: 07-13 CHEVY AVALANCHE RT Front fender brace Lower Bracket Hinge
Part Number: CV39A1250A Model: SUBURBAN List Price: 24.0
Description: 07-14 CHEVY SUBURBAN RT Front fender brace Lower Bracket Hinge
Part Number: AC12C1250AQ Model: MDX List Price: 68.0
Description: 07-13 ACURA MDX LT Front fender liner (CAPA)
Part Number: AC12C1251AQ Model: MDX List Price: 68.0
Description: 07-13 ACURA MDX RT Front fender liner (CAPA)
Enter your selection [1,2 & 3]: 3
Bye....THANK YOU
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.