Write a program that reads a text file called \"sales.txt\". The file contains w
ID: 3931855 • Letter: W
Question
Write a program that reads a text file called "sales.txt". The file contains weekly sales information from a supermarket. Every line in the file contains three integers representing an item code (a number between 1 and 1000), the number of items sold during a week, and the total number of items originally in stock. For example, if a line in the file contains the following data: 1234 100 500, this means that 100 units of item number 1234 were sold from an original stock of 500 units. Your program should analyze the data stored in the file and generate a new file called "orders.trt". This file should contain the item codes and the number of items that must be ordered to replenish the stock. An item should be ordered if the remaining number of units in stock is lessthanorequalto 30% of the original. Each line of the generated file should include the code of the item that must be ordered along with the number of units that needs to be ordered to replenish the original stock. For example, if a line in the output file contains the following data: 5678 250, this means that 250 units of item number 5678 should be ordered.Explanation / Answer
Please follow the code and comments for description :
CODE :
import java.io.BufferedReader; // required imports for the code
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
public class salesDataExample { // class to run the code
public static void main(String[] args) throws IOException { // driver method that throws any exceptions
File outFile = new File("orders.txt"); // create a object for the output file
if (!outFile.exists()) { // check for the existence and create one
outFile.createNewFile();
}
FileWriter fw = new FileWriter(outFile.getAbsoluteFile()); // get the path of the file
BufferedWriter bw = new BufferedWriter(fw); // create a writer object
String currentLine; // local variables
BufferedReader br = new BufferedReader(new FileReader("sales.txt")); // reader object for the input file
while ((currentLine = br.readLine()) != null) { // check for null
StringTokenizer st = new StringTokenizer(currentLine, " "); // token the data read based on the delimiter
String itemCode = st.nextToken(); // get the tokens saved
int itemSold = Integer.parseInt(st.nextToken()); // conver the data to int
int totalItems = Integer.parseInt(st.nextToken());
int remItems = totalItems - itemSold; // check for the left over items
int leastItems = (int) ((int) totalItems * 0.3); // calculate the percent of the total items
if (remItems <= leastItems) { // check for the items count
bw.write(itemCode + " " + Integer.toString(totalItems - remItems)); // write the data to file the item code and the items needed to retain
bw.write(" "); // new line character
}
}
bw.close(); // closing the reader and the writers
br.close();
}
}
sales.txt :
234 100 500
856 280 280
657 64 300
741 2 600
124 582 600
orders.txt :
856 280
124 582
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.