如何总结对象时间并创建总计?

时间:2019-03-08 02:02:10

标签: java time invoke

该程序的目的是提示用户输入活动时间,该活动时间将打印运行总计,并在用户决定结束应用程序后最终以分钟和秒为单位返回总时间。我遇到的问题是了解如何通过在类型Itime对象上调用addTime方法来添加带有activityTime的totalTime。

  

驱动程序类

public class ActivityManager
{
public static void main(String[] args) 
{
    Itime totalTime, activityTime; // declare totalTime and activityTime of type Itime
    int minutes; double seconds; // user input values
    Scanner input = new Scanner (System.in); // to read user input

    // display purpose and author
    System.out.println ("This program tracks total time in minutes");  
    System.out.println ("and seconds for a series of activities."); 
    System.out.println ();  // print blank line 

    // specify format for input 
    System.out.println ("Enter activity time in minutes and" 
        + " seconds, all in a");   
    System.out.println ("single line with spaces in between.  Entering" 
        + " values" ); 
    System.out.println ("outside appropriate ranges will terminate"
        + " the program."); 
    System.out.println ();  // print blank line

    // create the totalTime object of type Itime with 0 minutes and 0.0 seconds
    totalTime = new Itime (0,0.0);
    System.out.println ("Total time so far is: "
        + totalTime.toString()); 
    System.out.println ();  // print blank line

    // prompt and read time for an activity 
    System.out.print ("Enter time for an activity: "); 
    minutes = input.nextInt(); 
    seconds = input.nextDouble();

    // Accumulate if appropriate 
    while (minutes >= 0 && seconds >= 0 && seconds < 60) {
        // create the activityTime object of type Itime with given minutes and seconds
        activityTime = new Itime (minutes, seconds);
        // add totalTime and activityTime and put the result in totalTime
        totalTime = totalTime.addTime(activityTime);
        System.out.println ("Total time so far is: " + totalTime.toString()); 
        System.out.println ();  // print blank line

        // prompt and read time for another activity
        System.out.print ("Enter time for an activity: ");   
        minutes = input.nextInt(); 
        seconds = input.nextDouble(); 
    }

    // wrap up and print final total
    System.out.println ("Sentinel received"); 
    System.out.println ();  // print blank line 
    System.out.println ("Total time so far is: "
                        + totalTime.toString()); 
    System.out.println ();  // print blank line

    // print closing remarks
    System.out.println ("Program has terminated."); 
    System.out.println ();  // print blank line 
}        

}

  

主类

public class Itime
{
private int minutes;
private double seconds;
/**
 * Constructer objects of class Itime
 */
public Itime (int minutes, double seconds)
{
    assert minutes >=0;
    assert seconds >=0 && seconds <60;

}
/**
 * Getter methods
 */
public int getMinutes()     { return this.minutes; }
public double getSeconds()  { return this.seconds; }

/**
 * Method to return time in String format
 */
public String toString ()
{
    String toString = minutes + " minutes and " + seconds + " seconds";
    return toString;
}

**public addTime (pass Itime objects as params here)**
{

}

}

2 个答案:

答案 0 :(得分:0)

尝试一下

  public Itime addTime(Itime itime) {
    this.minutes = this.minutes + itime.getMinutes();
    this.seconds = this.seconds + itime.getSeconds();
    if (this.seconds > 60) {
      this.seconds = this.seconds % 60;
      this.minutes++;
    }
    return this;
  }

答案 1 :(得分:0)

java.time中的持续时间类

Java有一段时间的内置类:Duration。因此,您实际上是在尝试重新发明轮子。我建议您不要这样做。使用Itime进行实施时,您的Duration类的外观如下:

public class Itime {
    private static final double NANOS_PER_SECOND = TimeUnit.SECONDS.toNanos(1);

    private final Duration dur;

    /**
     * Constructer objects of class Itime
     */
    public Itime(int minutes, double seconds) {
        if (minutes < 0) {
            throw new IllegalArgumentException("minutes must be >= 0");
        }
        if (seconds < 0 || seconds >= 60) {
            throw new IllegalArgumentException("seconds must be >= 0 and < 60");
        }
        dur = Duration.ofMinutes(minutes).plus(Duration.ofNanos(Math.round(seconds * NANOS_PER_SECOND)));
    }

    private Itime(Duration dur) {
        this.dur = dur;
    }

    // Getter methods

    public int getMinutes() {
        return Math.toIntExact(dur.toMinutes());
    }

    public double getSeconds() {
        return dur.toSecondsPart() + dur.toNanosPart() / NANOS_PER_SECOND;
    }

    /**
     * @return time in String format
     */
    public String toString() {
        String toString = getMinutes() + " minutes and " + getSeconds() + " seconds";
        return toString;
    }

    public Itime addTime(Itime other) {
        return new Itime(dur.plus(other.dur));
    }

}

您使用的第二个构造方法使您的addTime方法成为了一种方法。 Duration类负责所有时间的数学运算。这是一件好事,因为它写起来有点繁琐且容易出错。

使用此实现,以下是您的驱动程序类的摘录:

Enter time for an activity: 91 43.21
Total time so far is: 91 minutes and 43.21 seconds

Enter time for an activity: 12 20
Total time so far is: 104 minutes and 3.21 seconds

Enter time for an activity: -1 -1
Sentinel received

注意事项:

  • 由于Duration不直接接受或作为double给我们几秒钟,因此很遗憾,我不得不在构造函数和getSeconds方法中加入一些数学运算。
  • 我声明了dur字段final,因为我发现不需要对其进行修改(您可以类似地在实现final中声明这两个字段)。
  • assert构造函数中使用public来验证参数被认为是较差的样式。相反,我使用if并抛出适当的IllegalArgumentException。 (但是参数验证很好; assert总比没有好。)
  • 因为我们需要将Itime传递给addTime方法,并从其中获取一个Itime。该方法必须使用类型为Itime的一个参数和返回类型为Itime的{​​{1}}来声明。

替代设计:放下Duration而不是Itime

在上述设计中,public Itime addTime(Itime other)实际上已成为Itime的包装。这种设计有其优点和缺点。相反,您可以完全放弃Duration类并直接使用Itime。优点:

  • 设计更简单,少一类。
  • 您可以将用户输入修改为Duration为您解析的格式。然后,我们将不再需要任何数学运算即可将Duration.parse转换为double可以理解的内容。

骗局

  • 您的程序似乎是围绕您的Duration类设计的,在某些方面Itime不太适合。具体来说,您可能想编写一种辅助方法,用于将Duration转换为类似Duraiton的字符串。

链接

Oracle tutorial: Date Time。尤其请参见Period and Duration部分。