[Need a help for java] [Problem] - I need to fill out 4 .java files to do this a
ID: 3663227 • Letter: #
Question
[Need a help for java]
[Problem] - I need to fill out 4 .java files to do this assignment.
Write a program that opens the salesdat.txt file and processes it contents. The program should display the following per store:
The total sales for each week. (Should print 5 values - one for each week).
The average daily sales for each week. (Should print 5 values - one for each week).
The total sales for all the weeks. (Should print 1 value)
The average weekly sales. (Should print 1 value)
The week with the highest amount in sales. (Should print 1 week #)
The week with the lowest amount in sales. (Should print 1 week #)
The file contains the dollars amount of sales that a retail store made each day for a number of weeks. Each line in the file contains thirty five numbers, which are sales numbers for five weeks. The number are separated by space. Each line in the file represents a separate store.
//FileIO.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileIO {
//Franchise readData(String filename)
public static void main(String[] args) {
try {
FileReader file = new FileReader("Salesdat.txt");
BufferedReader buff = new BufferedReader(file);
boolean eof = false;
while (!eof) {
String line = buff.readLine();
if (line == null)
eof = true;
else
System.out.println(line);
}
buff.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
}
}
//Store.java
public class Store {
private float salesbyweek[][];
Store() {
salesbyweek = new float[5][7];
}
//getter and setters
//setsaleforweekdayintersection(int week, int day, float sale)
public void setsaleforweekdayintersection(int week, int day, float sale){
salesbyweek[week][day]=sale;
}
//float [] getsalesforentireweek(int week)
//float getsaleforweekdayintersection(int week, int day)
//businessmethod
//a. totalsalesforweek
//b. avgsalesforweek
//c. totalsalesforallweeks
//d. averageweeklysales
//e. weekwithhighestsaleamt
//f. weekwithlowestsaleamt
//analyzeresults //call a through f
//print()
}
//Franchise.java
public class Franchise {
private Store stores[];
public Franchise(int num) {
stores = new Store[num];
}
public Store getStores(int i) {
return stores[i];
}
public void setStores(Store stores, int i) {
this.stores[i] = stores;
}
}
//Driver.java
public class Driver {
public static void main(String[] args) {
}
}
Explanation / Answer
public class Store
{
private float salesByWeek[][];
public Store()
{
salesByWeek = new float[5][7];
}
public void setSaleForWeekDayIntersection(int week, int day, float sale)
{
salesByWeek[week][day] = sale;
}
float[] getSalesForEntireWeek(int week)
{
float[] sales = new float[7];
for (int d = 0; d < 7; d++)
{
sales[d] = salesByWeek[week][d];
}
return sales;
}
float getSaleForWeekDayIntersection(int week, int day)
{
return salesByWeek[week][day];
}
float getTotalSalesForWeek(int week)
{
float total = 0;
for (int d = 0; d < 7; d++)
{
total += salesByWeek[week][d];
}
return total;
}
float getAverageSalesForWeek(int week)
{
return getTotalSalesForWeek(week) / 7;
}
float getTotalSalesForAllWeeks()
{
float total = 0;
for (int w = 0; w < 5; w++)
{
total += getTotalSalesForWeek(w);
}
return total;
}
float getAverageWeeklySales()
{
return getTotalSalesForAllWeeks() / 5;
}
int getWeekWithHighestSaleAmount()
{
int maxWeek = 0;
float maxSale = -1;
for (int w = 0; w < 5; w++)
{
float sale = getTotalSalesForWeek(w);
if (sale > maxSale)
{
maxSale = sale;
maxWeek = w;
}
}
return maxWeek;
}
int getWeekWithLowestSaleAmount()
{
int minWeek = 0;
float minSale = Float.MAX_VALUE;
for (int w = 0; w < 5; w++)
{
float sale = getTotalSalesForWeek(w);
if (sale < minSale)
{
minSale = sale;
minWeek = w;
}
}
return minWeek;
}
public void analyzeResults()
{
for (int w = 0; w < 5; w++)
{
System.out.printf("---- Week %d ---- ", w);
System.out.printf(" Total sales: %.2f ", getTotalSalesForWeek(w));
System.out.printf(" Average sales: %.2f ", getAverageSalesForWeek(w));
}
System.out.printf(" ");
System.out.printf("Total sales for all weeks: %.2f ", getTotalSalesForAllWeeks());
System.out.printf("Average weekly sales: %.2f ", getAverageWeeklySales());
System.out.printf("Week with highest sale: %d ", getWeekWithHighestSaleAmount());
System.out.printf("Week with lowest sale: %d ", getWeekWithLowestSaleAmount());
}
}
Driver.java
public class Driver
{
public static void main(String[] args)
{
Franchise f = FileIO.readData("Salesdat.txt");
for (int i = 0; i < 6; i++)
{
Store s = f.getStore(i);
System.out.printf("==== Store %d ==== ", i);
s.analyzeResults();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.