Last active
January 4, 2016 00:34
-
-
Save irajhedayati/e68a542e667681208b85 to your computer and use it in GitHub Desktop.
Ericsson Corporation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ir.iraj.ericsson; | |
import ir.iraj.ericsson.exceptions.IllegalBasePayException; | |
import ir.iraj.ericsson.exceptions.OverWorkException; | |
public class EricssonCorporation { | |
/** | |
* Minimum legal base pay | |
*/ | |
private double minimumBasePay; | |
/** | |
* Maximum allowed work hours per week | |
*/ | |
private double maximumWorkedHours; | |
/** | |
* Construct total payment calculator | |
* @param minimumBasePay Minimum legal payment per hour | |
* @param maximumWorkedHours Maximum hours an employee allowed to work | |
*/ | |
public EricssonCorporation(double minimumBasePay, double maximumWorkedHours) { | |
this.minimumBasePay = minimumBasePay; | |
this.maximumWorkedHours = maximumWorkedHours; | |
} | |
/** | |
* Calculate total payment | |
* @param basePay Base pay | |
* @param hours Hours worked | |
* @return Total pay | |
* @throws IllegalBasePayException | |
* @throws OverWorkException | |
*/ | |
public double calcTotalPay(double basePay, double hours) | |
throws OverWorkException, IllegalBasePayException { | |
if(basePay < minimumBasePay) { | |
throw new IllegalBasePayException("Base pay " + basePay + " is less than legal value " + minimumBasePay); | |
} | |
if(hours > maximumWorkedHours){ | |
throw new OverWorkException("Corporation allows maximum " + maximumWorkedHours + " hours per week"); | |
} | |
return basePay * hours; | |
} | |
public static void main(String[] args){ | |
EricssonCorporation ericssonCorporation = new EricssonCorporation(8.00,60.00); | |
try { | |
System.out.println("Employee 1\t$7.50\t35" + ericssonCorporation.calcTotalPay(7.5,35)); | |
} catch (OverWorkException | IllegalBasePayException e) { | |
System.out.println(e.getMessage()); | |
} | |
try { | |
System.out.println("Employee 2\t$8.20\t47" + ericssonCorporation.calcTotalPay(8.2,47)); | |
} catch (OverWorkException | IllegalBasePayException e) { | |
System.out.println(e.getMessage()); | |
} | |
try { | |
System.out.println("Employee 3\t$10.00\t73" + ericssonCorporation.calcTotalPay(10.00,73)); | |
} catch (OverWorkException | IllegalBasePayException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ir.iraj.ericsson; | |
import ir.iraj.ericsson.exceptions.IllegalBasePayException; | |
import ir.iraj.ericsson.exceptions.OverWorkException; | |
import org.junit.Before; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
public class EricssonCorporationTest { | |
private EricssonCorporation ericssonCorporation; | |
@Before | |
public void setup(){ | |
ericssonCorporation = new EricssonCorporation(8.00,60.00); | |
} | |
@Test | |
public void testCalcTotalPay(){ | |
try { | |
assertEquals(ericssonCorporation.calcTotalPay(8.2,47),385.4,0.01); | |
} catch (OverWorkException e) { | |
e.printStackTrace(); | |
} catch (IllegalBasePayException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Test(expected = IllegalBasePayException.class) | |
public void testCalcTotalPayThrowsIllegalBasePayException() throws IllegalBasePayException { | |
try { | |
ericssonCorporation.calcTotalPay(7.5,35); | |
} catch (OverWorkException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Test(expected = OverWorkException.class) | |
public void testCalcTotalPayThrowsOverWorkException() throws OverWorkException { | |
try { | |
ericssonCorporation.calcTotalPay(10,73); | |
} catch (IllegalBasePayException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ir.iraj.ericsson.exceptions; | |
/** | |
* Exception throw whenever an employee works over than permitted hours | |
*/ | |
public class IllegalBasePayException extends Exception { | |
public IllegalBasePayException(String message){ | |
super(message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ir.iraj.ericsson.exceptions; | |
/** | |
* Exception throw whenever an employee works over than permitted hours | |
*/ | |
public class OverWorkException extends Exception { | |
public OverWorkException(String message){ | |
super(message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>ir.irajh.ericsson</groupId> | |
<artifactId>interview1</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>1.7</source> | |
<target>1.7</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment