将计算值返回为String

时间:2014-07-24 23:14:04

标签: java

public PaintShopCalculator(int heightFeet, int heightInches,
        int lengthFeet, int lengthInches, int widthFeet, int widthInches) {
    // calculate the total area
    int totalarea = ((widthFeet*12+widthInches)*(lengthFeet*12+lengthInches))
            + 2*((heightFeet*12+heightInches)*(widthFeet*12+widthInches))+ 
            2*((heightFeet*12+heightInches)*(lengthFeet*12+lengthInches));
    //amount of paints needed
    double totalpaint = (totalarea/AREA_PER_GALLON);
    int totalpaint2 = (int) (totalarea/AREA_PER_GALLON);
    double totalprice = 0;
    int fivegallonneeded = 0;
    int onegallonneeded = 0;
    int halfgallonneeded = 0;
    int quartneeded = 0;
    int pintneeded = 0;
    int halfpintneeded = 0;

    if (totalpaint > 0){
        fivegallonneeded = (int)fivegallonneeded + totalpaint2/5 ;
        totalpaint = totalpaint - (fivegallonneeded*5);
        totalpaint2 = totalpaint2 - (fivegallonneeded*5);
        onegallonneeded = (int) onegallonneeded + totalpaint2/1;
        totalpaint = totalpaint - (onegallonneeded*1);
        totalpaint2 = totalpaint2 - (onegallonneeded*1);
        halfgallonneeded = (int) (halfgallonneeded + totalpaint/0.5);
        totalpaint = totalpaint - halfgallonneeded*0.5;
        quartneeded  = (int) (quartneeded + totalpaint/0.25);
        totalpaint = totalpaint - quartneeded*0.25;
        pintneeded = (int) (pintneeded + totalpaint/0.125);
        totalpaint = totalpaint - pintneeded*0.125;
        if (totalpaint > 0){
            halfpintneeded += 1;
        }
    // total price  
    totalprice = (fivegallonneeded * FIVEGALLONS) + (onegallonneeded * ONEGALLON) + (halfgallonneeded* HALFGALLON) 
            +(quartneeded * QUART) +(pintneeded * PINT) +(halfpintneeded * HALFPINT); 

我目前正致力于一个计算房间涂漆价格的项目。我已经完成了计算,但现在我需要知道如何将它们作为字符串返回(例如totalprice,fivegallonneeded)以打印出收据,告诉客户估计所需的涂料量和价格

1 个答案:

答案 0 :(得分:1)

返回包含所有客户需求的对象。

考虑将这些添加到方法中,该方法将返回对象,而不是使用构造函数。

所以基本上你的课程将是:

public class PaintShopCalculator {

    public RetObject calculateStuff(int heightFeet, int heightInches,
        int lengthFeet, int lengthInches, int widthFeet, int widthInches) {
    // calculate the total area
    int totalarea = ((widthFeet*12+widthInches)*(lengthFeet*12+lengthInches))
            + 2*((heightFeet*12+heightInches)*(widthFeet*12+widthInches))+ 
            2*((heightFeet*12+heightInches)*(lengthFeet*12+lengthInches));
    //amount of paints needed
    double totalpaint = (totalarea/AREA_PER_GALLON);
    int totalpaint2 = (int) (totalarea/AREA_PER_GALLON);
    double totalprice = 0;
    int fivegallonneeded = 0;
    int onegallonneeded = 0;
    int halfgallonneeded = 0;
    int quartneeded = 0;
    int pintneeded = 0;
    int halfpintneeded = 0;

    if (totalpaint > 0){
        fivegallonneeded = (int)fivegallonneeded + totalpaint2/5 ;
        totalpaint = totalpaint - (fivegallonneeded*5);
        totalpaint2 = totalpaint2 - (fivegallonneeded*5);
        onegallonneeded = (int) onegallonneeded + totalpaint2/1;
        totalpaint = totalpaint - (onegallonneeded*1);
        totalpaint2 = totalpaint2 - (onegallonneeded*1);
        halfgallonneeded = (int) (halfgallonneeded + totalpaint/0.5);
        totalpaint = totalpaint - halfgallonneeded*0.5;
        quartneeded  = (int) (quartneeded + totalpaint/0.25);
        totalpaint = totalpaint - quartneeded*0.25;
        pintneeded = (int) (pintneeded + totalpaint/0.125);
        totalpaint = totalpaint - pintneeded*0.125;
        if (totalpaint > 0){
            halfpintneeded += 1;
        }
    // total price  
    totalprice = (fivegallonneeded * FIVEGALLONS) + (onegallonneeded * ONEGALLON) +
      (halfgallonneeded* HALFGALLON) 
            +(quartneeded * QUART) +(pintneeded * PINT) +(halfpintneeded * HALFPINT); 


    RetObject ret = new RetObject();
    // put stuff into the retObject
    return ret;
  }