有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java调用类文件及其方法

这是我在SalesManager客户端应用程序中的当前代码,我想提示用户输入文件名。然后NSTATITE一个SalesAnalyzer对象并调用上面的所有方法,输出每个方法的结果

我如何称呼SalesAnaylzer及其所有方法

销售经理:

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();
    }
}

下面是类文件:

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;
    }
}

共 (0) 个答案