Domanda

Here is my current code In the SalesManager client application i want to prompt the user for the filename. Then nstantiate a SalesAnalyzer object and call all the methods above, outputting the results from each method.

How do I call SalesAnaylzer and all of its methods?

SalesManager:

import java.io.File;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.IOException;;

public class SalesManager
{
    public static void main( String []args) throws IOException
    {
        System.out.println(" What is the name of the file");
        Scanner scan= new Scanner(System.in);
        String fileName= scan.next();
    }
}

and here is the class file:

import java.io.File;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.IOException;
import java.util.*;
import javax.swing.*;
import java.awt.*;

public class SalesAnaylzer {

    DecimalFormat pricePattern = new DecimalFormat("$#0.00");
    int[][] sales = new int[3][4];

    public SalesAnaylzer(String fileName) throws IOException {

        File inputFile = new File(fileName);
        Scanner scan = new Scanner(inputFile);
        for (int row = 0; row < 4; row++) {
            for (int col = 0; col < 6; col++) {
                sales[row][col] = scan.nextInt();
            }
        }
    }

    public String toString() {
        String data = "";
        for (int row = 0; row < 4; row++) {
            data = data + "\nStore " + (row + 1) + ": ";
            for (int col = 0; col < 6; col++) {
                data = data + "QTR " + (col + 1) + ": " + pricePattern.format(sales[row][col]) + " ";
            }
        }
        return data;
    }

    public double totalSales() {
        double total = 0.0;
        for (int row = 0; row < 4; row++) {
            for (int col = 0; col < 6; col++) {
                total = total + sales[row][col];
            }
        }
        return total;
    }

    public double highStoreSales(int store) {
        double highest = 0.0;
        for (int row = 0; row < 4; row++) {
            if (sales[row][store] > highest)
                highest = sales[row][store];
        }
        return highest;
    }

    public double averageStoreSales(int quarter) {
        double total = 0.0;
        double avg = 0.0;
        for (int col = 0; col < 6; col++) {
            total = total + sales[quarter][col];
        }
        avg = (total / 4);
        return avg;
    }
}
È stato utile?

Soluzione

Make sure they are in the same package so that you can directly call on the constructor and methods of the SalesAnaylzer class. Then just create a SalesAnalyzer object like you would any other:

SalesAnaylzer sA = new SalesAnaylzer(filename);
System.out.println(sA);
etc...

Altri suggerimenti

First, lets imagine you saved both classes in the package (folder) named "myPackage". First thing you should add in every class is this line of code:

package myPackage;

After you did that you have to create a new instance of SalesAnalyzer inside the SalesManager class, the same way you created a new Scanner.

Scanner scan = new Scanner(System.in); //Here you created a new Scanner

Create a new SalesAnalyzer:

SalesAnalyzer newSA = new SalesAnalyzer();

Now, if you want to access any method, you have to write the name of yours SalesAnalyzer object, dot after that and then name of the method you wish to call.

/*
* Here you created a new String named fileName and added it value that
* yours "next" method returned. "next" is a method inside Scanner object
* and you called for it by writing "scan.next()".
*/

String fileName = scan.next();

This is what next method looks like inside Scanner object:

public String next() {
    ensureOpen();
    clearCaches();

    while (true) {
        String token = getCompleteTokenInBuffer(null);
        if (token != null) {
            matchValid = true;
            skipped = false;
            return token;
        }
        if (needInput)
            readInput();
        else
            throwFor();
    }
}

Now, lets say you want to call for totalSales method inside your SalesAnalyzer. It's a method that returns double type value. In case you want to save that value to some variable in your SalesManager class you have to create variable of double type and call for the totalSales method after. It should look like this:

double totalS = newSA.totalSales();

This way you accessed to method totalSales inside your SalesAnalyzer from your SalesManager class and saved the value it returned to double variable named totalS. Hopefully I clarified anything to you and helped you even a little. If you have any further question feel free to ask.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top