SO, here it goes, Week 2! How'd you do?
import java.io.*;
import java.util.*;
public class BajrovicAssignment2 {
public static void main(String[] args) throws FileNotFoundException{
//asks the user to type in the path of the file, noting that they must enter '/' instead of '\' because that is an escape sequence
System.out.println("Please enter the file path to your Comma Separated Value file" + "\n" + "\t" + "For example:C:/Users/Public/test.csv");
Scanner in = new Scanner(System.in);
String filePath = in.nextLine();
String fileLine;
Scanner in2 = new Scanner(new File(filePath));
String [] intoArray = new String [13]; //each appended line from the file
String[] months = new String [12]; //the first row of the file, that contains the months (12 because you don't care about the "manufacturer" title
int [] salesValues = new int [12]; //the sales numbers of every line from the file
while (in2.hasNextLine())
{
fileLine = in2.nextLine(); //stores each line of file into a regular string
StringBuilder newLine = new StringBuilder(); //create a new stringbuilder object
newLine.append(fileLine); //adds each line read from file to string builder
String appendString = newLine.toString(); //converts stringbuilder to a string
intoArray = appendString.split(","); //gets the values
int element = 0;
for (int i=1; i < 13; i++)
{
if(intoArray[0].equals("Manufacturer")) //stores the months in the months array declared earlier
{
int place = 0;
for(int y = 1; y < 12; y++)
{
months[place] = intoArray[y];
place++;
}
break;
}
salesValues[element] = Integer.parseInt(intoArray[i]); //converts all the 'strings' (monthly values) in the file to numbers
element++;
}
if ((intoArray[0].equals("Manufacturer"))!= true) //for all the rest of the lines in the file, calls the functions in the AutoSales class
{
AutoSales a = new AutoSales(intoArray[0], salesValues, months); //defines the local variables to pass to AutoSales class
System.out.print("************************************************\n");
System.out.println("Manufacturer: " + a.brand);
System.out.println("Total Yearly Sales: " + a.sum());
a.avg();
a.highest();
System.out.print("************************************************\n\n");
}
}
}
}
class AutoSales{
public String brand; //defines the fields of this class
public int[] monthlySalesFigures;
public String[] passedMonths;
public AutoSales(String ManuFacturer, int[] salesValues, String[] months) {
//defines the constructor of this class
brand = ManuFacturer;
monthlySalesFigures = salesValues;
passedMonths = months;
}
public void highest() {
//function finds the highest value in the row (so for a manufacturer)by moving through the array and replacing the mxm variable with values that are larger than its current value
int mxm = monthlySalesFigures[0], i;
String monthHighest = passedMonths[0];
for (i=0; imxm) {
monthHighest = passedMonths[i];
mxm = monthlySalesFigures[i]; }
}
System.out.println(monthHighest + " has the highest Sales: " + mxm);
}
public void avg() {
//finds the average by calling the sum method
float avg;
int sum = sum();
avg = sum/12;
System.out.println("Average Monthly Sales:" + avg);
}
public int sum() {
//adds all the values of the file by adding to the first value in that row in the file while moving through the value array
int sum = 0;
for (int i = 0; i < monthlySalesFigures.length; i++){
sum = sum + monthlySalesFigures[i];
}
return sum;
}
}
0 comments:
Post a Comment