IN JAVA PLEASE. INPUT IS TO BE READ IN FROM A FILE. PLEASE PROVIDE THE INPUT AND
ID: 3753124 • Letter: I
Question
IN JAVA PLEASE.
INPUT IS TO BE READ IN FROM A FILE.
PLEASE PROVIDE THE INPUT AND OUTPUT.
This program assumes that a company has warehouses in 5 tites: New York, Los Angeles, Mami ston, and Chicago. In each warehouse the company stocks three tems. A record contains the amount of each Item currently in each warehouse. The record should contain an array of 5 warehouses with the name of each warehouse and an array of the 3 items with the amount that is currently in stock in the warehouse. Initially the warehouses are empty. Read in a card containing the three prices of the three items. Then read in cards which may be any one of two types: 1. Shipment cards conteining.... s city amt amt2 amt3 This data represents a shipment to a given city of amt1 of item1, amt2 of tem2, and amt3 of item3. 2. Order cards containing... O city amtt amt2 amt3 This data represents an order in a given city of the respective amounts of the various items As each shipment card is read in, print it out, update the amounts in that warehouse and then print on the next line: cty amt2 ams As each order card is read in, print it out. If there is enough in stock at that warehouse of each tem, update the amounts. If there is not enough, search the other warehouses for the one which has the most of that particular item, ship the amount needed from one warehouse to the other, printing out: x of item y shipped from city1 to city2 city1 amt1 amt2 amts 10% extra is to be charged for any item shipped from one city to another. If no single warehouse has enough extra to fill the order for a particular item, print out: Order Unfilled However any shipments of the previous items remain in effect. In either case print out the adjusted... city amtl amt2 amt3 And if the order if filled print.. Price of Order: PriceExplanation / Answer
ANS:
JAVA CODE:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class WareHouse {
public static void main(String[] args) {
// TODO Auto-generated method stubBufferedReader br = null;
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("C:\Users\Tharun\Downloads\chegg\Data.txt");
br = new BufferedReader(fr);
String sCurrentLine;
Map<String,int[]> stock = new HashMap<String,int[]>();
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
String[] temp = sCurrentLine.split("\s+");
String city;
int[] p = new int[3];
if(temp.length== 6)
{
city = temp[1]+temp[2];
p[0]=Integer.parseInt(temp[3]);
p[1]=Integer.parseInt(temp[4]);
p[2]=Integer.parseInt(temp[5]);
}
else
{
city = temp[1];
p[0]=Integer.parseInt(temp[2]);
p[1]=Integer.parseInt(temp[3]);
p[2]=Integer.parseInt(temp[4]);
}
if("s".equals(temp[0]))
{
if(stock.containsKey(city))
{
int[] t = stock.get(city);
for(int i=0;i<3;i++)
t[i] += p[i];
}
else
{
stock.put(city, p);
}
System.out.println(city+" "+stock.get(city)[0]+" "+stock.get(city)[1]+" "+stock.get(city)[2]);
}
else if("o".equals(temp[0]))
{
double price = 0.0;
boolean flag = true;
int[] t = stock.get(city);
if(t[0] >= p[0] && t[1] >= p[1] && t[2] >=p[2])
{
for(int i=0;i<3;i++)
{
t[i] -= p[i];
if(i==0)
price += (2*p[i]);
if(i==1)
price += (7*p[i]);
if(i==2)
price += (8.5*p[i]);
}
}
else
{
if(t[0] >= p[0])
{
t[0] -= p[0];
price += (2*p[0]);
}
else
{
String cityTemp = findMaximum(stock,city,p[0]-t[0], 0);
if(null != cityTemp)
{
System.out.println(p[0]-t[0] +"amount of Item1 shipped from "+cityTemp+" to "+city);
t[0]=0;
int[] cityTempstock = stock.get(cityTemp);
price += (2*p[0])+ (0.1 * p[0]-t[0]);
cityTempstock[0]-=(p[0]-t[0]);
stock.put(cityTemp, cityTempstock);
System.out.println(cityTemp+" "+stock.get(cityTemp)[0]+" "+stock.get(cityTemp)[1]+" "+stock.get(cityTemp)[2]);
}
else
flag =false;
}
if(t[1] >= p[1])
{
t[1] -= p[1];
price += (7*p[1]);
}
else
{
String cityTemp = findMaximum(stock,city,p[1]-t[1], 1);
if(null != cityTemp)
{
System.out.println(p[1]-t[1] +"amount of Item2 shipped from "+cityTemp+" to "+city);
t[1]=0;
int[] cityTempstock = stock.get(cityTemp);
price += (7*p[1])+ (0.1 * p[1]-t[1]);
cityTempstock[1]-=(p[1]-t[1]);
stock.put(cityTemp, cityTempstock);
System.out.println(cityTemp+" "+stock.get(cityTemp)[0]+" "+stock.get(cityTemp)[1]+" "+stock.get(cityTemp)[2]);
}
else
flag =false;
}
if(t[2] >= p[2])
{
t[2] -= p[2];
price += (8.5*p[2]);
}
else
{
String cityTemp = findMaximum(stock,city,p[2]-t[2], 2);
if(null != cityTemp)
{
System.out.println(p[2]-t[2] +"amount of Item3 shipped from "+cityTemp+" to "+city);
t[2]=0;
int[] cityTempstock = stock.get(cityTemp);
price += (2*p[2])+ (0.1 * p[2]-t[2]);
cityTempstock[2]-=(p[2]-t[2]);
stock.put(cityTemp, cityTempstock);
System.out.println(cityTemp+" "+stock.get(cityTemp)[0]+" "+stock.get(cityTemp)[1]+" "+stock.get(cityTemp)[2]);
}
else
flag =false;
}
}
System.out.println(city+" "+stock.get(city)[0]+" "+stock.get(city)[1]+" "+stock.get(city)[2]);
System.out.println("Price is "+price);
if(!flag)
System.out.println("Order Unfilled for some items");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
static String findMaximum(Map<String,int[]> stock,String city,int excess, int index)
{
int max = -1;
String maxCity = null;
for( String c : stock.keySet())
{
if(!c.equals(city))
{
int[] temp = stock.get(c);
if(temp[index] >= max)
{
max = temp[index];
maxCity = c;
}
}
}
if(max > excess)
return maxCity;
else
return null;
}
}
Sample output:
s New York 23 24 1
NewYork 23 24 1
s Miami 25 25 25
Miami 25 25 25
s Los Angleles 40 13 17
LosAngleles 40 13 17
s Houston 100 30 10
Houston 100 30 10
s Chicago 42 23 19
Chicago 42 23 19
s New York 0 0 15
NewYork 23 24 16
s Miami 13 17 21
Miami 38 42 46
o Los Angleles 15 10 15
LosAngleles 25 3 2
Price is 227.5
o New York 12 24 8
NewYork 11 0 8
Price is 260.0
o Houston 75 45 10
15amount of Item2 shipped from Miami to Houston
Miami 38 -3 46
Houston 25 0 0
Price is 554.5
o Chicago 20 15 15
Chicago 22 8 4
Price is 272.5
o New York 15 0 0
4amount of Item1 shipped from Miami to NewYork
Miami 23 -3 46
NewYork 0 0 8
Price is 31.5
s Los Angleles 10 20 10
LosAngleles 35 23 12
s Houston 0 30 40
Houston 25 30 40
o New York 15 15 25
15amount of Item1 shipped from LosAngleles to NewYork
LosAngleles 20 23 12
15amount of Item2 shipped from Houston to NewYork
Houston 25 15 40
17amount of Item3 shipped from Miami to NewYork
Miami 23 -3 21
NewYork 0 0 0
Price is 190.5
o Chicago 75 30 40
22amount of Item2 shipped from LosAngleles to Chicago
LosAngleles 20 -7 12
36amount of Item3 shipped from Houston to Chicago
Houston 25 15 0
Chicago 22 0 0
Price is 297.0
Order Unfilled for some items
s New York 20 15 20
NewYork 20 15 20
o Houston 10 20 10
5amount of Item2 shipped from NewYork to Houston
NewYork 20 -5 20
10amount of Item3 shipped from Miami to Houston
Miami 23 -3 11
Houston 15 0 0
Price is 183.0
THANK YOU
COMMENT BELOW IF YOU HAVE ANY DOUBTS