Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on writing a program that reads inventory items from a file and adds a markup charge. I am assuming each line will be correct and not using any validation. However, I am building the program on the format of each line HOPEFULLY being: name quantity cost markup.
The cost is the wholesale cost the company paid for the item(per each). The markup is a number between 1 and 100 which indicates the percentage of the markup to determine the retail cost. I'm trying to Output all input data to a file, along with the price, the value of the items wholesale (quantity * cost), the value of the items retail (quantity * price). All currencies should be rounded to two decimals on output (printf). The file should be nicely formatted in tabular form using printf statements for output, and include a title row.
I am struggling to figure out how I can make the output file display hundredths for all of the inputs, excluding the Quantity row. Secondly, I can't really round the 'price (R)` being that it throws off the `Value (R)` and right now it is correct according to windows calculator & my math friend who looked at it but doesn't know the code.
{MY CODE}
------------
Java
package realplayground;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Pleasegod {
public static void main(String[] args) throws FileNotFoundException {
int Quantity=0, countofQ=0, countofLines=0, count=0;
double Cost=0, Markup, FinCost=0, TotalWValue=0, TotalRValue=0, MarkPerct=0, MarkAdd=0,Value=0;
String Line, item1, item2, item3, item4;
String[] Sect;
Scanner inFile;
PrintWriter outFile;
System.out.println("(Basic) Inventory Program Starting. Opening file...");
System.out.println("Please ensure data is entered per line in the format of: 'Name Quantity Cost Markup' ");
inFile = new Scanner(new File("input.txt"));
outFile = new PrintWriter(new File("output.txt"));
outFile.printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s%n",
"Item", "Quantity", "Cost", "Markup %", "Price(R)", "Value (W)", "Value (R)");
outFile.println
("---------------------------------------------------------------------------------------------------------------");
while(inFile.hasNext()){
Line = inFile.nextLine();
Sect = Line.split(" ");
count++;
if(Sect.length == 4) {
item1 = Sect[0];
item2 = Sect[1];
item3 = Sect[2];
item4 = Sect[3];
Quantity = Integer.parseInt(item2);
Cost = Double.parseDouble(item3);
Markup = Double.parseDouble(item4);
Value = Quantity * Cost;
MarkPerct = Markup/100;
FinCost = CalculateRetail(Cost, Markup, MarkPerct, MarkAdd, FinCost);
double ValueMarkup = Quantity * FinCost;
ValueMarkup = Math.round(ValueMarkup * 100.0) / 100.0;
countofQ += Quantity;
countofLines = count;
TotalWValue += Value;
TotalRValue += ValueMarkup;
outFile.printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s%n", Sect[0], Sect[1], Cost, Markup, FinCost, Value, ValueMarkup);
}
else {
outFile.println("Line did not have 4 item(s)");
}
}
outFile.close();
inFile.close();
System.out.println("");
System.out.printf("Total Wholesale-Cost of all items: %1.2f", TotalWValue);
System.out.println("");
System.out.printf("Total Retail-Cost of all items: %1.2f", TotalRValue);
System.out.println("");
System.out.println("Total # of data lines: " + countofLines);
System.out.println("Total # of items: " + countofQ);
}
public static double CalculateRetail(double Cost, double Markup, double MarkPerct, double MarkAdd, double FinCost){
MarkAdd = MarkPerct * Cost;
return FinCost = MarkAdd + Cost;
}
}


{INPUT FILE}
------------
thingy 22 15.65 45.00
Whatcha 59 67.99 25.5
gizmo 10 100.00 40.00

error
trythis 10 92.9999999999999999999999999999999999 10

{OUTPUT FILE}
---------------------
Item Quantity Cost Markup % Price(R) Value (W) Value (R)
---------------------------------------------------------------------------------------------------------------
thingy 22 15.65 45.0 22.692500000000003344.3 499.24
Whatcha 59 67.99 25.5 85.32745 4011.41 5034.32
gizmo 10 100.0 40.0 140.0 1000.0 1400.0
The line did not have 4 item(s)
trythis 10 93.0 10.0 102.3 930.0 1023.0

What I have tried:

I have tried rounding `FinCost` but this throws off my Value (R) which is currently correct on the {OUTPUT FILE} posted. Adding decimal format but I hit the same issue with Value (R). I in a sense am just trying to mask the number 22.692500000000003 making it 22.69 but still use the whole number in my calculations? Also, I had any old teacher look at this and he told me that my Value (R) was correct but didn't really have time to help me debug. Thank you.
Posted
Updated 14-Mar-19 16:28pm
v2
Comments
Richard MacCutchan 12-Dec-18 4:51am    
Do not use Double types for financial calculations. Convert all values to the lowest denomination (cent, pies etc.), or use a Decimal type if available.

1 solution

Try using %-10.2f instead of %-10s for relevant number values in outFile.printf().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900