display the total sales for each week
the average daily sales for each week
the total sales for all of the weeks
the average weekly sales
the week number that had the highest amount of sales
the week number that had the lowest amount of sales
I can get everything but the very last one. In my code you will see that I tried to use a really large number so it would pull out the lowest amount, but it still didn't work: low = 100000000;
Any help would be appreciated.
Thank you.
SalesData.txt (173bytes)
Number of downloads: 117
import javax.swing.JOptionPane;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;
public class SalesAnalysis
{
private String line;
private Scanner inputFile;
private double averageDaily; // Holds the average daily sales for each week
private double totalSales; // Holds the total sales for all of the weeks
private double averageWeekly; // Holds the average weekly sales
// Constructor, takes a file name as its argument
public SalesAnalysis(String file_Name) throws IOException
{
// Open the file
File file = new File("SalesData.txt");
inputFile = new Scanner(file);
}
// Method that reads the next line(if there is any) in the file
public boolean interpretNextLine() throws IOException
{
boolean readNext;
// Determine if there are more lines to read in the file
readNext = inputFile.hasNext();
// Now, read the next line if there is one
if(readNext)
line = inputFile.nextLine();
return readNext;
}
// Method that calculates the total sales for all three weeks
public double getTotal()
{
totalSales = 0;
// Tokenize the last line read from the file
String[] token = line.split(",");
// Calculate the total of the sales
for(String string : token)
totalSales += Double.parseDouble(string);
return totalSales;
}
// Method that calculates the average daily sales for each week
public double getAverageDailySales()
{
averageDaily = 0;
String[] token = line.split(",");
for(String string : token)
averageDaily = getTotal() / token.length;
return averageDaily;
}
// Method that calculates the average weekly sales
public double getAverageWeeklySales()
{
String[] token = line.split(",");
for( String string : token)
averageWeekly = getTotal() / 3;
return averageWeekly;
}
public void close() throws IOException
{
inputFile.close();
}
public static void main(String[] args) throws IOException
{
int count = 0;
double totalSales = 0;
double totalAvgWeek = 0;
double high = 0;
double low = 1000000000;
// Create a String object
String string = new String("Numbers");
SalesAnalysis sales = new SalesAnalysis(string);
// Display the results
while(sales.interpretNextLine())
{
JOptionPane.showMessageDialog(null, "The total sales for week " + (count + 1) + " was " + sales.getTotal());
JOptionPane.showMessageDialog(null, "The average daily sales for week " + (count + 1) + " was " + sales.getAverageDailySales());
totalSales += sales.getTotal();
totalAvgWeek += sales.getAverageWeeklySales();
//high += sales.getTotal();
//low += sales.getTotal();
if(sales.getTotal() > high)
{
high = sales.getTotal();
high = count;
}
if(sales.getTotal() < low)
{
low = sales.getTotal();
low = count;
}
count ++;
}
JOptionPane.showMessageDialog(null, "The average weekly sales was " + totalAvgWeek);
JOptionPane.showMessageDialog(null, "The total sales were " + totalSales);
JOptionPane.showMessageDialog(null, "Week " + high + " had the most sales");
JOptionPane.showMessageDialog(null, "Week " + low + " had the least sales");
// Close the file
sales.close();
System.exit(0);
}
}

New Topic/Question
Reply


MultiQuote




|