Link to home
Start Free TrialLog in
Avatar of Halt123
Halt123

asked on

TotalPayment not working

Dear experts,

At the moment when I run this code it only displays 0.0 for the value it doesnt display the proper total

The code for the revelant bit is:
public static double Ftotalpayment()
 {

    double FTotalPayment = 0.0;
    int numJob = collection.getnumJob();

    for (int index  = 0; index < numJob; ++index)
    {

      FixedRateJob temp = collection.getFJob(index);

          if ((temp.getPaid()).equals("No"))
          {
            double totalCost = temp.getTotal();
            FTotalPayment +=  totalCost;
           }

      }

       System.out.println(FTotalPayment);
       return FTotalPayment;
}

thank you for your help in advance
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

PLease post

FixedRateJob.getTotal
Avatar of Halt123
Halt123

ASKER

public double getTotal()
      {
      Total = (LabourCost + MaterialCost);
      return Total;
      }
(e.g. if that only returns zero, you'll get a zero total)
Avatar of Halt123

ASKER

Sorry could you explain it a bit more please
Might be a good idea to post all your current code if it's not too large
Avatar of Halt123

ASKER

Do you want both the fixedratejob code and the application3 code?
Yes please
Avatar of Halt123

ASKER

FIXEDRATEJOB
Public class FixedRateJob extends Job
{
   //---Attributes---
    private double LabourCost;
     private double Total;

  FixedRateJob()
  {
  }
  //---Operations---
  public FixedRateJob(String theId,String theCustomer,String IsPaid,double theMaterialCost,double theLabourCost)
  {
    super(theId,theCustomer,IsPaid,theMaterialCost);
      LabourCost = theLabourCost;
   }

  public void setLabourCost(double theLabourCost){LabourCost = theLabourCost;}

   public String toString()
    {
     return Id + "" + Customer + "" + Paid + "" + MaterialCost + "" +  LabourCost;
    }

     public double getTotal()
    {
        Total = (LabourCost + MaterialCost);
         return Total;
     }

       public void displayDetails()
       {
          super.displayDetails();
          System.out.println("labour cost is:" +LabourCost);
        }
       

}// class FixedRateJob
heres the first one
Avatar of Halt123

ASKER

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

public class Application3
{
  private static JobCollection collection = new JobCollection(100);
  private static Scanner kybd = new Scanner(System.in);

  public static void main(String args[])
  {
    mainmenu();
    int num = readChoice();
   
    while (num != 14)
    {
      doChoice(num);
      mainmenu();
      num = readChoice();
    }
  }

   //menu list options
   public static void mainmenu()
   {
      System.out.println("........Welcome........\n");
      System.out.println("1:Add Fixed Job");
      System.out.println("2:Add Hourly Paid Job");
      System.out.println("3:Find a Fixed Job by Id");
      System.out.println("4:Find a Hourly Paid Job by Id");
      System.out.println("5:Make a Payment for a Fixed Job");
      System.out.println("6:Make a Payment for a Hourly Paid Job");
      System.out.println("7:Calulate total payments due from all  Fixed Jobs");
      System.out.println("8:Calulate total payments due from all  Hourly Paid Jobs");
      System.out.println("9:Calulate cost of materials for all Fixed jobs");
      System.out.println("10:Calulate cost of materials for all Hourly Paid jobs");
      System.out.println("11:Calulate total revenus for all Fixed jobs");
      System.out.println("12:Calulate total revenus for all Hourly jobs");
      System.out.println("13:Display the Jobs");
      System.out.println("14:Quit");
     
      System.out.println("\n" + " Please enter your choice 1-14");
    }
//-----------------------------------------------------------------------------------
//reads the users choice and displays "Invalid choice re-enter choice" if invalid
public static int readChoice()
{
 int choice = 0;
 boolean invalidEntry = true;
do{
   choice = kybd.nextInt();
   invalidEntry = (choice < 1 || choice >14);
   if(invalidEntry)
   System.out.println("\n Invalid choice re-enter your choice:");
   } while (invalidEntry);
  return choice;
}
 
//-------------------------------------------------------------------------------------

//switch statement used to choose option
 public static void doChoice(int choice)
{
 switch(choice)
 {
  case 1: addFJob(); break;
  case 2: addHJob(); break;
  case 3: findFJob(); break;
  case 4: findHJob(); break;
  case 5: payFJob(); break;
  case 6: payHJob(); break;
  case 7: Ftotalpayment(); break;
  case 8: Htotalpayment(); break;
  case 9: Fcostmaterial(); break;
  case 10: Hcostmaterial(); break;
  case 11: Ftotalrevenue(); break;
  case 12: Htotalrevenue(); break;
  case 13: displayJob(); break;
  case 14: quit(); break;
 }
}

//---------------------------------------------------------------------------------------
 //add a fixed job
 public static void addFJob()
 {
  String Id;
  String Customer;
  String Paid;
  double MaterialCost;
  double LabourCost;


    System.out.println("please enter Job Id");
    Id = kybd.next();
    System.out.println("please enter Customer name");
    Customer = kybd.next();
    System.out.println("Has the Job been paid for?");
    Paid = kybd.next();
    System.out.println("please enter Material cost");
    MaterialCost = kybd.nextInt();
    System.out.println("please enter labour cost");
    LabourCost = kybd.nextInt();
    FixedRateJob job = new  FixedRateJob(Id, Customer, Paid, MaterialCost, LabourCost);
    collection.addFJob(job);
   
}

//add a Houlry Paid job
public static void addHJob()
  {
 
  String Id;
  String Customer;
  String Paid;
  double MaterialCost;
  int NumberHourWorked;
  double RatePerHour;

    System.out.println("please enter Job Id");
    Id = kybd.next();
    System.out.println("please enter Customer name");
    Customer = kybd.next();
    System.out.println("Has the Job been paid for?");
    Paid = kybd.next();
    System.out.println("please enter Material cost");
    MaterialCost = kybd.nextInt();
    System.out.println("please enter Number Hour Worked");
    NumberHourWorked = kybd.nextInt();
    System.out.println("please enter Rate Per Hour");
    RatePerHour = kybd.nextInt();
   HourlyPaidJob job2 = new  HourlyPaidJob(Id, Customer, Paid, MaterialCost, NumberHourWorked, RatePerHour);
   collection.addHJob(job2);
   }
 
 


//------------------------------------------------------------
public static Job findFJob()
 {
 
  System.out.println("What Job would you like to find?");
  String Id= kybd.next();
  Job j = findFJob(Id);
   System.out.println(j);
  return j;
 }

public static Job findFJob(String Id)
 {
   Job j = collection.findFJob(Id);
   System.out.println(j);
   return j;
  }

public static Job findHJob()
{
  System.out.println("What Job would you like to find?");
  String Id= kybd.next();
  Job h = findHJob(Id);
  System.out.println(h);
  return h;
 }

public static Job findHJob(String Id)
{
  Job h = collection.findHJob(Id);
  System.out.println(h);
  return h;
}
//---------------------------------------------------
 public static void payFJob()
{
  String agree;
  System.out.println("What Job would you like to pay?");
  String Id= kybd.next();
  Job j = findFJob(Id);


    System.out.println("Is this the job you wanted to make a payment to? Y for Yes and N for No");
    agree = kybd.next();
     if(agree.equals("Y"))
     {
       j.setPaid("Yes");
     }
     else
     {
       j.setPaid("No");
     }


}

public static void payHJob()
{
  String agree;
  System.out.println("What Job would you like to pay?");
  String Id= kybd.next();
  Job h = findHJob(Id);


    System.out.println("Is this the job you wanted to make a payment to? Y for Yes and N for No");
    agree = kybd.next();
     if(agree.equals("Y"))
     {
       h.setPaid("Yes");
     }
     else
     {
       h.setPaid("No");
     }
}

//------------------------------------------------------------------
public static double Ftotalpayment()
{
   double FTotalPayment = 0.0;
    int numJob = collection.getnumJob();

    for (int index  = 0; index < numJob; ++index)
    {
         FixedRateJob temp = collection.getFJob(index);
      if ((temp.getPaid()).equals("No"))
       {
           double totalCost = temp.getTotal();
            FTotalPayment +=  totalCost;
        }
      }
        System.out.println(FTotalPayment);
        return FTotalPayment;
}


public static double Htotalpayment()
{
    double HTotalPayment = 0.0;
    int numJob1 = collection.getnumJob1();

     for (int index  = 0; index <numJob1; ++index)
     {
         HourlyPaidJob temp = collection.getHJob(index);
          if ((temp.getPaid()).equals("No"))
          {
             double totalCost = temp.getTotal();
              HTotalPayment +=  totalCost;
           }
        }
          System.out.println(HTotalPayment);
         return HTotalPayment;
}

//-----------------------------------------
public static double Fcostmaterial()
{
        double FCostMaterial = 0.0;
        int numJob = collection.getnumJob();

      for (int index = 0; index < numJob ; ++index)
      {
            FixedRateJob temp = collection.getFJob(index);
            if ((temp.getPaid()).equals("No"))
            {
                  FCostMaterial += temp.getMaterialCost();
            }
      }
       return FCostMaterial;
}

public static double Hcostmaterial()
{
       double HCostMaterial= 0.0;
        int numJob1 = collection.getnumJob1();
      for (int index = 0; index < numJob1 ; ++index)
      {
            HourlyPaidJob temp = collection.getHJob(index);
            if ((temp.getPaid()).equals("No"))
            {
                  HCostMaterial += temp.getMaterialCost();
            }
      }
        return HCostMaterial;
}

//------------------------------------------
public static void Ftotalrevenue()
{
double FTotalRenvenue = 0.0;
double FCostMaterial = 0.0;
double FTotalPayment = 0.0;
int numJob = collection.getnumJob();


         for (int index = 0; index <numJob; ++index)
         {
            FixedRateJob temp = collection.getFJob(index);
            if ((temp.getPaid()).equals("Yes"))
            {
               FTotalRenvenue += (FTotalPayment - FCostMaterial);
             }
          }
}


public static void Htotalrevenue()
{
   double HTotalRenvenue = 0.0;
  double HCostMaterial = 0.0;
  double HTotalPayment = 0.0;
  int numJob1 = collection.getnumJob1();

   for (int index = 0; index <numJob1; ++index)
   {
       HourlyPaidJob temp = collection.getHJob(index);
        if ((temp.getPaid()).equals("Yes"))
        {
            HTotalRenvenue += (HTotalPayment - HCostMaterial);
        }
    }


}
//-----------------------------------------------------
public static void displayJob()
{
  int index =  0;
 
  Job h = collection.getFJob(index);
  Job j = collection.getHJob(index);

   if (h != null ) h.displayDetails();
   if (j != null )j.displayDetails();

 
   

}
//-----------------------------------------------------
 public static void quit()
{
 System.exit(0);
}


}

heres the other one
>>
if ((temp.getPaid()).equals("No"))
          {
            double totalCost = temp.getTotal();
            FTotalPayment +=  totalCost;
           }
>>

Make sure that's getting into the if block if it should do
Avatar of Halt123

ASKER

Do ypu mean input data with the value of Paid = No and input data with the value of Paid = yes and see what happens?
Check your JobCollection class, looks like it may be the cause of your problems.

you also don't need to keep Total as a member var in your FixedTotal class as you calculate it every time.
It can just be:

   public double getTotal()
    {
        return (LabourCost + MaterialCost);
     }

And delete the Total member var
Avatar of Halt123

ASKER

This is my JobCollection class, i cant see anything wrong with it..

public class JobCollection //
{

   private FixedRateJob[] theJob1;
   private HourlyPaidJob[] theJob;

   private int numJob;
   private int numJob1;

    public JobCollection(int size)
    {
      theJob1 = new FixedRateJob[size];
      theJob = new HourlyPaidJob[size];
      numJob = 0;
      numJob1 = 0;
    }
 

     public boolean addFJob(FixedRateJob newFixedRateJob)//adding new Fixed Job
     {
       theJob1[numJob] = newFixedRateJob;
       numJob1++;
       return true;
      }

     public boolean addHJob(HourlyPaidJob newHourlyPaidJob)//adding new Hourly Job
     {
          theJob[numJob] = newHourlyPaidJob;
          numJob++;
          return true;
     }
     

      public HourlyPaidJob getHJob(int index)//getting the Job
      {
        if(index>=0 && index<numJob)
        {
        return theJob[index];
     
        }
        else
        {
        return null;
        }
      }

         public FixedRateJob getFJob(int index)//getting the Job
       {
        if(index>=0 && index<numJob1)
        {
                     return theJob1[index];
        }
        else
        {
               return null;
        }
       }


            public HourlyPaidJob findHJob(String Id)//finding the Job
            {
              for(int index=0; index<numJob; index++)
              {
                  if (theJob[index].getId().equals(Id))
                        return theJob[index];
                                   }
                    return null;
            }

            public FixedRateJob findFJob(String Id)//finding the Job
            {
              for(int index=0; index<numJob1; index++)
              {
                  if (theJob1[index].getId().equals(Id))
                        return theJob1[index];
              }
              return null;
            }

      
  public int getnumJob()
 {
    return numJob;
  }

  public int getnumJob1()
 {
    return numJob1;
 }
}        
> int numJob = collection.getnumJob();

that should be:

int numJob = collection.getnumJob1();
Avatar of Halt123

ASKER

it brings up the error  
 Please enter your choice 1-14
7
Exception in thread "main" java.lang.NullPointerException
        at Application3.Ftotalpayment(Application3.java:253)
        at Application3.doChoice(Application3.java:72)
        at Application3.main(Application3.java:17)
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Halt123

ASKER

When I run the code it gives you the total payment due for that one Job, but I would like it to give me a running total.

for example if I entered in
Job ID = js
Customer = Ben
Paid= No
Material Cost = 2
Labour Cost = 2

and
Job ID = ew
Customer = Lucy
Paid= No
Material Cost = 1
Labour Cost = 1

The total payment would = 6
Avatar of Halt123

ASKER

thank you for all of your help
You should probably make that a method of your 'collection' class
Iterate the array(s) in your collection class and call getTotal on each, adding to a running total variable
Something like

double runningTotal = 0.0;
for(int i = 0;i < theJob1.length;i++) {
        runningTotal += theJob1[i].getTotal();
}
return runningTotal;
Avatar of Halt123

ASKER

Ok i will give it a go

thank you
hmm, all looks ok. Add some debug println() statements into your loop to track whats happening

btw, theres also a similiar problem in your other totals methos

> int numJob1 = collection.getnumJob1();

whould be:

int numJob1 = collection.getnumJob();

Would be a lot simpler to have your collection class handle a single collection of jobs, and thrn in your application have two instances of collections. One to store fixed rate and the other storing hourly. less chance of these types of errors that way. And less code duplication.
> Ok i will give it a go
you just need to move your existing code as a method

public static double Ftotalpayment()
{
   double FTotalPayment = 0.0;
    int numJob = getnumJob1();

    for (int index  = 0; index < numJob; ++index)
    {
         FixedRateJob temp = getFJob(index);
         if (temp==null)
         {
             System.out.println("job was null");
          }
         else if (temp.getPaid()==null)
          {
              System.out.println("paid is null");
          }
      else if ((temp.getPaid()).equals("No"))
       {
           double totalCost = temp.getTotal();
            FTotalPayment +=  totalCost;
        }
      }
        System.out.println(FTotalPayment);
        return FTotalPayment;
}

It'll still work the same though.
Avatar of Halt123

ASKER

ill check through my code for errors n then try it again

thank you
A few println()'s should help to highlight where its going wrong.
Avatar of Halt123

ASKER

I'm not sure if this will help here the output from when I run it:
H:\Job>java Application3
........Welcome........

1:Add Fixed Job
2:Add Hourly Paid Job
3:Find a Fixed Job by Id
4:Find a Hourly Paid Job by Id
5:Make a Payment for a Fixed Job
6:Make a Payment for a Hourly Paid Job
7:Calulate total payments due from all  Fixed Jobs
8:Calulate total payments due from all  Hourly Paid Jobs
9:Calulate cost of materials for all Fixed jobs
10:Calulate cost of materials for all Hourly Paid jobs
11:Calulate total revenus for all Fixed jobs
12:Calulate total revenus for all Hourly jobs
13:Display the Jobs
14:Quit

 Please enter your choice 1-14
1
please enter Job Id
er
please enter Customer name
dsf
Has the Job been paid for?
No
please enter Material cost
2
please enter labour cost
2
........Welcome........

//menu choices go in here

 Please enter your choice 1-14
7
4.0
........Welcome........
//menu choices go in here

 Please enter your choice 1-14
1
please enter Job Id
oi
please enter Customer name
qw
Has the Job been paid for?
No
please enter Material cost
1
please enter labour cost
1
........Welcome........
//menu choices go in here

 Please enter your choice 1-14
7
job was null
2.0
........Welcome........

//menu choices go in here

 Please enter your choice 1-14
>        theJob1[numJob] = newFixedRateJob;

should be:

       theJob1[numJob1] = newFixedRateJob;
Avatar of Halt123

ASKER

it works .. :)

thank you

no worries :)