The objectives of this assignment are to: Gain experience using the Eclipse IDE;
ID: 3686316 • Letter: T
Question
The objectives of this assignment are to:
Gain experience using the Eclipse IDE; including its environment for managing files.
Gain an understanding of input and output of text files.
Gain experience in using data structures to hold data in memory.
Gain experience with the Comparable interface and searching arrays of objects.
Practice good programming techniques.
You have been asked to produce a Java Application that allows the user to display a report on a particular part. You will need to create three classes: Prog8, Warehouse, and Part. The report will use two files: parts.txt and warehouse.txt with the formats shown below.
warehouse.txt file: text file with the following fields separated by spaces:
Fields
Description
Warehouse ID
3 characters
City
maximum 20 characters
State
2 characters
parts.txt file: CSV (comma separated value) text file in which each line in the file represents a warehouse. The comma delimited fields are:
Fields
Description
Part Number
5 characters
Description
maximum 30 characters
Price
double
Warehouse ID
3 characters
Quantity
Integer, quantity at this warehouse
Read each file into an array of objects. The user will enter a part number and the program will display a report containing the part information and quantities at each warehouse in the format below. Note that Part should be comparable based upon part number. If the part is not found, display a message stating that fact.
Pre-dug Post Hole
Part#: 86420
Price: 2.49
Warehouse Qty
Atlanta 34
Louisville 68
Pittsburgh 124
Total 256
Testing
When your program is finished, test it using the following part numbers:
86420, 01020, and 15705.
Requirements
· Your source code should be documented using Javadoc style according to the course standards.
· Abide by good programming practices.
· Use the default package in your project; do not specify a package in your code. Use of a package will interfere with testing and severely impact grading of your program.
· Use any file and class names exactly as specified in the assignment.
Fields
Description
Warehouse ID
3 characters
City
maximum 20 characters
State
2 characters
Explanation / Answer
Hello there ,
Please find below code and it's O/P.
/**
* Represents Part.
*
* @author dipal.prajapati
*
*/
public class Part implements Comparable {
String partNumber;
String desc;
double price;
String warehouseID;
int quantity;
public Part(String partNumber, String desc, double price,
String warehouseID, int quantity) {
super();
this.partNumber = partNumber;
this.desc = desc;
this.price = price;
this.warehouseID = warehouseID;
this.quantity = quantity;
}
@Override
public String toString() {
return "Part [partNumber=" + partNumber + ", desc=" + desc + ", price="
+ price + ", warehouseID=" + warehouseID + ", quantity="
+ quantity + "]";
}
public int compareTo(Object anotherPart) throws ClassCastException {
if (!(anotherPart instanceof Part))
throw new ClassCastException("A Part object expected.");
String anotherPartNumber = ((Part) anotherPart).partNumber;
return this.partNumber.compareTo(anotherPartNumber);
}
}
======
/**
* Represents Warehouse.
* @author dipal.prajapati
*
*/
public class Warehouse {
String warehouseID;
String city;
String state;
public Warehouse(String warehouseID, String city, String state) {
super();
this.warehouseID = warehouseID;
this.city = city;
this.state = state;
}
@Override
public String toString() {
return "Warehouse [warehouseID=" + warehouseID + ", city=" + city
+ ", state=" + state + "]";
}
}
=====
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Prog8 {
static Map<String, List<Part>> mapOfPart = new HashMap<String, List<Part>>();
static Map<String, Warehouse> mapOfWarehouse = new HashMap<String, Warehouse>();
public static void main(String args[]) {
System.out.println("Pre-dug Post Hole ");
readFiles();
System.out.println(createReport("86420"));
System.out.println(createReport("01020"));
System.out.println(createReport("15705"));
}
/**
* It creates reports.
* @param partNumber
* @return
*/
public static String createReport(String partNumber) {
StringBuffer buffer = new StringBuffer();
List<Part> listOfParts = mapOfPart.get(partNumber);
buffer.append("Part#:" + partNumber + " ");
buffer.append("Price:" + mapOfPart.get(partNumber).get(0).price + " ");
buffer.append("Warehouse Qty ");
if (mapOfPart.containsKey(partNumber)) {
for (int i = 0; i < listOfParts.size(); i++) {
Part part = listOfParts.get(i);
if (mapOfWarehouse.containsKey(part.warehouseID)) {
buffer.append(mapOfWarehouse.get(part.warehouseID).city
+ " " + part.quantity + " ");
}
}
} else {
buffer.append("Part +" + partNumber + " not found.");
}
return buffer.toString();
}
/**
* It reads both the files and populate the maps.
*/
public static void readFiles() {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("parts.txt")); //Reading part file.
while ((sCurrentLine = br.readLine()) != null) {
String[] array = sCurrentLine.split(",");
if (mapOfPart.containsKey(array[0])) {
List<Part> listOfParts = mapOfPart.get(array[0]);
listOfParts.add(new Part(array[0], array[1], Double
.parseDouble(array[2]), array[3], Integer
.parseInt(array[4])));
mapOfPart.put(array[0], listOfParts);
} else {
List<Part> listOfParts = new ArrayList<Part>();
listOfParts.add(new Part(array[0], array[1], Double
.parseDouble(array[2]), array[3], Integer
.parseInt(array[4])));
mapOfPart.put(array[0], listOfParts);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("warehouse.txt")); //Reading warehouse file.
while ((sCurrentLine = br.readLine()) != null) {
String[] array = sCurrentLine.split(" ");
mapOfWarehouse.put(array[0], new Warehouse(array[0], array[1],
array[2]));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
=====O/P===
Pre-dug Post Hole
Part#:86420
Price:2.49
Warehouse Qty
Atlanta 23
Minneapolis 15
Louisville 15
Nashville 9
Part#:01020
Price:11.4
Warehouse Qty
Louisville 17
Nashville 20
Part#:15705
Price:9.78
Warehouse Qty
Louisville 9
Houston 4
Atlanta 50
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.