[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

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
0
Halt123
Asked:
Halt123
  • 14
  • 8
  • 8
1 Solution
 
CEHJCommented:
PLease post

FixedRateJob.getTotal
0
 
Halt123Author Commented:
public double getTotal()
      {
      Total = (LabourCost + MaterialCost);
      return Total;
      }
0
 
CEHJCommented:
(e.g. if that only returns zero, you'll get a zero total)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Halt123Author Commented:
Sorry could you explain it a bit more please
0
 
CEHJCommented:
Might be a good idea to post all your current code if it's not too large
0
 
Halt123Author Commented:
Do you want both the fixedratejob code and the application3 code?
0
 
CEHJCommented:
Yes please
0
 
Halt123Author Commented:
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
0
 
Halt123Author Commented:
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
0
 
CEHJCommented:
>>
if ((temp.getPaid()).equals("No"))
          {
            double totalCost = temp.getTotal();
            FTotalPayment +=  totalCost;
           }
>>

Make sure that's getting into the if block if it should do
0
 
Halt123Author Commented:
Do ypu mean input data with the value of Paid = No and input data with the value of Paid = yes and see what happens?
0
 
objectsCommented:
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
0
 
Halt123Author Commented:
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;
 }
}        
0
 
objectsCommented:
> int numJob = collection.getnumJob();

that should be:

int numJob = collection.getnumJob1();
0
 
Halt123Author Commented:
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)
0
 
objectsCommented:
try this:

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

    for (int index  = 0; index < numJob; ++index)
    {
         FixedRateJob temp = collection.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;
}
0
 
Halt123Author Commented:
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
0
 
Halt123Author Commented:
thank you for all of your help
0
 
CEHJCommented:
You should probably make that a method of your 'collection' class
0
 
CEHJCommented:
Iterate the array(s) in your collection class and call getTotal on each, adding to a running total variable
0
 
CEHJCommented:
Something like

double runningTotal = 0.0;
for(int i = 0;i < theJob1.length;i++) {
        runningTotal += theJob1[i].getTotal();
}
return runningTotal;
0
 
Halt123Author Commented:
Ok i will give it a go

thank you
0
 
objectsCommented:
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.
0
 
objectsCommented:
> 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.
0
 
Halt123Author Commented:
ill check through my code for errors n then try it again

thank you
0
 
objectsCommented:
A few println()'s should help to highlight where its going wrong.
0
 
Halt123Author Commented:
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
0
 
objectsCommented:
>        theJob1[numJob] = newFixedRateJob;

should be:

       theJob1[numJob1] = newFixedRateJob;
0
 
Halt123Author Commented:
it works .. :)

thank you

0
 
objectsCommented:
no worries :)
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 14
  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now