-1

Hello i am having trouble creating a running total of one of my methods.

public double calcProduct()
{
   productTotal = price * qty * shipCost;
   return productTotal;
}

the method i have listed above is supposed to calculate the total of 4 instances i have created but for some reason it is adding up the same number of "562" despite shipping cost and all the products being priced different. I also need a grand total of the 4 instances in one set price. i also have a static double called productTotal uninitialized. Edit: Here is a copy of my code for the "SalesOrder method and main method:

Main:

public class SalesOrderDemo


  // and initialize it to 0
  double grandTotal = 0;

  // Print out a heading line as follows:
  //     "Sales Orders"
  System.out.println("Sales Orders");
  //     "------------"
  System.out.println ("------------");

  // Orders to place ... 
  System.out.println ("Orders to place...");
  //    Use the overloaded constructor to create 
  //    the Following instances of Sales Order:
  //
  // Instance #1 -- instance name: samsungTV
  //  "Samsung 65 inch Television" - qty 1 - price $199.99
  //      To be shipped normal
  SalesOrder Instance1 = new SalesOrder();
  Instance1.setProductName ("Samsung 65 inch Television");
  Instance1.setQty (1);
  Instance1.setPrice(1599.99);
  Instance1.calcShipping("normal");
  // Instance #2 -- instance name: smartLights
  //  "Hue Smart Lights" - qty 6 - price $49.95
  //      To be shipped same day
  SalesOrder Instance2 = new SalesOrder();
  Instance2.setProductName ("Hue Smart Lights");
  Instance2.setQty (6);
  Instance2.setPrice(49.95);
  Instance2.calcShipping("sameDay");
  // Instance #3 -- instance name: bathTowels
  //  "Bathrool Towels" - qty 8 - price $19.45
  //      To be shipped 2nd day
  SalesOrder Instance3 = new SalesOrder();
  Instance3.setProductName ("Bathroom Towels");
  Instance3.setPrice(19.45);
  Instance3.setQty(8);
  Instance3.calcShipping("2-day");

  // Instance #1 -- instance name: samsungTV
  //  "Dinner Plates" - qty 15 - price $2.50
  //      To be shipped same day
  SalesOrder Instance4 = new SalesOrder();
  Instance4.setProductName("Dinner Plates");
  Instance4.setQty(15);
  Instance4.setPrice(2.50);
  Instance4.calcShipping("sameDay");


  // Execute the mutator method "calcShipping" to add the 
  //    appropriate shipping fee to the cost of the product
  // The method will pass the shipping type string parameter:
  //    A character to store the type of shipping method
  //        "normal" for no additional shipping charge
  //        "2-day" for adding $2 to the shipping fee for 2 day delivery
  //        "same" for adding $10 to the shipping fee for same day delivery
  // Execute the method for each of the instances you created above



  // Execute the get method "calcProdcut" to 
  //    1. Calculate the total amount for each product,
  //    2. Print the products information on each line
  //    3. Return to total amount value to be accumulated into "grandTotal"
  // Execute the method for each of the instances you created above
  Instance1.calcProduct();
  Instance2.calcProduct();
  Instance3.calcProduct();
  Instance4.calcProduct();

  grandTotal = Instance1.calcProduct() + Instance2.calcProduct()
  + Instance3.calcProduct() + Instance4.calcProduct();
  // Print all total of all of the orders as shown on the example
  // Format to 2 decimal places
  //    Don't forget to print a blank line

  Instance1.printProduct();
  System.out.println("");
  Instance2.printProduct();
  System.out.println("");
  Instance3.printProduct();
  System.out.println("");
  Instance4.printProduct();
  System.out.println(" ");
  System.out.println("The total of the orders are: " + grandTotal);

} }

Besco
  • 39
  • 1
  • 7

1 Answers1

0

It seems like you're having a bit of a misunderstanding of what "instance methods" allow you to do.

So, you're calling calcProduct twice, but no other SalesOrder instance knows about the values of the others, and that's by design. No matter how many times you run that method, result should be the same.

That being said, this is all you need. The shipping is probably not considered part of the product price. And it should be added, maybe, rather than multiplied (assuming all products of unlimited quantity are shipped at once, for a flat rate)

public double calcProduct() {
    return price * qty;
}

The correct way to calculate the total would be to sum each one, like you're already doing. Getter methods should not have side effects and alter results of other instances.

Note, it would be much simpler, if you used a loop over an arraylist

List<SalesOrder> orders = new ArrayList<>();
// add orders 
double grandTotal = 0;
for (SalesOrder so : orders) grandTotal += (so calcProduct() + so.calcShipping());
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245