Skip to content

Instantly share code, notes, and snippets.

@DinisCruz
Created May 30, 2016 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DinisCruz/b793d581bd487da20254bd54a5ffb54f to your computer and use it in GitHub Desktop.
Save DinisCruz/b793d581bd487da20254bd54a5ffb54f to your computer and use it in GitHub Desktop.
Elegant Objects code snippets
//executed in https://www.compilejava.net/
public class CashTest
{
public static void main(String[] args)
{
System.out.println(new Cash(12).Value());
System.out.println(new Cash("12").Value());
// System.out.println(new Cash("12.12f").Value()); // doesn't work and throws exception
System.out.println(new Cash(12.12f).Value());
}
}
public class Cash
{
private int dollars;
Cash(float dlr)
{
this((int) dlr);
}
Cash(String dlr)
{
this.dollars = Integer.parseInt(dlr);
}
Cash(int dlr)
{
this.dollars = dlr;
}
public int Value()
{
return this.dollars;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment