为我的一种方法创建运行总计

时间:2018-11-08 04:00:51

标签: java class methods method-call

您好,我无法创建一种我的方法的总运行量。

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

我上面列出的方法应该计算出我创建的4个实例的总数,但是由于某种原因,尽管运输成本和所有产品的定价都不一样,但它仍会添加相同数量的“ 562”。我还需要以固定价格总计4个实例。我也有一个未初始化的静态double,称为productTotal。 编辑:这是我的代码的“ SalesOrder方法和主要方法:

主要:

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

} }

1 个答案:

答案 0 :(得分:0)

似乎您对“实例方法”允许您执行的操作有点误解。

因此,您两次调用<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> <div class="container-fluid"> <div class="row mx-md-2"> <div class="card-group col-lg-8"> <div class="col-6 col-sm-6 col-md-4 col-lg-4 mb-3 divCard"> <article class="card"> <a href="#"> <img class="card-img-top" src="https://imgjapan.com/wp-content/themes/img_jpn/static/img/global-locations/img-singapore.jpg"> </a> </article> </div> <div class="col-6 col-sm-6 col-md-4 col-lg-4 mb-3 divCard"> <article class="card"> <a href="#"> <img class="card-img-top" src="https://imgjapan.com/wp-content/themes/img_jpn/static/img/global-locations/img-singapore.jpg"> </a> </article> </div> <div class="col-6 col-sm-6 col-md-4 col-lg-4 mb-3 divCard"> <article class="card"> <a href="#"> <img class="card-img-top" src="https://imgjapan.com/wp-content/themes/img_jpn/static/img/global-locations/img-singapore.jpg"> </a> </article> </div> </div> <div class="col-12 col-lg-4" style="background-color: red"></div> </div> </div>,但是没有其他calcProduct实例知道其他实例的值,这是设计使然。无论您运行该方法多少次,结果都应该相同。

话虽如此,这就是您所需要的。运输可能不被视为产品价格的一部分。并且应该加上而不是乘以(假设所有数量不限的产品都以统一价格一次发货)

SalesOrder

计算总数的正确方法是将每个总数相加,就像您已经在做的那样。吸气方法不应有副作用,并且不会改变其他实例的结果。

请注意,如果您在arraylist上使用了循环,那会简单得多

public double calcProduct() {
    return price * qty;
}
相关问题