Help!!!! Need ASAP in java please Brewster’s Used Car, Inc. employs several sale
ID: 3762362 • Letter: H
Question
Help!!!! Need ASAP in java please
Brewster’s Used Car, Inc. employs several salespeople. Brewster, the owner of the company, has provided a file named sales.txt that contains the sales records for each salesperson for the past month. Each record in the file contains the following two fields:
The salesperson’s ID number, as an integer
The amount of a sale, as a real number
The records are already sorted by salesperson ID. Design a program that prints a sales report. The report should show each salesperson’s sales and the total sales for that salesperson. The report should also show the total sales for all salespeople for the month. Here is an example of how the sales report should appear:
Brewster’s Used Cars, Inc.
Sales Report
Salesperson ID Sale Amount
======================================...
100 ==== $10,000.00
100 ==== $12,000.00
100 ==== $5,000.00
Total sales for this salesperson : $27,000.00
101 ==== $14,000.00
101 ==== $18,000.00
101 ==== $12,500.00
Total sales for this salesperson : $44,500.00
102 ==== $13,500.00
102 ==== $14,500.00
102 ==== $20,000.00
Total sales for this salesperson : $48,000.00
Total of all sales: $119,500.00.
Explanation / Answer
public class SalesReport {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
File text = new File("sales.txt");
// Creating Scanner instnace to read File in Java
Scanner scnr = new Scanner(text);
// Reading each line of file using Scanner class
int lineNumber = 1;
int tempEmpId = 0;
double empTotalSale = 0.0, totalMonthSale = 0.0;
System.out.println("Salesperson ID Sale Amount");
while (scnr.hasNextLine()) {
String line = scnr.nextLine();
String lineArr[] = line.split(" ");
int empId = Integer.parseInt(lineArr[0]);
double saleVal = Double.parseDouble(lineArr[1]);
if (tempEmpId == 0) {
tempEmpId = empId;
empTotalSale += saleVal;
} else if (tempEmpId == empId) {
empTotalSale += saleVal;
} else if (tempEmpId != empId) {
tempEmpId = empId;
//
System.out.println("Total sales for this salesperson : $"
+ empTotalSale);
empTotalSale = 0;
empTotalSale += saleVal;
} else {
empTotalSale += saleVal;
System.out.println("Total sales for this salesperson : $"
+ empTotalSale);
empTotalSale += saleVal;
}
System.out.println(empId + " ==== " + saleVal);
totalMonthSale += saleVal;
lineNumber++;
}
System.out.println("Total sales for this salesperson : $"
+ empTotalSale);
System.out.println("Total of all sales: $" + totalMonthSale);
}
}
sales.txt:
100 10000.00
100 12000.00
100 5000.00
101 14000.00
101 18000.00
101 12500.00
102 13500.00
102 14500.00
102 20000.00
output:
Salesperson ID Sale Amount
100 ==== 10000.0
100 ==== 12000.0
100 ==== 5000.0
Total sales for this salesperson : $27000.0
101 ==== 14000.0
101 ==== 18000.0
101 ==== 12500.0
Total sales for this salesperson : $44500.0
102 ==== 13500.0
102 ==== 14500.0
102 ==== 20000.0
Total sales for this salesperson : $48000.0
Total of all sales: $119500.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.