|
|
Java Question
|
|
|
|
Mac Elite
Join Date: Jun 2004
Status:
Offline
|
|
Hi,
I recently started learning java, and I have written a few programs. I was looking to inhace a program called work that calculates how much I earn in a given day or week. I was wondering if theres a code for java that allows you to subtract time values. For example, if I want to make it ask if I know how many hours I worked (lets say 6:00 PM-11:00PM), could I get it to figure out how many hours that is? (And also work with A.M. values). The program is written underneath (sorry if it's not indented correctly, I'm not quite sure how to do that either yet.) Please let me know if anyone knows a solution to the time thing!
Thanks,
-Brandon
__________________________________________________ ____________
/*The DummiesIO.get_______ is something the author of the java
* dummies book I'm reading wrote for input and stuff. Also this is
* one of my first programs so go easy on me!.
*/
import java.text.NumberFormat;
class work
{
public static void main(String args[])
{
double rate, hours, gross, net, tax, tips, total;
char reply;
NumberFormat currency =
NumberFormat.getCurrencyInstance();
String niceTotal1;
String niceTotal2;
String niceTotal3;
String niceTotal4;
String niceTips;
System.out.println();
System.out.print("How many hours did you work? ");
tips=0;
rate=5.50;
hours=DummiesIO.getDouble();
gross=rate*hours;
tax=gross*.07636363636363636;
net=gross-tax;
System.out.println();
System.out.println("Do you know how much you earned in tips?");
System.out.print("(Y/N) ");
reply = DummiesIO.getChar();
if (reply=='y' || reply=='Y')
{
System.out.println("How much?");
tips=DummiesIO.getDouble();
total=net+tips;
niceTotal1 = currency.format(gross);
niceTotal2 = currency.format(tax);
niceTotal3 = currency.format(net);
niceTotal4 = currency.format(total);
niceTips = currency.format(tips);
System.out.print("--------------------");
System.out.println("--------------------------");
System.out.print("Hours\tTips\tGross");
System.out.println("\tTax\tNet Pay\tTotal");
System.out.print(hours);
System.out.print("\t");
System.out.print(niceTips);
System.out.print("\t");
System.out.print(niceTotal1);
System.out.print("\t");
System.out.print(niceTotal2);
System.out.print("\t");
System.out.print(niceTotal3);
System.out.print("\t");
System.out.println(niceTotal4);
System.out.print("-------------------");
System.out.println("---------------------------");
}
else
{
niceTotal1 = currency.format(gross);
niceTotal2 = currency.format(tax);
niceTotal3 = currency.format(net);
System.out.println("-------------------------------");
System.out.print("Hours\tGross");
System.out.println("\tTax\tNet Pay");
System.out.print(hours);
System.out.print("\t");
System.out.print(niceTotal1);
System.out.print("\t");
System.out.print(niceTotal2);
System.out.print("\t");
System.out.println(niceTotal3);
System.out.println("-------------------------------");
}
}
}
--------------------------------------------------------------
Thanks again"
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jun 2004
Status:
Offline
|
|
I forgot to mention that if you woul like to run the program, you can go to the author's website and click either "Download all the code" or "Download DummiesIO.java" and put it in a folder along with the code above and the run it from the terminal
thanks!
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
The best thing to do is store the values as dates (GregorianCalendar). Create a calendar with the start time. Create another calendar with the end time. You can subtract the differences by calling getTimeInMillis() on each calendar and doing the subtraction to get the number of milliseconds between them.
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jun 2004
Status:
Offline
|
|
Can I have an example of what you mean?
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Originally posted by brandon420506:
Can I have an example of what you mean?
Code:
import java.util.GregorianCalendar;
public class SampleClass
{
public SampleClass()
{
//int year, int month, int date, int hour, int minute
GregorianCalendar start_time = new GregorianCalendar(2004,07,12,8,00);
GregorianCalendar end_time = new GregorianCalendar(2004,07,12,18,00);
long start_time_ms = start_time.getTimeInMillis();
long end_time_ms = end_time.getTimeInMillis();
double time_in_minutes = (end_time_ms - start_time_ms)/(1000*60);
double time_in_hours = time_in_minutes/60;
System.out.println(time_in_minutes + " minutes");
System.out.println(time_in_hours + " hours");
}
public static void main(String[] args)
{
SampleClass sc = new SampleClass();
}
}
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|