Permalink
Browse files

lab 2

  • Loading branch information...
1 parent 44a0312 commit 0857874b423e35c01c1de4ebd6c19e53a68f76f8 @duguyue100 committed Feb 29, 2012
Showing with 124 additions and 0 deletions.
  1. +104 −0 lab_answer/lab2/Time24.java
  2. +20 −0 lab_answer/lab2/TimeCard.java
@@ -0,0 +1,104 @@
+import java.text.DecimalFormat;
+import java.util.Scanner;
+
+
+public class Time24 {
+ private int hour;
+ private int minute;
+
+
+ private void normalizeTime()
+ {
+ int extraHours=minute/60;
+
+ minute%=60;
+ hour=(hour+extraHours%24);
+ }
+
+ public Time24()
+ {
+ hour=0;
+ minute=0;
+ }
+
+ public Time24(int h, int m)
+ {
+ hour=h;
+ minute=m;
+ normalizeTime();
+ }
+
+ public void addTime(int m)
+ {
+ if (m<0)
+ {
+ throw new IllegalArgumentException("Time24.setTime: "+ "argument must be a positive integer");
+ }
+
+ minute+=m;
+ normalizeTime();
+ }
+
+ public Time24 interval(Time24 t)
+ {
+ int currTime=hour*60+minute;
+ int tTime=t.hour*60+t.minute;
+
+ if (tTime<currTime)
+ tTime+=24*60;
+
+ return new Time24(0,tTime-currTime);
+ }
+
+ public int getHour()
+ {
+ Scanner input=new Scanner(System.in);
+ return input.nextInt();
+ }
+
+ public int getMinute()
+ {
+ Scanner input=new Scanner(System.in);
+ return input.nextInt();
+ }
+
+ public void setTime(int h, int m)
+ {
+ if (h<0||m<0)
+ {
+ throw new IllegalArgumentException("Time24.setTime: "+"parameters must be positive integers");
+ }
+
+ this.hour=h;
+ this.minute=m;
+
+ normalizeTime();
+ }
+
+ public String toString()
+ {
+ DecimalFormat fmt=new DecimalFormat("00");
+ return new String(hour+":"+fmt.format(minute));
+ }
+
+ public static Time24 praseTime(String s)
+ {
+ String hours="";
+ String minutes="";
+
+ int k=0;
+ while (s.charAt(k)!=':')
+ {
+ hours=hours+s.charAt(0);
+ k++;
+ }
+ k++;
+ while (k<s.length())
+ {
+ minutes=minutes+s.charAt(k);
+ k++;
+ }
+
+ return new Time24(Integer.parseInt(hours),Integer.parseInt(minutes));
+ }
+}
@@ -0,0 +1,20 @@
+
+public class TimeCard {
+ private String workerID;
+ private double payrate;
+ private Time24 punchInTime, punchOutTime;
+
+ public TimeCard(String id, double pr, int punchInHour, int punchInMinute)
+ {
+ workerID=id;
+ payrate=pr;
+ punchInTime.setTime(punchInHour, punchInMinute);
+ }
+
+ public String payWorker(int punchOutHour, int punchOutMinute)
+ {
+ punchOutTime.setTime(punchOutHour, punchOutMinute);
+ Time24 result=punchOutTime.interval(punchInTime);
+ return result.toString();
+ }
+}

0 comments on commit 0857874

Please sign in to comment.