Results 1 to 17 of 17
- 03-08-2011, 02:13 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
User input int value that translate to hours...
Sorry for being unclear, I will do my best to explain my thoughts. I am building an app that takes user input (as ints), and let him define a work shift, something that goes like:
please tell me when do you begin work?
_________________
(the user writes) 7
when do you finish?
(again user) 12
_________________
Now, I need some way to diffreniate between 12AM and 00PM, making sure the user will input the right values. Something that will only accept values that correspond to day/night hours. Otherwise if the user enters 7 as a starting hour and 2 as the ending hour, well... you know where this is going :)
Can someone please give me an hint on which way to go about this?
- 03-08-2011, 02:31 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You could swap to using military time. You can also consider asking them whether they start in the am or pm, and use that to determine the number. You could also determine the distance from n1 - 12, and add it to n2
so for 7-2 would be
Java Code:(12 - 7) + 2
- 03-08-2011, 02:37 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
I have actually thought about the first two suggestions and thinking about the military time option. If I go for that option I might get values entered such as 15:45. How will I convert these kind of values to something I could run calcs on?
I mean how can I make 15:45 == 15 3/4?
I'm a real noob in programming, but I'm learning :)
- 03-08-2011, 02:47 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
If you do 45 / 60 what do you get? What do you get with 3 / 4? If you found a way to not use military time, how would you compare 2:45pm to 2 3/4?
I think the easiest would be to do 12 - n1 + n2.
That will produce the number of hours you want, 10. This is a pretty easy conversion as well.Java Code:n1 = 7; n2 = 5; n3 = 12 - n1 + n2; System.out.println(n3);
I honestly feel this would be the best implementation for what you want.
- 03-08-2011, 02:49 AM #5
If you get 15:45 as input then it has to be a String. Then you can use the split method to get the hours and minutes.
- 03-08-2011, 02:59 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
OK, thats seems to work for finding the amount of hours, so now just let me add a new factor - I also want the user to enter an hour from which he starts to earn 150% compared to a regular hour.
I'm guessing I will now add to the code "n4" that represnt the 150% starting hour, something like this:
n1 = 7;
n2 = 5;
n2.5 = 3; //User starts to earn 150% compared to other hours
n3 = 12 - n1 + n2;
n4 = 12 - n2.5;
System.out.println(n3);
is this right?
- 03-08-2011, 03:00 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
- 03-08-2011, 03:06 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
not entirely.
Lets say they begin work at 7 and they work till 7 and you want to start 150% pay at 3. will then solve this by finding how many hours they worked until 3. How would you do that? Do that for me and give an answer and we can move onto the next step.
http://www.java-forums.org/java-tips...-function.htmlLast edited by sunde887; 03-08-2011 at 03:08 AM.
- 03-08-2011, 03:14 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
- 03-08-2011, 03:22 AM #10
- 03-08-2011, 03:32 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Thats not what I was looking for. If someone works 7 - 7, you first want to find how many hours from 7 - time and a half time, in my example, till 3. Using the method I showed you, how many hours would 7 - 3 be?
- 03-08-2011, 03:37 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
- 03-08-2011, 03:43 AM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I am glad to be patient. Not much else going on tonight and I want to help out.
What time do you want to start 150%? Lets say its 3. How would you determine the hours from 7 - 3?
- 03-08-2011, 03:57 AM #14
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
Assuming the shift is from 7 till 7, I was thinking about subtracting 3 from 7 (the end of shift or say, endOfShift - shift150). This should get me 4 hours of 150% pay.
Now, if I already found out that by entering startOfShift = 7 and endOfShift = 7, the user meant a 12 hours shift, then I would only need to: totalHoursOfShift - 4, to find the remaining regular 100% pay hours.
Maybe a little more info will help us...
At the end I will want to find based on a few inputs from user like:
payHourly // user enters how much he earns for each regular hour
startOfShift
endOfShift
shift150
Based on these parameters it will calculate how much money the user have earned in this shift including both 100% and 150% hours pay.Last edited by yosi199; 03-08-2011 at 04:04 AM.
- 03-08-2011, 04:04 AM #15
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are correct. I was thinking of solving how many hours from start until the hour where they get time and a half.
If they begin making time and a half at 3, then 7 - 3 = 12 - 7 + 3 = 8 + 7 - 3(end - time and a half time) = 4 = 12.
With this method you can find out that 8 hours will earn normal pay and 4 will earn time and a half. Then you can do
payrate * 8 +((payrate * 4) * 1.5)
- 03-08-2011, 04:18 AM #16
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
I already manged to do all the calcs and get the info I want. My problem is that I have to make sure somehow that I wont have to deal with one user inputting:
startOfShift = 7
endOfShift = 7
shift150 = 3
and then another user will come and write it in a totally different way:
startOfShift = 19:00
endOfShift = 7
shift150 = 3
My original questions was whats the best way to deal with these? Should I lock the user to one explictly defined way of inputting? or maybe letting them have a free hand and I will try compensate by covering a few ways the user can enter the shift details?
I think I will have to learn the split method cause at the end I will to be quite accurate with the user's input so they could write accurate shifts like:
start at 2:34
end at 00:14
and such...
- 03-08-2011, 04:30 AM #17
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Since it's your program you can decide this, however; it is probably better to have more flexible code which makes the user have a better experience.
A new idea I had was for you to create a time class. You can create a constructor which can set up the correct time based on military or regular time, and also convert between military and regular.
something like
you can also add more methods of your own. This way you can create a time object and make sure they are both using the same format then calculate the difference.Java Code:class Time{ int hours; int minutes; boolean am; //constructor for military Time(); //constructor for regular Time(); //convert to military public Time toMilitary() //convert to regular public Time toRegular() //difference public Time difference(Time t) public String toString() }
Similar Threads
-
Help with user input
By sconniegorilla in forum New To JavaReplies: 2Last Post: 02-16-2011, 02:00 PM -
Need help getting input(first/last name) from user
By nightrise420 in forum New To JavaReplies: 11Last Post: 09-11-2010, 03:09 AM -
how to get input from User
By Alvaro in forum New To JavaReplies: 7Last Post: 01-15-2010, 11:02 PM -
User input- Pop Up Box
By dedachi in forum AWT / SwingReplies: 3Last Post: 03-23-2009, 04:47 AM -
cant take input from user
By new_1 in forum New To JavaReplies: 6Last Post: 12-25-2007, 07:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks