Sales Analysis Java Program Description: A Java program is to be created to do a
ID: 3559812 • Letter: S
Question
Sales Analysis Java
Program Description: A Java program is to be created to do a monthly sales analysis for the Company.
To process the program, the programmer should read the file of Monthly sales amounts (Monthly.txt). It will be best to store this data in parallel arrays to ensure quick processing of the analysis. The programmer should produce a report giving the following statistics:
? The total sales amount for the month
? The average sales amount per salesman for the month
? The lowest sales amount for the month along with that salesman
Explanation / Answer
Program Code:
//Header Files
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
//SalesPerson class
class SalesPerson
{
String fname,lname;
double salesAmount;
//setfName initializes the fname (firstName)
public void setfName(String fname)
{
this.fname = fname;
}
//setlName initializes the lname (lastName)
public void setlName(String lname)
{
this.lname = lname;
}
//getName returns the full name
public String getName()
{
return fname+" "+lname;
}
//setsalesAmount() initializes the salesAmount
public void setsalesAmount(double salesAmount)
{
this.salesAmount = salesAmount;
}
//getsalesAmount returns the salesAmount
public double getsalesAmount()
{
return salesAmount;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------
class Sales
{
File file = new File("monthly.txt");
String fname,lname;
double salesAmount;
ArrayList<SalesPerson> al = new ArrayList<SalesPerson>();
//addSalesInfo adds the each salesperson to the ArrayList
public void addSalesInfo()
{
try
{
//reading file through Scanner
Scanner sc = new Scanner(file);
//if file has next line
while (sc.hasNextLine())
{
//read first word of the line
fname = sc.next();
//read next word of the line
lname = sc.next();
//read the data i.e., Amount
salesAmount = sc.nextDouble();
//create an object to SalesPerson
SalesPerson sp = new SalesPerson();
//invoke set methods
sp.setfName(fname);
sp.setlName(lname);
sp.setsalesAmount(salesAmount);
//add object to ArrayList
al.add(sp);
}
sc.close();
}
catch (FileNotFoundException e)
{
//if no file found
System.out.println("No File ");
e.printStackTrace();
}
}
//getTotalSalesAmount() method prints the total amount
public void getTotalSalesAmount()
{
double sum = 0;
//for each object in the ArrayList
for(int i = 0;i < al.size(); i ++)
{
//add amount to sum
sum += al.get(i).getsalesAmount();
}
//prompt the result
System.out.println(" The Total Sales Amount is : "+sum);
}
//getTotalSalesAmount() method prints the Average amount
public double getAverageSalesAmount()
{
double sum = 0;
//for each object in the ArrayList
for(int i = 0;i < al.size(); i ++)
{
//add amount to sum
sum += al.get(i).getsalesAmount();
}
//find average by dividing sum with size of ArrayList
double avg = sum / al.size();
return avg;
}
//geLowestSalesAmountDetails() method prints the Amount and name of sales person
//who has collected least amount
public void getLowestSalesAmountDetails()
{
double min = al.get(0).getsalesAmount();
int index = 0;
//for each object in the ArrayList
for(int i = 1;i < al.size(); i ++)
{
//if min is greater
if(min > al.get(i).getsalesAmount())
{
//change the value of min
min = al.get(i).getsalesAmount();
//store the value of i
index = i;
}
}
System.out.println(" The Minimum Sales Amount is "+al.get(index).getsalesAmount());
System.out.println(" The Minimum Sales Amount is made by Mr."+al.get(index).getName());
}
//geHighestSalesAmountDetails() method prints the Amount and name of sales person
//who has collected highest amount
public void getHighestSalesAmountDetails()
{
double max = al.get(0).getsalesAmount();
int index = 0;
//for each object in the ArrayList
for(int i = 1;i < al.size(); i ++)
{
//if max is lesser
if(max < al.get(i).getsalesAmount())
{
//change the value of max
max = al.get(i).getsalesAmount();
//store the value of i
index = i;
}
}
System.out.println(" The Maximum Sales Amount is "+al.get(index).getsalesAmount());
System.out.println(" The Maximum Sales Amount is made by Mr."+al.get(index).getName());
}
//getAboveAverage() method prints the Amount and name of sales persons
//who collected more than average amount
public void getAboveAverage()
{
double avg = getAverageSalesAmount();
//for each object in the ArrayList
for(int i = 0;i < al.size(); i ++)
{
//if salesAmount is greater than averageSalesAmount
if(al.get(i).getsalesAmount() > avg)
{
System.out.println(" The Minimum Sales Amount is "+al.get(i).getsalesAmount());
System.out.println(" The Minimum Sales Amount is made by Mr."+al.get(i).getName());
}
}
}
//getBelowAverage() method prints the Amount and name of sales persons
//who collected less than average amount
public void getBelowAverage()
{
double avg = getAverageSalesAmount();
//for each object in the ArrayList
for(int i = 0;i < al.size(); i ++)
{
//if salesAmount is lesser than averageSalesAmount
if(al.get(i).getsalesAmount() < avg)
{
System.out.println(" The Minimum Sales Amount is "+al.get(i).getsalesAmount());
System.out.println(" The Minimum Sales Amount is made by Mr."+al.get(i).getName());
}
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
//Main class
public class Analysis
{
public static void main(String[] args)
{
//create an object to Sales class
Sales ob = new Sales();
//invoke addSalesInfo() method
ob.addSalesInfo();
//create an object to Scanner
Scanner sc = new Scanner(System.in);
//infinite Loop
while(true)
{
//Options menu
System.out.println("------------------------------------------------------------------");
System.out.println(" choose the following option");
System.out.println(" 1.The total sales amount for the month");
System.out.println(" 2. The average sales amount per salesman for the month");
System.out.println(" 3.The lowest sales amount for the month along with that salesman
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.