In: Computer Science
PLEASE DO IN C#
Warehouse Inventories
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 C# 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.
Requirements:
The class must be named Inventory.
The main program (driver) must be named Warehouses.
A driver (main class) as well as a programmer-defined class must be used.
Submit program through the assignment tool in Moodle2.
Include a comment stating your name.
Each student must create his/her own independent program.
Warehouse Inventories
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 C# 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.
Requirements:
The class must be named Inventory.
The main program (driver) must be named Warehouses.
A driver (main class) as well as a programmer-defined class must be used.
Complete code in C#:-
using System;
using System.IO;
using System.Collections.Generic;
// Inventory class.
class Inventory {
// Instance variable, 'Name' stores class name.
public String Name;
// 'Items' stores quantity of each one of the 5
items.
public int[] Items;
// Constructor, creates Inventory object.
public Inventory(int[] items, String Name) {
// Sets name of class
this.Name = Name;
// Sets quantity of each
item.
this.Items = new int[5];
for(int i = 0; i < 5; i++)
{
this.Items[i] =
items[i];
}
}
// Printing Status of Each Inventory.
public void Print() {
Console.WriteLine(this.Name);
Console.Write(this.Items[0]+"
"+this.Items[1]+" "+this.Items[2]+" "+this.Items[3]+"
"+this.Items[4]+"\n\n");
}
}
// Main driver class.
class Warehouses {
// This converts String array to Integer
array.
private static void ParseStringToInteger(String[]
strlist, int[] Items) {
Items[0] =
Int32.Parse(strlist[0]);
Items[1] =
Int32.Parse(strlist[1]);
Items[2] =
Int32.Parse(strlist[2]);
Items[3] =
Int32.Parse(strlist[3]);
Items[4] =
Int32.Parse(strlist[4]);
}
// This Function gives the inventory of minimum
quantity of a specified item, represented by 'index'.
private static Inventory minInventory(Inventory one,
Inventory two, Inventory three, Inventory fourth, Inventory fifth,
Inventory sixth, int index) {
// Calculate minimum quantity of
the item in all classes.
int min =
Math.Min(one.Items[index], Math.Min(two.Items[index],
Math.Min(three.Items[index], Math.Min(fourth.Items[index],
Math.Min(fifth.Items[index], sixth.Items[index])))));
// If cases, matches the minimum
quantity with the present in each inventory.
// Which ever gets match, return
that inventory.
if(min == one.Items[index]) {
return
one;
}
else if(min == two.Items[index])
{
return
two;
}
else if(min == three.Items[index])
{
return
three;
}
else if(min == fourth.Items[index])
{
return
fourth;
}
else if(min == fifth.Items[index])
{
return
fifth;
}
return sixth;
}
// Same as 'minInventory()' function except the
only thing that it will return the inventory with maximum quantity
instead of minimum.
private static Inventory maxInventory(Inventory one,
Inventory two, Inventory three, Inventory fourth, Inventory fifth,
Inventory sixth, int index) {
// Finding maximum quantity
int max =
Math.Max(one.Items[index], Math.Max(two.Items[index],
Math.Max(three.Items[index], Math.Max(fourth.Items[index],
Math.Max(fifth.Items[index], sixth.Items[index])))));
// Retrun inventory that matches
with maximum quantity.
if(max == one.Items[index]) {
return
one;
}
else if(max == two.Items[index])
{
return
two;
}
else if(max == three.Items[index])
{
return
three;
}
else if(max == fourth.Items[index])
{
return
fourth;
}
else if(max == fifth.Items[index])
{
return
fifth;
}
return sixth;
}
// Main function.
public static void Main(String[] args) {
// Dictionary which defines
mapping between item number and its index in "Inventory.Items"
array.
Dictionary<String, int>
mapping = new Dictionary<String, int>();
mapping.Add("102", 0);
mapping.Add("215", 1);
mapping.Add("410", 2);
mapping.Add("525", 3);
mapping.Add("711", 4);
// Asking for Inventory file
name.
Console.Write("Enter Invetory File
name : ");
// Reading file name from
terminal
String inventoryFile =
Console.ReadLine();
String line;
// Creating instances of
Inventory.
Inventory Atlanta, Baltimore,
Chicago, Denver, Ely, Fargo, warehouse;
// This 'Items' array will store
the quantity for each item in each inventory.
int[] Items = new int[5];
// Trying to open inputs and read
them.
// Is exception gets generate,
catch it in 'catch' block.
try {
// Creating
input stream for 'Inventor.txt' file.
using
(StreamReader sr = new StreamReader(inventoryFile)) {
// Reading data for 'Atlanta'.
line = sr.ReadLine();
String[] strlist = line.Split(" ");
ParseStringToInteger(strlist, Items);
// Creating 'Atlanta' object.
Atlanta = new Inventory(Items, "Atlanta");
// Reading data for 'Baltmiore'
line = sr.ReadLine();
strlist = line.Split(" ");
ParseStringToInteger(strlist, Items);
// Creating Object for 'Baltimore'.
Baltimore = new Inventory(Items,
"Baltimore");
// Reading data for 'Chicago'.
line = sr.ReadLine();
strlist = line.Split(" ");
ParseStringToInteger(strlist, Items);
// Creating 'Chicago' object.
Chicago = new Inventory(Items, "Chicago");
// Reading data for 'Denver'.
line = sr.ReadLine();
strlist = line.Split(" ");
ParseStringToInteger(strlist, Items);
// Creating 'Denver' object.
Denver = new Inventory(Items, "Denver");
// Reading data for 'Ely'.
line = sr.ReadLine();
strlist = line.Split(" ");
ParseStringToInteger(strlist, Items);
// Creating 'Ely' object.
Ely = new Inventory(Items, "Ely");
// Reading data for 'Fargo'.
line = sr.ReadLine();
strlist = line.Split(" ");
ParseStringToInteger(strlist, Items);
// Creating 'Fargo' object.
Fargo = new Inventory(Items, "Fargo");
}
// Priting
'Inventory's' status before processing transactions.
Console.WriteLine("---------------------------------------------------------------------------------");
Console.WriteLine("Status of each Inventory before Transactions:
");
Atlanta.Print();
Baltimore.Print();
Chicago.Print();
Denver.Print();
Ely.Print();
Fargo.Print();
// Processing
transactions and printing status of updated inventory.
Console.WriteLine("\n---------------------------------------------------------------------------------");
Console.Write("Enter Transaction File name : ");
// Reading transaction file name from user.
String
transactionFile = Console.ReadLine();
// Creating
stream for transaction
using
(StreamReader sr = new StreamReader(transactionFile)) {
// Reading transaction file line by line.
while((line = sr.ReadLine()) != null) {
String[] strlist =
line.Split(" ");
// Using swith-case mechanism
for handling different tranactions.
// P OR S.
switch (strlist[0]) {
case
"P":
// If transaction is of type purchasing
warehouse = minInventory(Atlanta, Baltimore,
Chicago, Denver, Ely, Fargo, mapping[strlist[1]]);
warehouse.Items[mapping[strlist[1]]] +=
Int32.Parse(strlist[2]);
warehouse.Print();
break;
case
"S":
// If
transaction is of type selling.
warehouse = maxInventory(Atlanta, Baltimore,
Chicago, Denver, Ely, Fargo, mapping[strlist[1]]);
warehouse.Items[mapping[strlist[1]]] -=
Int32.Parse(strlist[2]);
warehouse.Print();
break;
default
:
Console.WriteLine("File contains unwanted
data.");
break;
}
}
}
// Printing
Inventory's status after processing transactions.
Console.WriteLine("\n---------------------------------------------------------------------------------");
Console.WriteLine("Status of each Inventory after Transactions:
");
Atlanta.Print();
Baltimore.Print();
Chicago.Print();
Denver.Print();
Ely.Print();
Fargo.Print();
// Catch block for hndling
generated exceptions.
} catch (Exception e) {
// Let the user know what went wrong.
Console.WriteLine("The file could not be
read:");
Console.WriteLine(e.Message);
}
}
}
Screenshot of output:-