- Samuel025
- 文章數 : 4
積分 : 12
注冊日期 : 2020-08-18
Question 5: (10 marks) The owner of a photocopy store charges 5 cents per copy. In addition there is a setup fee for 2.5$ for each photocopying job. The store revenue from selling x copies is: revenue = 0.05x + 2.5 If it costs the owner 3 cents per copy, write a Java program that reads a text file which contains the sales of each day during a period of time then determines the total revenue, cost and profit over this period of time. (Profit = revenue-cost). (Put your Java code inside a try-catch block and don't forget to import the required library).
- Christopher N.Contributor
- 文章數 : 16
積分 : 18
注冊日期 : 2020-05-01
- 代碼:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.DecimalFormat;
public class photocopy {
public static void main(String[] args){
// start try catch block
try {
// DecimalFormat class for rounding up to 2 places of decimla
final DecimalFormat df = new DecimalFormat("#.##");
// total revenue and total cost variable
double totalRevenue = 0.0;
double totalCost = 0.0;
// totalNumberOfPhotocopies variable to store the total number of photocopies
int totalNumberOfPhotocopies = 0;
// open the file sales.txt
File file = new File("sales.txt");
// reading fie sales.txt line by line
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
String line = "";
while((line=br.readLine())!=null){
// check if line is empty or not
if(line.length()!=0){
// parsing the raed value to integer
int x = Integer.parseInt(line);
// calculating revenue from sales
double revenue = 0.05 * x + 2.5;
// adding revenue to total revenue
totalRevenue = totalRevenue + revenue;
// adding x to total totalNumberOfPhotocopies
totalNumberOfPhotocopies = totalNumberOfPhotocopies + x;
}
}
br.close();
// calculating total cost
totalCost = totalNumberOfPhotocopies * 0.03;
// calculating profit
double profit = totalRevenue - totalCost;
// displaying the profit
System.out.println("Profit: $"+Double.valueOf(df.format(profit)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
這個論壇的權限:
您 無法 在這個版面回復文章