Search code examples
staticundefinednon-static

"Undefined for type" and "cannot make static reference"?


I'm writing a program but I'm unable to call a few of the methods I made. The errors are as follows:
-method reportMenu(String) in the type CommissionReportSystem is not applicable for the arguments ()

-Cannot make a static reference to the non-static method getSalesData() from the type CommissionReportSystem

-The method computeTotalSales() is undefined for the type CommissionReportSystem

-The method computeSalesCommission(double) in the type CommissionReportSystem is not applicable for the arguments ()

-The method showAgentCommission(double) in the type CommissionReportSystem is not applicable for the arguments ()

I've tried a lot of fixes but nothing seems to be sticking and I'm unsure of how to proceed. I've included the relevant parts of the code below. I would appreciate any tips on how to fix any of these. Thank you!

      import java.io.*;
      import java.text.*;
      import java.util.*;

public class CommissionReportSystem {

    private static final String String = null;

    public static void main(String[] args) {
        getSalesData ();
        computeTotalSales ();
        computeSalesCommission ();
        showAgentCommission ();
        shutdown ();

        }
        String [] getSalesData (){
            String [] data = new String [2];

            String ticketsSold = "";
            String ticketPrice = "";
            String buffer = new String ();
            data[0] = buffer;
            data[1] = buffer;

            BufferedReader br = null;


            try {
                br = new BufferedReader (new InputStreamReader(System.in));
                System.out.print ("Enter tickets sold:");
                buffer = br.readLine ();
                ticketsSold = buffer;

                System.out.print ("Enter ticket price:");
                buffer = br.readLine ();
                ticketPrice = buffer;


            } catch (Exception e) {
                System.out.println ("Invalid entry");
            }

            data [0] = ticketsSold;
            data [1] = ticketPrice;
            return data;


            }
        public static double totalSales (String ticketsSold, String ticketPrice){

            int ticketsSoldNum = Integer.parseInt(ticketsSold);
            double ticketPriceNum = Double.parseDouble(ticketPrice);
            double totalSalesNum = ticketsSoldNum * ticketPriceNum;


            return totalSalesNum;}


        public static final double computeSalesCommission (double totalSalesNum){
            final double rate1 = 0.025;
            final double rate2 = 0.0375;
            final double rate3 = 0.0425;
            final double salesLimit1 = 2000;
            final double salesLimit2 = 4000;
            final double agentCommission= 0;

            if (totalSalesNum <= 2000) {
                agentCommission = rate1 * totalSalesNum;
            } 
            else if (totalSalesNum <= 4000){
                agentCommission = rate2 * totalSalesNum;
            }
            else (totalSalesNum > 4000){
                agentCommission = rate3 * totalSalesNum;
            }

            return agentCommission;

        }

        public static void showAgentCommission (double agentCommission){
            System.out.format ("Congratulation agent Cindy Smith, your current daily commission:" + agentCommission);
        }

        public static void shutdown (){
            System.out.format ("Thank you for your time! Have a great day!");
        }

        public static void handleInvalidData (){


        }
}

Solution

  • 1) getSalesData() is an instance method. If you want to call an instance method, create an object of the class and call method using that. Else you have to make the method static. Remember one the thing you cannot access the instance variables inside static method.

    2) There is no method computeTotalSales() in your class.

    3) computeSalesCommission() requires an argument of type double. You have called it without any argument.

    4) The last comment is also valid for showAgentCommission().