Programming Geek
Rated 4.1/5 based on 446 reviews

### Using WEBGPSS to find solution of MuPhoria second edition

If you have not read the problem statement, read it here .

Here goes one of the approach to find the solution of problem of MuPhoria second edition. This is not the winning entry but will give you ample idea on how to solve the problem. This will also introduce you to a new a general purpose analysis software WEBGPSS. Follow the steps to get a not optimum but a decent solution using WEBGPSS and java programming.

Here goes a few steps on how to solve the problem:

Step 1 : On the basis of the day number, corresponding week is selected.

Step 2
: The Demand and Forecast for all the weeks is set by introducing  Normal Distribution Function on the sale of the previous year’s corresponding week.

Step 3 : The Demand and Stock value is compared.
If Demand  >  Stock  then
·        Give order for the Forecast of that day to the suppliers lead time ago.
·        Increase the Stock of that day as the orders given to the suppliers lead time ago will arrive on this day.
Stock = Stock + Forecast
/*Now, we have enough ingredients in our Stock to meet the Demand of the current day, so we will fulfil the Demand by providing ingredients from the Stock. So, at the end of the day, the Stock would be left over with the following amount.*/
Stock = Stock - Demand
/*Now, we also have to maintain certain amount of ingredients always in our stock for meeting any sudden increase of Demand. For that, we are maintaining a minimum amount called Re-order point (ROP) in our stock. Whenever the Stock value goes below Re-order point, Order to the suppliers for maintaining the Re-order point is placed (Current day – Lead time) ago.*/

If Stock < ROP  then

/*Give order of (ROP - Stock) to the three suppliers (Current day – Lead time) ago. So, the Stock value will get increased as the orders given to the suppliers lead time ago will arrive on this day.*/

Val1 = ROP – Stock  (Val1 : Amount ordered to the suppliers)
Stock = Stock + Val1

else  If Demand < Stock then

·        If (Stock - Demand) <= ROP then

Give order for the Forecast of that day to the suppliers lead time ago.
Increase the Stock of that day as the orders given to the suppliers lead time ago will arrive on this day.

Stock = Stock + Forecast

So, at the end of the day, the Stock would be left over with the following amount.
Stock = Stock - Demand
If Stock < ROP  then

Give order of (ROP - Stock) to the three suppliers (Current day – Lead time) ago. So, the Stock value will get increased as the orders given to the suppliers lead time ago will arrive on this day.

Val1 = ROP – Stock  (Val1 : Amount ordered to the suppliers)
Stock = Stock + Val1

else If (Stock - Demand) > ROP then

It means we have enough ingredients in our Stock to maintain ROP. So, at the end of the day, the Stock would be left over with the following amount.

Stock = Stock - Demand

If Stock < ROP  then

Give order of (ROP - Stock) to the three suppliers (Current day – Lead time) ago. So, the Stock value will get increased as the orders given to the suppliers lead time ago will arrive on this day.

Val1 = ROP – Stock  (Val1 : Amount ordered to the suppliers)
Stock = Stock + Val1

Step 4 : Terminate

All the above mentioned steps will be repeated for each day for each of the four ingredients.

Note :

·        There is no possibility of “Unmet Demand” in our algorithm. We are able to meet the customer’s demand at any cost.
If the Forecast for a particular day is greater than the Demand, then it   is easily fulfilled.
But even if the Forecast for a particular day is less than the Demand, then the extra Demand (Demand - Forecast) will be provided by the stock.
So we are taking good care of our customers.

·        We are also keeping the Stock fresh as we are receiving the supply in every 2-3 days. We have to spend the “Processing cost” only for Chilli on the seventh day as shelf life of Chilli is 7 days. Otherwise, we have no Processing cost on any ingredient on any other day.

·        We are giving orders of the ingredients to the suppliers for few days of the month of August in July only because for receiving the ingredients in initial days of August, we have to place the order in last days of July. So, we are considering this also.

Have a look at following diagram:

1) The application is developed in java and WEBGPSS so JDK 1.6 (or later) and WEBGPSS must be installed (to compile and run the application) . To run the application JRE 1.6 (or later) is sufficient.

2) Download here the solution package. Do not extract the muphoria.zip in c:\ drive(on windows) or root directory(\) on UNIX because the output files might not be generated due to insufficient write permission in system directory.

3) src folder includes source files and build folder includes build files.

Getting Started
1)    ordersummary.txt is obtained by saving Historical Order Summary as type txt (tab delimited).
2)    supplierdetails.txt is obtained by saving Supplier Details as type txt (tab delimited).

Do not move these files from its directory.

Run Application
Step 1 : After installation of WebGPSS,

Start >> All Programs >> WebGPSS >> Server

Click Set button.

Step 2 :      Start >> All Programs >> WebGPSS >> Client

Click OK button.

Step 3 : Open the file “WEBGPSSFile

Step 4 : To view created functions,
Control >> Functions

To View Start values of various variables used ,
Control >> Start values

Step 5 : Click on Run to run the program. (Run button present near           bottom left corner).
Select the tab “View Output” to see the output.

Select the tab “Graph1” (or any other graph tab) to see the graph. Graph (Stock vs time(in days)).

Step 6 : Save result of ‘Print Output’ in current directory.

Save with any file name in “build” directory.

Step 7 : Open the Command prompt in “build” directory and type

javac *.java    (to compile)

java Main       (to run)

or you can copy & paste the java program posted below:

```import java.io.File;
import java.io.FileWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;

/**
*
* @author VIKASH
*/
public class Muphoria {

Supplier[] supList;//holds supplier info
Scanner sc;//scans data from files
DayPlan[] dayPlans = new DayPlan[41];//holds plan for July month (more than 31 because order must be placed on last days of July for earlier days of august)...
float totalCost = 0;//holds total cost
int totalQuantity = 0;//holds total quantity
float totalUsedCost = 0;//holds used cost

public Muphoria() {
setUpSuppliers();//scan supplier details from file

//create objects of DayPlan
for (int i = 0; i < dayPlans.length; i++) {
dayPlans[i] = new DayPlan();

}
}

/**
* set up supplier details
*/
private void setUpSuppliers() {

supList = new Supplier[12];
try {
File sup = new File("supplierdetails.txt");
sc = new Scanner(sup);
} catch (Exception e) {
javax.swing.JOptionPane.showMessageDialog(null, e.getMessage());
System.exit(1);
}
int i = 7;
while (i > 0) {
sc.nextLine();
i--;
}
while (sc.hasNext()) {
String s[] = sc.nextLine().replaceAll("\"", "").replaceAll(",", "").trim().split("\t");
supList[i] = new Supplier();
supList[i].item = new Ingredient(s[0]);
supList[i].name = s[1];
supList[i].maxOrder = Integer.parseInt(s[3].trim());
supList[i].costPerUnit = Integer.parseInt(s[4].trim());
i++;
}

}

/**
* Calls various method to compute and generate output
*/
public void process() {
printDayPlans();//PRINT DAY PLAN OF THE MONTH
System.out.println("Output files generated in current directory!!!");
printSum();
}

/**
* Prints the total cost
*/
private void printSum() {
int total = 0;

for (int i = 0; i < 31; i++) {//procurement cost
total += dayPlans[i].getOrder_Potatoes() * dayPlans[i].getPrice_Potatoes()
+ dayPlans[i].getOrder_Chillies() * dayPlans[i].getPrice_Chillies()
+ dayPlans[i].getOrder_Garlic() * dayPlans[i].getPrice_Garlic()
+ dayPlans[i].getOrder_Oil() * dayPlans[i].getPrice_Oil();
}
int potatoes = 140000;
int chillies = 280000;
int garlic = 280000;
int oil = 14000;

for (int i = 0; i < 31; i++) {//holding cost

potatoes = dayPlans[i].getPotatoesStock();
chillies = dayPlans[i].getChilliesStock();
garlic = dayPlans[i].getGarlicStock();
oil = dayPlans[i].getOilStock();
total += potatoes * 3 + chillies * 5 / 2 + garlic * 8 / 5 + oil * 153 / 10;

}
for (int i = 0; i < 31; i++) {//processing cost
total += dayPlans[i].getProcessingCost();
}

System.out.println("Total Cost ( Procurement cost+Inventory storage cost+Wastage cost+Lost sales ) = " + total);
javax.swing.JOptionPane.showMessageDialog(null, "Total Cost (Procurement cost+Inventory storage cost+Wastage cost+Lost sales) = " + total);
}

/**
* Generate dayPlan for all the days of July month
*/
private void printDayPlans() {
FileWriter f = null, f1 = null;
try {
File file = new File("DayPlan.csv");
file.delete();
file = new File("DayPlan1.csv");
file.delete();

f = new FileWriter("DayPlan.csv", true);
f1 = new FileWriter("DayPlan1.csv", true);
String s1 = ",,Base Inventory(July) for : \n,,(a),Potatoes  ,140000\n,,(b),Chillies  ,280000\n,,(c),Garlic  ,280000\n,,(d),Oil  , 14000\n\n,Sales Forecast,,Potatoes,Chillies,Garlic,Oil\n,,,,(Order Summary)";
String s = ",,Base Inventory(July) for : \n,,(a),Potatoes  ,140000\n,,(b),Chillies  ,280000\n,,(c),Garlic  ,280000\n,,(d),Oil  , 14000\n\n30-June-2013,";
f.write(s);
f1.write(s1);
} catch (Exception e) {
javax.swing.JOptionPane.showMessageDialog(null, e.getMessage());
System.exit(1);
}

for (int i = 0; i < 31; i++) {
try {
f.flush();
f1.flush();
String s1 = "\nDay#" + (i + 1) + "," + dayPlans[i].getForecast() + "           ,,    " + dayPlans[i].getOrder_Potatoes() + "  ,     " + dayPlans[i].getOrder_Chillies() + "    ,  " + dayPlans[i].getOrder_Garlic() + "    ,     " + dayPlans[i].getOrder_Oil();
String s = "\"" + (i + 1) + "-July-2013\t\nSales Forecast:" + dayPlans[i].getForecast() + "\nOrder Summary\nPotatoes:" + dayPlans[i].getOrder_Potatoes() + "\nChillies:" + dayPlans[i].getOrder_Chillies() + "\nGarlic:" + dayPlans[i].getOrder_Garlic() + "\nOil:" + dayPlans[i].getOrder_Oil() + "\",";
if ((i + 2) % 7 == 0) {
s += "\n";
}
f.write(s);
f1.write(s1);
f.flush();
f1.flush();
//  System.out.println(s);
} catch (Exception e) {
javax.swing.JOptionPane.showMessageDialog(null, e.getMessage());
System.exit(1);
}
}
}

/**
* Read data from order.txt and compute frequency of orders of all the goods ordered
* Based on this it calls setUpDemand method to place order of the day for the respective good
*/
int freq1[][] = new int[3][15];
int freq2[][] = new int[3][15];
int freq3[][] = new int[3][15];
int freq4[][] = new int[3][15];
File f;
try {
f = new File("ordersummary.txt");
sc = new Scanner(f);

} catch (Exception e) {
javax.swing.JOptionPane.showMessageDialog(null, e.getMessage());
System.exit(1);
} finally {
}
int i = 1;
while (i <= 5) {
sc.nextLine();
i++;
}

i = 0;
while (sc.hasNext()) {

String[] s = sc.nextLine().trim().replaceAll("\"", "").replaceAll(",", "").split("\t");
if (s[0] == null || s[0].isEmpty() || s[0].equals("")) {
break;
}

freq1[i][Integer.parseInt(s[4].trim())] += 1;
freq2[i][Integer.parseInt(s[12].trim())] += 1;
if (i == 2) {
i = 0;
} else {
i++;
}

}
i = 0;
while (sc.hasNext()) {
String[] s = sc.nextLine().trim().replaceAll("\"", "").replaceAll(",", "").split("\t");
if (s[0] == null || s[0].isEmpty() || s[0].equals("")) {
continue;
}
try {
freq3[i][Integer.parseInt(s[4].trim())] += 1;
freq4[i][Integer.parseInt(s[12].trim())] += 1;

if (i == 2) {
i = 0;
} else {
i++;
}

} catch (Exception e) {
}

}

FileWriter out = null;
try {
file.delete();
for (i = 1; i <= 4; i++) {
switch (i) {
case 1:
//System.out.println("Frequency of lead time for suppliers of Potato:");
out.flush();
out.write("\nFrequency of lead time of suppliers of Potato:\n");
break;
case 2:
//System.out.println("Frequency of lead time for suppliers of Chilli");
out.flush();
out.write("\nFrequency of lead time of suppliers of Chilli:\n");
break;
case 3:
//System.out.println("Frequency of lead time for suppliers of Gralic:");
out.flush();
out.write("\nFrequency of lead time of suppliers of Garlic:\n");
break;
case 4:
//System.out.println("Frequency of lead time for suppliers of Oil:");
out.flush();
out.write("\nFrequency of lead time of suppliers of Oil:\n");
break;
}

}
System.out.print("Enter output file name of WEBGPSS saved in current directory (or press 1 to select already build file ) : ");

file = new File("order.csv");
file.delete();

out = new FileWriter("order.csv", true);
out.write("   ,Ordered On   ,\t   Ordered To \t  ,   Product   ,   Quantity   ,   Cost/Unit   ,   Received On   ,   Total Cost   ,   Used(Cost)   ,   Wasted(Cost)   ");
totalCost = 0;
totalQuantity = 0;
totalUsedCost = 0;
sc=new Scanner(System.in);
String in="";
try{
in=sc.nextLine().trim();
if(Integer.parseInt(in)==1)
processWEBGPSSOutData(null,out,true);
}catch(Exception e)
{ //e.printStackTrace();
processWEBGPSSOutData(in.trim(),out,false);
}

} catch (Exception e) {
//javax.swing.JOptionPane.showMessageDialog(null, e.getMessage());
e.printStackTrace();
System.exit(1);
}

try {
String sum = "   \nSum ,  ,,, " + totalQuantity + "   ,,    ,  " + BigInteger.valueOf((long) (totalCost)) + "   ,  " + BigInteger.valueOf((long) totalUsedCost) + " ," + BigInteger.valueOf((long) (totalCost - totalUsedCost));
out.write("" + sum);
out.flush();
out.close();
} catch (Exception e) {
javax.swing.JOptionPane.showMessageDialog(null, e.getMessage());
System.exit(1);
}
}

private void processWEBGPSSOutData(String in,FileWriter writer,boolean useDefault) throws Exception {
if(useDefault)
sc = new Scanner(new File("./result.out"));
else
sc = new Scanner(new File(in));
int day = 1;
String s = sc.nextLine().trim();

while (sc.hasNextLine()) {

if (s.indexOf("#") >= 0) {

s = sc.nextLine().trim();
//forecast,demand,lost sale
if (s.indexOf("Day") >= 0) {

day = Integer.parseInt(s.split(":")[1].trim());

s = sc.nextLine().trim();

dayPlans[day - 1].setForecast(Integer.parseInt(s.split(":")[1].trim()));

s = sc.nextLine().trim();

dayPlans[day - 1].setSale(Integer.parseInt(s.split(":")[1].trim()));
// s = sc.nextLine().trim();

// dayPlans[day - 1].setLostSale(Integer.parseInt(s.split(":")[1].trim()));

}
s = sc.nextLine().trim();

//for potato
if (s.indexOf("-") >= 0) {
String temp = "";

int cnt = 0;
while (true) {
s = sc.nextLine().trim();
if (s.indexOf("--") >= 0) {
break;
}
cnt++;
temp +=  s + "\n";

}
//System.out.println(day+" po "+temp);
switch (cnt) {
case 1:

dayPlans[day - 1].setPotatoesStock(Integer.parseInt(temp.trim().replaceAll("[^0-9]", "").trim()));

break;
case 2:
case 4:

String[] data = temp.trim().split("\n");
writer.flush();
String[] supDetails = data[0].trim().split(":");
for (int i = 0; i < data.length-1; i++) {
supDetails = data[i].trim().split(":");
Supplier supplier = supList[Integer.parseInt(supDetails[0].replaceAll("[a-zA-Z]", "").trim()) - 1];
int orderDay = ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim()));
dayPlans[orderDay - 1].setOrder_Potatoes(Integer.parseInt(supDetails[1].trim()));
dayPlans[orderDay - 1].setPrice_Potatoes(supplier.getCostPerUnit());
totalQuantity += Integer.parseInt(supDetails[1].trim());
int cost = (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim()));
totalCost += cost;
int usedCost = cost;
if (dayPlans[day - 1].getLostSale() >= 0) {
usedCost = cost - dayPlans[day - 1].getLostSale() * supplier.getCostPerUnit() / 3;
}
totalUsedCost += usedCost;
String write = "";
if (day <= 31) {

write = "\n," + orderDay + "," + supplier.getName() + ",Potatoes," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + "," + (day > 31 ? day - 31 : day) + "," + cost + "," + usedCost + "," + (cost - usedCost);
} else if ((Integer.parseInt(supDetails[3].trim()) <= 31)) {
write = "\n," + ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim())) + "," + supplier.getName() + ",Potatoes," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + ",to be received on (" + (day > 31 ? day - 31 : day) + ")," + (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim())) + ",---,";
}

writer.write(write);
}
dayPlans[day - 1].setPotatoesStock(Integer.parseInt(data[data.length - 1].split(":")[1].trim()));

break;
default:
data = temp.trim().split("\n");
writer.flush();
supDetails = data[0].trim().split(":");
for (int i = 0; i < data.length; i++) {
if (i == data.length-1) {
dayPlans[day - 1].setPotatoesStock(Integer.parseInt(data[i].split(":")[1].trim()));
continue;
}
if (i == 3) {
continue;
}

supDetails = data[i].trim().split(":");
Supplier supplier = supList[Integer.parseInt(supDetails[0].replaceAll("[a-zA-Z]", "").trim()) - 1];
int orderDay = ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim()));
dayPlans[orderDay - 1].setOrder_Potatoes(Integer.parseInt(supDetails[1].trim()));
dayPlans[orderDay - 1].setPrice_Potatoes(supplier.getCostPerUnit());
totalQuantity += Integer.parseInt(supDetails[1].trim());
int cost = (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim()));
totalCost += cost;
int usedCost = cost;
if (dayPlans[day - 1].getLostSale() >= 0) {
usedCost = cost - dayPlans[day - 1].getLostSale() * supplier.getCostPerUnit() / 3;
}
totalUsedCost += usedCost;
String write = "";
if (day <= 31) {

write = "\n," + orderDay + "," + supplier.getName() + ",Potatoes," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + "," + (day > 31 ? day - 31 : day) + "," + cost + "," + usedCost + "," + (cost - usedCost);
} else if ((Integer.parseInt(supDetails[3].trim()) <= 31)) {
write = "\n," + orderDay + "," + supplier.getName() + ",Potatoes," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + ",to be received on (" + (day > 31 ? day - 31 : day) + ")," + (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim())) + ",---,";
}
writer.write(write);
}
break;
}

}

//for chilli
if (s.indexOf("-") >= 0) {
String temp = "";
int cnt = 0;
while (true) {

s = sc.nextLine().trim();
if (s.indexOf("--") >= 0) {
break;
}
cnt++;

temp += s + "\n";

}
//System.out.println(day+" ch "+temp);

switch (cnt) {
case 1:

dayPlans[day - 1].setChilliesStock(Integer.parseInt(temp.trim().replaceAll("[^0-9]", "").trim()));

break;
case 2:
case 4:

String[] data = temp.trim().split("\n");
writer.flush();
String[] supDetails = data[0].trim().split(":");
for (int i = 0; i < data.length-1; i++) {
supDetails = data[i].trim().split(":");
Supplier supplier = supList[Integer.parseInt(supDetails[0].replaceAll("[a-zA-Z]", "").trim()) + 2];
int orderDay = ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim()));

dayPlans[orderDay - 1].setOrder_Chillies(Integer.parseInt(supDetails[1].trim()));
dayPlans[orderDay - 1].setPrice_Chillies(supplier.getCostPerUnit());
totalQuantity += Integer.parseInt(supDetails[1].trim());
int cost = (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim()));
totalCost += cost;
int usedCost = cost;
if (dayPlans[day - 1].getLostSale() >= 0) {
usedCost = cost - dayPlans[day - 1].getLostSale() * 2 * supplier.getCostPerUnit() / 3;
}
totalUsedCost += usedCost;
String write = "";
if (day <= 31) {

write = "\n," + orderDay + "," + supplier.getName() + ",Chillies," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + "," + (day > 31 ? day - 31 : day) + "," + cost + "," + usedCost + "," + (cost - usedCost);
} else if ((Integer.parseInt(supDetails[3].trim()) <= 31)) {
write = "\n," + ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim())) + "," + supplier.getName() + ",Chillies," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + ",to be received on (" + (day > 31 ? day - 31 : day) + ")," + (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim())) + ",---,";
}

writer.write(write);
}
dayPlans[day - 1].setChilliesStock(Integer.parseInt(data[data.length - 1].split(":")[1].trim()));

break;
case 5:
data = temp.trim().split("\n");
//System.out.println(data[0]);
writer.flush();
supDetails = data[0].trim().split(":");
dayPlans[day-1].setProcessingCost(Integer.parseInt(supDetails[1].trim())*875/100);
for (int i = 3; i < data.length-1; i++) {
supDetails = data[i].trim().split(":");
Supplier supplier = supList[Integer.parseInt(supDetails[0].replaceAll("[a-zA-Z]", "").trim()) + 2];
int orderDay = ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim()));

dayPlans[orderDay - 1].setOrder_Chillies(Integer.parseInt(supDetails[1].trim()));
dayPlans[orderDay - 1].setPrice_Chillies(supplier.getCostPerUnit());
totalQuantity += Integer.parseInt(supDetails[1].trim());
int cost = (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim()));
totalCost += cost;
int usedCost = cost;
if (dayPlans[day - 1].getLostSale() >= 0) {
usedCost = cost - dayPlans[day - 1].getLostSale() * 2 * supplier.getCostPerUnit() / 3;
}
totalUsedCost += usedCost;
String write = "";
if (day <= 31) {

write = "\n," + orderDay + "," + supplier.getName() + ",Chillies," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + "," + (day > 31 ? day - 31 : day) + "," + cost + "," + usedCost + "," + (cost - usedCost);
} else if ((Integer.parseInt(supDetails[3].trim()) <= 31)) {
write = "\n," + ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim())) + "," + supplier.getName() + ",Chillies," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + ",to be received on (" + (day > 31 ? day - 31 : day) + ")," + (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim())) + ",---,";
}

writer.write(write);
}
dayPlans[day - 1].setChilliesStock(Integer.parseInt(data[data.length - 1].split(":")[1].trim()));

break;
default:
data = temp.trim().split("\n");
//System.out.println(data[0]);

writer.flush();
supDetails = data[0].trim().split(":");
for (int i = 0; i < data.length; i++) {
if (i == data.length-1) {
dayPlans[day - 1].setChilliesStock(Integer.parseInt(data[i].split(":")[1].trim()));
continue;
}
if (i == 3) {
continue;
}
supDetails = data[i].trim().split(":");

Supplier supplier = supList[Integer.parseInt(supDetails[0].replaceAll("[a-zA-Z]", "").trim()) + 2];
int orderDay = ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim()));

dayPlans[orderDay - 1].setOrder_Chillies(Integer.parseInt(supDetails[1].trim()));
dayPlans[orderDay - 1].setPrice_Chillies(supplier.getCostPerUnit());
totalQuantity += Integer.parseInt(supDetails[1].trim());
int cost = (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim()));
totalCost += cost;
int usedCost = cost;
if (dayPlans[day - 1].getLostSale() >= 0) {
usedCost = cost - dayPlans[day - 1].getLostSale() * 2 * supplier.getCostPerUnit() / 3;
}
totalUsedCost += usedCost;
String write = "";
if (day <= 31) {

write = "\n," + ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim())) + "," + supplier.getName() + ",Chillies," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + "," + (day > 31 ? day - 31 : day) + "," + cost + "," + usedCost + "," + (cost - usedCost);
} else if ((Integer.parseInt(supDetails[3].trim()) <= 31)) {
write = "\n," + ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim())) + "," + supplier.getName() + ",Chillies," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + ",to be received on (" + (day > 31 ? day - 31 : day) + ")," + (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim())) + ",---,";
}
writer.write(write);
}

break;
}
}
//System.out.println(day+" "+s);
//for garlic
if (s.indexOf("-") >= 0) {
String temp = "";
int cnt = 0;
while (true) {

s = sc.nextLine().trim();
if (s.indexOf("--") >= 0) {
break;
}
cnt++;
temp += s + "\n";

}
//System.out.println(day+" "+temp);
switch (cnt) {
case 1:

dayPlans[day - 1].setGarlicStock(Integer.parseInt(temp.trim().replaceAll("[^0-9]", "").trim()));
break;
case 2:
case 4:
String[] data = temp.trim().split("\n");
writer.flush();
String[] supDetails = data[0].trim().split(":");
for (int i = 0; i < data.length-1; i++) {
supDetails = data[i].trim().split(":");
Supplier supplier = supList[Integer.parseInt(supDetails[0].replaceAll("[a-zA-Z]", "").trim()) + 5];
int orderDay = ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim()) > 0 ? Integer.parseInt(supDetails[3].trim()) : 1);
dayPlans[orderDay - 1].setOrder_Garlic(Integer.parseInt(supDetails[1].trim()));
dayPlans[orderDay - 1].setPrice_Garlic(supplier.getCostPerUnit());

totalQuantity += Integer.parseInt(supDetails[1].trim());
int cost = (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim()));
totalCost += cost;
int usedCost = cost;
if (dayPlans[day - 1].getLostSale() >= 0) {
usedCost = cost - dayPlans[day - 1].getLostSale() * 2 * supplier.getCostPerUnit() / 3;
}
totalUsedCost += usedCost;

String write = "";
if (day <= 31) {

write = "\n," + orderDay + "," + supplier.getName() + ",Garlic," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + "," + (day > 31 ? day - 31 : day) + "," + cost + "," + usedCost + "," + (cost - usedCost);
} else if ((Integer.parseInt(supDetails[3].trim()) <= 31)) {
write = "\n," + ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim())) + "," + supplier.getName() + ",Garlic," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + ",to be received on (" + (day > 31 ? day - 31 : day) + ")," + (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim())) + ",---,";
}

writer.write(write);
}
dayPlans[day - 1].setGarlicStock(Integer.parseInt(data[data.length - 1].split(":")[1].trim()));

break;
default:
data = temp.trim().split("\n");

writer.flush();
supDetails = data[0].trim().split(":");
for (int i = 0; i < data.length; i++) {
if (i == data.length-1) {
dayPlans[day - 1].setGarlicStock(Integer.parseInt(data[i].split(":")[1].trim()));
continue;
}
if (i == 3) {
continue;
}

supDetails = data[i].trim().split(":");
int orderDay = ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim()) > 0 ? Integer.parseInt(supDetails[3].trim()) : 1);
Supplier supplier = supList[Integer.parseInt(supDetails[0].replaceAll("[a-zA-Z]", "").trim()) + 5];
dayPlans[orderDay - 1].setOrder_Garlic(Integer.parseInt(supDetails[1].trim()));
dayPlans[orderDay - 1].setPrice_Garlic(supplier.getCostPerUnit());
totalQuantity += Integer.parseInt(supDetails[1].trim());
int cost = (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim()));
totalCost += cost;
int usedCost = cost;
if (dayPlans[day - 1].getLostSale() >= 0) {
usedCost = cost - dayPlans[day - 1].getLostSale() * 2 * supplier.getCostPerUnit() / 3;
}
totalUsedCost += usedCost;

String write = "";
if (day <= 31) {
write = "\n," + ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim())) + "," + supplier.getName() + ",Chillies," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + "," + (day > 31 ? day - 31 : day) + "," + cost + "," + usedCost + "," + (cost - usedCost);
} else if ((Integer.parseInt(supDetails[3].trim()) <= 31)) {
write = "\n," + ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim())) + "," + supplier.getName() + ",Chillies," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + ",to be received on (" + (day > 31 ? day - 31 : day) + ")," + (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim())) + ",---,";
}
writer.write(write);
}

break;
}
}

//for oil
if (s.indexOf("-") >= 0) {
String temp = "";
int cnt = 0;
while (true) {

s = sc.nextLine().trim();
if (s.indexOf("#") >= 0) {
break;
}
cnt++;
temp += s + "\n";
if (!(sc.hasNextLine())) {
break;
}

}

switch (cnt) {
case 1:

dayPlans[day - 1].setOilStock(Integer.parseInt(temp.trim().replaceAll("[^0-9]", "").trim()));
break;
case 2:
case 4:
String[] data = temp.trim().split("\n");
writer.flush();
String[] supDetails = data[0].trim().split(":");
for (int i = 0; i < data.length-1; i++) {
supDetails = data[i].trim().split(":");
Supplier supplier = supList[Integer.parseInt(supDetails[0].replaceAll("[a-zA-Z]", "").trim()) + 8];
int orderDay = ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim()));
dayPlans[orderDay - 1].setOrder_Oil(Integer.parseInt(supDetails[1].trim()));
dayPlans[orderDay - 1].setPrice_Oil(supplier.getCostPerUnit());
totalQuantity += Integer.parseInt(supDetails[1].trim());
int cost = (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim()));
totalCost += cost;
int usedCost = cost;
if (dayPlans[day - 1].getLostSale() >= 0) {
usedCost = cost - dayPlans[day - 1].getLostSale() * supplier.getCostPerUnit() / 30;
}
totalUsedCost += usedCost;
String write = "";
if (day <= 31) {

write = "\n," + orderDay + "," + supplier.getName() + ",Oil," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + "," + (day > 31 ? day - 31 : day) + "," + cost + "," + usedCost + "," + (cost - usedCost);
} else if ((Integer.parseInt(supDetails[3].trim()) <= 31)) {
write = "\n," + ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim())) + "," + supplier.getName() + ",Oil," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + ",to be received on (" + (day > 31 ? day - 31 : day) + ")," + (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim())) + ",---,";
}

writer.write(write);
}
dayPlans[day - 1].setOilStock(Integer.parseInt(data[data.length - 1].split(":")[1].trim()));

break;
default:

data = temp.trim().split("\n");
writer.flush();
supDetails = data[0].trim().split(":");
for (int i = 0; i < data.length; i++) {
if (i == data.length-1) {
dayPlans[day - 1].setOilStock(Integer.parseInt(data[i].split(":")[1].trim()));
continue;
}
if (i == 3) {
continue;
}

supDetails = data[i].trim().split(":");
int orderDay = ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim()));

Supplier supplier = supList[Integer.parseInt(supDetails[0].replaceAll("[a-zA-Z]", "").trim()) + 8];
dayPlans[orderDay - 1].setOrder_Oil(Integer.parseInt(supDetails[1].trim()));
dayPlans[orderDay - 1].setPrice_Oil(supplier.getCostPerUnit());
totalQuantity += Integer.parseInt(supDetails[1].trim());
int cost = (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim()));
totalCost += cost;
int usedCost = cost;
if (dayPlans[day - 1].getLostSale() >= 0) {
usedCost = cost - dayPlans[day - 1].getLostSale() * supplier.getCostPerUnit() / 30;
}
totalUsedCost += usedCost;

String write = "";
if (day <= 31) {

write = "\n," + ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim())) + "," + supplier.getName() + ",Chillies," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + "," + (day > 31 ? day - 31 : day) + "," + cost + "," + usedCost + "," + (cost - usedCost);
} else if ((Integer.parseInt(supDetails[3].trim()) <= 31)) {
write = "\n," + ((Integer.parseInt(supDetails[3].trim())) > 31 ? Integer.parseInt(supDetails[3].trim()) - 31 : Integer.parseInt(supDetails[3].trim())) + "," + supplier.getName() + ",Chillies," + supDetails[1].trim() + "," + supplier.getCostPerUnit() + ",to be received on (" + (day > 31 ? day - 31 : day) + ")," + (supplier.getCostPerUnit() * Integer.parseInt(supDetails[1].trim())) + ",---,";
}
writer.write(write);
}

break;
}
}

}

}
}

private void printLeadFrequency(int[][] freq, FileWriter out) throws Exception {
for (int i = 0; i < freq.length; i++) {
String s = "\nSupplier" + (i + 1) + "\nlead  frequency  \n";

out.flush();
out.write(s);

for (int j = 0; j < freq[i].length; j++) {
if (freq[i][j] != 0) {
s = "\n  " + (j + 1) + "    " + freq[i][j];
out.flush();
out.write(s);

}
}
}
}

}

/**
*
* @author VIK
* This class have various properties for the plan of the day.
* Also it has several getter and setter methods to get and set the values of these properties.
*/
class DayPlan {

private int forecast;//sales forecast of the day
private int sale;//total sale of the day
private int order_Potatoes;//order of potatoes
private int order_Chillies;//order of chillies
private int order_Garlic;//order of garlic
private int order_Oil;//order of oil
private int lostSale;//lost sale
private float costIncurred;//total cost incurred
private int holdingCost;//holding cost
private int processingCost;//processing cost
private int price_Potatoes;//price of potatoes/unit
private int price_Chillies;//price of chillies/unit
private int price_Garlic;//price of garlic/unit
private int price_Oil;//price of oil/unit
private int potatoesStock;
private int chilliesStock;
private int garlicStock;
private int oilStock;

public int getChilliesStock() {
return chilliesStock;
}

public void setChilliesStock(int chilliesStock) {
this.chilliesStock = chilliesStock;
}

public int getGarlicStock() {
return garlicStock;
}

public void setGarlicStock(int garlicStock) {
this.garlicStock = garlicStock;
}

public int getOilStock() {
return oilStock;
}

public void setOilStock(int oilStock) {
this.oilStock = oilStock;
}

public int getPotatoesStock() {
return potatoesStock;
}

public void setPotatoesStock(int potatoesStock) {
this.potatoesStock = potatoesStock;
}

}

}

}

}

}

}

}

}

public int getPrice_Chillies() {
return price_Chillies;
}

public void setPrice_Chillies(int price_Chillies) {
this.price_Chillies = price_Chillies;
}

public int getPrice_Garlic() {
return price_Garlic;
}

public void setPrice_Garlic(int price_Garlic) {
this.price_Garlic = price_Garlic;
}

public int getPrice_Oil() {
return price_Oil;
}

public void setPrice_Oil(int price_Oil) {
this.price_Oil = price_Oil;
}

public int getPrice_Potatoes() {
return price_Potatoes;
}

public void setPrice_Potatoes(int price_Potatoes) {
this.price_Potatoes = price_Potatoes;
}

public int getLostSale() {
return lostSale;
}

public void setLostSale(int lostSale) {
this.lostSale = lostSale;
}

public int getHoldingCost() {
return holdingCost;
}

public void setHoldingCost(int holdingCost) {
this.holdingCost += holdingCost;
}

public int getProcessingCost() {
return processingCost;
}

public void setProcessingCost(int processingCost) {
this.processingCost += processingCost;
}

public float getCostIncurred() {
return costIncurred;
}

public void setCostIncurred() {
costIncurred = getLostSale() * 100 + getOrder_Potatoes() * getPrice_Potatoes() + getOrder_Chillies() * getPrice_Chillies() + getOrder_Garlic() * getPrice_Garlic() + getOrder_Oil() * getPrice_Oil();
}

public int getForecast() {
return forecast;
}

public void setForecast(int forecast) {
this.forecast = forecast;
}

public int getOrder_Chillies() {
return order_Chillies;
}

public void setOrder_Chillies(int order_Chillies) {
this.order_Chillies += order_Chillies;
}

public int getOrder_Garlic() {
return order_Garlic;
}

public void setOrder_Garlic(int order_Garlic) {
this.order_Garlic += order_Garlic;
}

public int getOrder_Oil() {
return order_Oil;
}

public void setOrder_Oil(int order_Oil) {
this.order_Oil += order_Oil;
}

public int getOrder_Potatoes() {
return order_Potatoes;
}

public void setOrder_Potatoes(int order_Potatoes) {
this.order_Potatoes += order_Potatoes;
}

public int getSale() {
return sale;
}

public void setSale(int sale) {
this.sale = sale;
}
}

/**
*
* @author VIK
* This class has several properties of a supplier.
* The class also contains setter and getter methods of the properties.
*
*/
class Supplier {

String name;
Ingredient item;
int maxOrder;
int costPerUnit;

public void setCostPerUnit(int costPerUnit) {
this.costPerUnit = costPerUnit;
}

public void setItem(Ingredient item) {
this.item = item;
}

}

public void setMaxOrder(int maxOrder) {
this.maxOrder = maxOrder;
}

public void setName(String name) {
this.name = name;
}

public int getCostPerUnit() {
return costPerUnit;
}

public Ingredient getItem() {
return item;
}

}

public int getMaxOrder() {
return maxOrder;
}

public String getName() {
return name;
}
}

/**
*
* @author VIK
*
* This class contains basic properties of an ingredient. It also contains setter and getter methods for these properties.
*/
class Ingredient {

String name;
float quantity;
float holdingCost;
float processingPrice;
int days;
float costIncurred = 0;

public Ingredient(String name) {
this.name = name;
Cost cost = new Cost(name);
setHoldingCost(cost.getHoldingCost());
setProcessingPrice(cost.getProcessingCost());

}

public float getCostIncurred() {
return costIncurred;
}

public void setCostIncurred(float costIncurred) {
this.costIncurred += costIncurred;
}

public int getDays() {
return days;
}

public void setDays() {
if (name.equals("Potatoes")) {
days = 14;
} else if (name.equals("Green Chillies")) {
days = 7;
} else if (name.equals("Garlic")) {
days = 21;
} else if (name.equals("Oil")) {
days = 56;
}
}

public float getHoldingCost() {
return holdingCost;
}

public void setHoldingCost(float holdingCost) {
this.holdingCost = holdingCost;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public float getProcessingPrice() {
return processingPrice;
}

public void setProcessingPrice(float processingPrice) {
this.processingPrice = processingPrice;
}

public float getQuantity() {
return quantity;
}

public void setQuantity(float quantity) {
this.quantity = quantity;
}

public void reduceDays() {
days -= 1;

}

public void reduceQuantity(float quantity) {
this.quantity -= quantity;
}
}

/**
*
* @author VIK
*
* This class holds various cost information and java bean methods to set and get the values of these properties.
*/
class Cost {

String name;//holds name of the item
float holdingCost;//holds holding cost
float processingCost;//holds processing cost

public Cost(String name) {
this.name = name;
}

public float getHoldingCost() {
return holdingCost;
}

public void setHoldingCost() {

if (name.equals("Potatoes")) {
holdingCost = 3;
} else if (name.equals("Green Chillies")) {
holdingCost = 2.5f;
} else if (name.equals("Garlic")) {
holdingCost = 1.6f;
} else if (name.equals("Oil")) {
holdingCost = 15.3f;
}

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public float getProcessingCost() {
return processingCost;
}

public void setProcessingCost() {

if (name.equals("Potatoes")) {
processingCost = 10.0f;
} else if (name.equals("Green Chillies")) {
processingCost = 8.75f;
} else if (name.equals("Garlic")) {
processingCost = 6;
} else if (name.equals("Oil")) {
processingCost = 51;
}

}
}

```
```/**
*
* @author VIKASH
* Execution Logic class
*/

public class Main
{
public static void main(String[] srgs)
{
Muphoria muphoria =new Muphoria();
muphoria.process();
}
}
```

Output will be generated in DayPlan.csv or DayPlan1.csv and order will be generated in order.csv file. Open .csv files in Microsoft excel for better visibility.

Note 1: DayPlan1.csv and DayPlan.csv are having same data, only representation is different for better understandability.

Note 2:  While running the application, make sure that existing files are  not opened in any application.

Do you have any other ideas. Do share through your comment below!