Skip to content

Instantly share code, notes, and snippets.

@TrevorBasinger
Last active August 29, 2015 14:14
Show Gist options
  • Save TrevorBasinger/8e4c3678a2e268070d32 to your computer and use it in GitHub Desktop.
Save TrevorBasinger/8e4c3678a2e268070d32 to your computer and use it in GitHub Desktop.
/**
* @kinseybasinger
* @chapter3
*/
import java.util.Scanner;
public class Salary
{
public static void main(String[] args)
{
/* Lets init the scanner first.
* You only need one because you can reuse. */
Scanner consoleIn = new Scanner(System.in);
System.out.println("Hourly pay rate:");
double rate = consoleIn.nextDouble();
System.out.println("Regular hours worked");
int hours = consoleIn.nextInt();
System.out.println("Overtime hours worked");
int overtime = consoleIn.nextInt();
//System.out.println("hai! :3"); - Brief coding interlude. Testing to make sure my code wasn't broken
int hoursTotal = hours + overtime;
System.out.println("Total hours worked: " + hoursTotal);
System.out.println("Regular Wages: $" + rate * hours);
// Moving overtime calculations into another func is cool.
// Keep in mind you can pass any amount of arguments in.
// I pass in the standard rate first, then the amount of hours
// worked overtime.
double overTimeTotal = overTimeCalc(rate, overtime);
System.out.println("Overtime Wages: $" + overTimeTotal);
double wages = rate * hours + overTimeTotal;
System.out.println("Total Wages Earned: $" + wages);
}
/**
* I modified this method a little bit. It's better to "separate concerns"
* because it allows you to make things more modular and reusable. The method
* should only concern itself with making a calculation, not printing anything
* to screen. That way, you can reuse it without the side effect of printing a
* value to the screen. You can also compose it with other functions safely.
* When I say compose I mean you can chain functions like this: g (f (x))
*
* It also leads to better maintainablility and make things easier to reason
* about.
*
* Johnathon writes code that does what Suzie expects it to do. Now Suzie doesn't
* have to go home and shoot up in front of her kids to numb the pain.
*
* Function etiquette: Super simple stuff.
*/
public static double overTimeCalc (double rate, int hours)
{
// Generally, I think overtime is time and a half.
// You can adjust accordingly.
// rate * .5 gets the additional monies
// then we add additional monies to regular monies
// then multiply by hours to get all the monies
return ((rate * .5) + rate) * hours;
}
}
/**
* The difference between great programmers and mediocre ones is that great
* programmers strive to write clear, readable code. This allows them to clearly
* undestand the problem and formulate a simple elegant solution.
*
* I promise you that once you really start writing large chunks of code, you'll
* start to look back on your old stuff and won't be able to recognize it. My old
* code seems completely foreign to me a lot of the time.
*
* I know that your teacher expects you to write a certain way, but keep in mind:
*
* Comments don't make actual code easier to read or understand.
* Comments have to be maintained as well.
*
* Code can be written to be easily understood.
* Using good variable names and function names helps.
* Breaking things in to small chunks and separating concerns helps.
*
* I know your hands are tied with the way the teacher wants you to write code,
* but I figured you could file this away for later.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment