Objective: Work with multiple objects and review reading data files. Description
ID: 3682135 • Letter: O
Question
Objective:
Work with multiple objects and review reading data files.
Description: A wholesale distributor has six warehouses (Atlanta, Baltimore, Chicago, Denver, Ely and Fargo) and sells five different items (identified by part number: 102, 215, 410, 525 and 711). Each warehouse may stock any or all of the five items. The company buys and sells these items constantly. Company transaction records contain a transaction code (‘P’ for a purchase or ‘S’ for a sale) followed by an item number and the quantity (bought or sold).
The transaction records are contained in a transaction data file named Transactions.txt.
Sample transaction records: Transactions.txt
P 410 1000
S 215 120
S 711 300
|
A separate data file contains the initial status of the six warehouses at the beginning of the day (i.e., the ending status from the night before). This data file has only six records (lines). Each record (line) contains five numbers that show the quantity on hand for the five items in that warehouse. This file is named Inventory.txt.
Sample status data file: Inventory.txt
500 120 60 0 350
100 230 0 50 0
0 75 0 0 220
600 50 120 300 40
210 160 30 80 50
90 50 90 200 70
|
The status data file is updated by processing the transaction records in the transaction data file according to these rules:
1 – For a sale (‘S’) – subtract the quantity sold from the warehouse that
has the largest supply of that item on hand.
2 – For a purchase (‘P’) – add the quantity purchased to the warehouse
that has the lowest supply of that item on hand.
Instructions:
Write an object-oriented Java program to do the above inventory warehouse processing. Each of the six warehouses should be treated as an individual object. For example, Atlanta would be an object with each of the five part numbers as instance fields. Each of the other warehouses should also be objects with the five part numbers as instance fields. Of course, there would be one class which would be the main (driver) class from which these 6 objects would be created.
In the beginning of the program, the status data file (Inventory.txt) should be read and an object for each warehouse created. The Inventory.txt data file is in the following order: the first line is the Atlanta warehouse, the second line is the Baltimore warehouse, third Chicago, then Denver, Ely and Fargo. After the objects are created, the transactions data file (Transactions.txt) are read and processed.
The objects should be updated as the transaction records are read and processed.
The program should:
1 – Display the initial (beginning-of-day) status for all warehouses.
2 – Process each transaction from the transaction data file and show which
warehouse’s inventory was updated to reflect that transaction.
3 – Display the final (end-of-day) status for all warehouses.
Inventory.txt
Transactions.txt
Explanation / Answer
Warehouse.java
package warehouse;
public class Warehouse {
private int product102;
private int product215;
private int product410;
private int product525;
private int product711;
public Warehouse(int product102, int product215, int product410, int product525, int product711) {
this.product102 = product102;
this.product215 = product215;
this.product410 = product410;
this.product525 = product525;
this.product711 = product711;
}
public int getProduct102() {
return product102;
}
public void setProduct102(int product102) {
this.product102 = product102;
}
public int getProduct215() {
return product215;
}
public void setProduct215(int product215) {
this.product215 = product215;
}
public int getProduct410() {
return product410;
}
public void setProduct410(int product410) {
this.product410 = product410;
}
public int getProduct525() {
return product525;
}
public void setProduct525(int product525) {
this.product525 = product525;
}
public int getProduct711() {
return product711;
}
public void setProduct711(int product711) {
this.product711 = product711;
}
}
WarehouseProcessing.java
package warehouse;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class WarehouseProcessing {
public static void main(String[] args) {
List<Warehouse> warehouseList = new LinkedList<>();
Integer maxInventory[] = {0,0,0,0,0};
Integer minInventory[] = {5000,5000,5000,5000,5000};
Integer maxInventoryWarehouse[] = {0,0,0,0,0};
Integer minInventoryWarehouse[] = {0,0,0,0,0};
String line;
String tokens[];
Map<Integer, String> mapWarehouse = new HashMap<>();
mapWarehouse.put(0, "Atlanta");
mapWarehouse.put(1, "Baltimore");
mapWarehouse.put(2, "Chicago");
mapWarehouse.put(3, "Denver");
mapWarehouse.put(4, "Ely");
mapWarehouse.put(5, "Fargo");
try {
BufferedReader br = new BufferedReader(new FileReader("inventory.txt"));
line = br.readLine();
while (line != null) {
tokens = line.split(", ");
Warehouse warehouse = new Warehouse(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]),
Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3]), Integer.parseInt(tokens[4]));
warehouseList.add(warehouse);
line = br.readLine();
}
for (int i = 0; i < 6; i++) {
Warehouse wh = warehouseList.get(i);
if (wh.getProduct102() > maxInventory[0]) {
maxInventory[0] = wh.getProduct102();
maxInventoryWarehouse[0] = i;
}
if (wh.getProduct215() > maxInventory[1]) {
maxInventory[1] = wh.getProduct215();
maxInventoryWarehouse[1] = i;
}
if (wh.getProduct410() > maxInventory[2]) {
maxInventory[2] = wh.getProduct410();
maxInventoryWarehouse[2] = i;
}
if (wh.getProduct525() > maxInventory[3]) {
maxInventory[3] = wh.getProduct525();
maxInventoryWarehouse[3] = i;
}
if (wh.getProduct711() > maxInventory[4]) {
maxInventory[4] = wh.getProduct711();
maxInventoryWarehouse[4] = i;
}
if (wh.getProduct102() < minInventory[0]) {
minInventory[0] = wh.getProduct102();
minInventoryWarehouse[0] = i;
}
if (wh.getProduct215() < minInventory[1]) {
minInventory[1] = wh.getProduct215();
minInventoryWarehouse[1] = i;
}
if (wh.getProduct410() < minInventory[2]) {
minInventory[2] = wh.getProduct410();
minInventoryWarehouse[2] = i;
}
if (wh.getProduct525() < minInventory[3]) {
minInventory[3] = wh.getProduct525();
minInventoryWarehouse[3] = i;
}
if (wh.getProduct711() < minInventory[4]) {
minInventory[4] = wh.getProduct711();
minInventoryWarehouse[4] = i;
}
}
br = new BufferedReader(new FileReader("transactions.txt"));
line = br.readLine();
while (line != null) {
tokens = line.split(", ");
if (tokens[0].equals("P")) {
int product = Integer.parseInt(tokens[1]);
Warehouse wh;
switch (product) {
case 102:
System.out.println("Warehouse " + mapWarehouse.get(minInventoryWarehouse[0]) + "was updated");
wh = warehouseList.get(minInventoryWarehouse[0]);
wh.setProduct102(wh.getProduct102() + Integer.parseInt(tokens[2]));
updateMinInventoryWarehouse(product, warehouseList, minInventory, minInventoryWarehouse);
break;
case 215:
System.out.println("Warehouse " + mapWarehouse.get(minInventoryWarehouse[1]) + "was updated");
wh = warehouseList.get(minInventoryWarehouse[1]);
wh.setProduct102(wh.getProduct215() + Integer.parseInt(tokens[2]));
updateMinInventoryWarehouse(product, warehouseList, minInventory, minInventoryWarehouse);
break;
case 410:
System.out.println("Warehouse " + mapWarehouse.get(minInventoryWarehouse[2]) + "was updated");
wh = warehouseList.get(minInventoryWarehouse[2]);
wh.setProduct102(wh.getProduct410() + Integer.parseInt(tokens[2]));
updateMinInventoryWarehouse(product, warehouseList, minInventory, minInventoryWarehouse);
break;
case 525:
System.out.println("Warehouse " + mapWarehouse.get(minInventoryWarehouse[3]) + "was updated");
wh = warehouseList.get(minInventoryWarehouse[3]);
wh.setProduct102(wh.getProduct525() + Integer.parseInt(tokens[2]));
updateMinInventoryWarehouse(product, warehouseList, minInventory, minInventoryWarehouse);
break;
case 711:
System.out.println("Warehouse " + mapWarehouse.get(minInventoryWarehouse[4]) + "was updated");
wh = warehouseList.get(minInventoryWarehouse[4]);
wh.setProduct102(wh.getProduct711() + Integer.parseInt(tokens[2]));
updateMinInventoryWarehouse(product, warehouseList, minInventory, minInventoryWarehouse);
break;
}
}
else {
int product = Integer.parseInt(tokens[1]);
Warehouse wh;
switch (product) {
case 102:
System.out.println("Warehouse " + mapWarehouse.get(maxInventoryWarehouse[0]) + "was updated");
wh = warehouseList.get(maxInventoryWarehouse[0]);
wh.setProduct102(wh.getProduct102() - Integer.parseInt(tokens[2]));
updateMinInventoryWarehouse(product, warehouseList, maxInventory, maxInventoryWarehouse);
break;
case 215:
System.out.println("Warehouse " + mapWarehouse.get(maxInventoryWarehouse[1]) + "was updated");
wh = warehouseList.get(maxInventoryWarehouse[1]);
wh.setProduct102(wh.getProduct215() - Integer.parseInt(tokens[2]));
updateMinInventoryWarehouse(product, warehouseList, maxInventory, maxInventoryWarehouse);
break;
case 410:
System.out.println("Warehouse " + mapWarehouse.get(maxInventoryWarehouse[2]) + "was updated");
wh = warehouseList.get(maxInventoryWarehouse[2]);
wh.setProduct102(wh.getProduct410() - Integer.parseInt(tokens[2]));
updateMinInventoryWarehouse(product, warehouseList, maxInventory, maxInventoryWarehouse);
break;
case 525:
System.out.println("Warehouse " + mapWarehouse.get(maxInventoryWarehouse[3]) + "was updated");
wh = warehouseList.get(maxInventoryWarehouse[3]);
wh.setProduct102(wh.getProduct525() - Integer.parseInt(tokens[2]));
updateMinInventoryWarehouse(product, warehouseList, maxInventory, maxInventoryWarehouse);
break;
case 711:
System.out.println("Warehouse " + mapWarehouse.get(maxInventoryWarehouse[4]) + "was updated");
wh = warehouseList.get(maxInventoryWarehouse[4]);
wh.setProduct102(wh.getProduct711() - Integer.parseInt(tokens[2]));
updateMinInventoryWarehouse(product, warehouseList, maxInventory, maxInventoryWarehouse);
break;
}
}
line = br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void updateMinInventoryWarehouse(int product, List<Warehouse> warehouseList, Integer minInventory[], Integer minInventoryWarehouse[]) {
switch(product) {
case 102:
for (int i = 0; i < 6; i++) {
Warehouse wh = warehouseList.get(i);
if (wh.getProduct102() < minInventory[0]) {
minInventory[0] = wh.getProduct102();
minInventoryWarehouse[0] = i;
}
}
break;
case 215:
for (int i = 0; i < 6; i++) {
Warehouse wh = warehouseList.get(i);
if (wh.getProduct215() < minInventory[1]) {
minInventory[1] = wh.getProduct215();
minInventoryWarehouse[1] = i;
}
}
break;
case 410:
for (int i = 0; i < 6; i++) {
Warehouse wh = warehouseList.get(i);
if (wh.getProduct410() < minInventory[2]) {
minInventory[2] = wh.getProduct410();
minInventoryWarehouse[2] = i;
}
}
break;
case 525:
for (int i = 0; i < 6; i++) {
Warehouse wh = warehouseList.get(i);
if (wh.getProduct525() < minInventory[3]) {
minInventory[3] = wh.getProduct525();
minInventoryWarehouse[3] = i;
}
}
break;
case 711:
for (int i = 0; i < 6; i++) {
Warehouse wh = warehouseList.get(i);
if (wh.getProduct711() < minInventory[4]) {
minInventory[4] = wh.getProduct711();
minInventoryWarehouse[4] = i;
}
}
break;
}
}
private static void updateMaxInventoryWarehouse(int product, List<Warehouse> warehouseList, Integer maxInventory[], Integer maxInventoryWarehouse[]) {
switch(product) {
case 102:
for (int i = 0; i < 6; i++) {
Warehouse wh = warehouseList.get(i);
if (wh.getProduct102() > maxInventory[0]) {
maxInventory[0] = wh.getProduct102();
maxInventoryWarehouse[0] = i;
}
}
break;
case 215:
for (int i = 0; i < 6; i++) {
Warehouse wh = warehouseList.get(i);
if (wh.getProduct215() > maxInventory[1]) {
maxInventory[1] = wh.getProduct215();
maxInventoryWarehouse[1] = i;
}
}
break;
case 410:
for (int i = 0; i < 6; i++) {
Warehouse wh = warehouseList.get(i);
if (wh.getProduct410() > maxInventory[2]) {
maxInventory[2] = wh.getProduct410();
maxInventoryWarehouse[2] = i;
}
}
break;
case 525:
for (int i = 0; i < 6; i++) {
Warehouse wh = warehouseList.get(i);
if (wh.getProduct525() > maxInventory[3]) {
maxInventory[3] = wh.getProduct525();
maxInventoryWarehouse[3] = i;
}
}
break;
case 711:
for (int i = 0; i < 6; i++) {
Warehouse wh = warehouseList.get(i);
if (wh.getProduct711() > maxInventory[4]) {
maxInventory[4] = wh.getProduct711();
maxInventoryWarehouse[4] = i;
}
}
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.