Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package school.lemon.changerequest.java.Employees;

/**
* Created by lbrdev on 04.01.2017.
* Project: oop.pr1
*/
public class Accountant extends Employee {
private static int countAccountant;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this field?


public Accountant(String name) {
super(name);
}

@Override
public double calculateSalary() {
if (getRatioOfHours() >= 1.) {
return getSalary();
}
return getRatioOfHours() * getSalary();
}

public int overallSalary(Employee[] employees) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to store array/list of employees in the field and do not pass them through argument.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this method returns integer?

double i = 0.;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i is not the best name for this purpose.

for (Employee employee : employees) {
i += employee.calculateSalary();
}
return (int) i;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package school.lemon.changerequest.java.Employees;

/**
* Created by lbrdev on 04.01.2017.
* Project: oop.pr1
*/
public class Employee {
private String name;
private int salary;
private int hours;
public static final int DEFAULT_HOURS = 160;
public static final int DEFAULT_SALARY = 100;


public Employee(String name) {
this.name = name;
}

public String getName() {
return name;
}

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}

public int getHours() {
return hours;
}

public void setHours(int hours) {
this.hours = hours;
}

public double getRatioOfHours() {
return (double) getHours() / DEFAULT_HOURS;
}

public double calculateSalary() {
return getRatioOfHours() * getSalary();
}

@Override
public String toString() {
return "Current month salary of " + getName() + " is " + calculateSalary();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package school.lemon.changerequest.java.Employees;

/**
* Created by lbrdev on 04.01.2017.
* Project: oop.pr1
*/
public class Manager extends Employee {

public Manager(String name) {
super(name);
}

@Override
public double calculateSalary() {
if (getRatioOfHours() >= 1.) {
return getSalary();
}
return getRatioOfHours() * getSalary();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package school.lemon.changerequest.java.Employees;

/**
* Created by lbrdev on 04.01.2017.
* Project: oop.pr1
*/
public class Programmer extends Employee {

public Programmer(String name) {
super(name);
}

@Override
public double calculateSalary() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you override method with exactly the same implementation? I'd prefer to make base method abstract.

return getRatioOfHours() * getSalary();
}
}
47 changes: 47 additions & 0 deletions src/main/java/school/lemon/changerequest/java/Employees/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package school.lemon.changerequest.java.Employees;

/**
* Created by lbrdev on 04.01.2017.
* Project: oop.pr1
*/
public class Test {
public static void main(String[] args) {
//
Employee[] employee = new Employee[6];

employee[0] = new Programmer("One");
employee[0].setSalary(Employee.DEFAULT_SALARY);
employee[0].setHours(80);

employee[1] = new Programmer("Two");
employee[1].setSalary(Employee.DEFAULT_SALARY);
employee[1].setHours(320);

employee[2] = new Accountant("Three");
employee[2].setSalary(Employee.DEFAULT_SALARY);
employee[2].setHours(80);

Accountant accountant = new Accountant("Four");
accountant.setSalary(Employee.DEFAULT_SALARY);
accountant.setHours(320);
employee[3] = accountant;

employee[4] = new Manager("Five");
employee[4].setSalary(Employee.DEFAULT_SALARY);
employee[4].setHours(80);

employee[5] = new Manager("Six");
employee[5].setSalary(Employee.DEFAULT_SALARY);
employee[5].setHours(320);

System.out.println("Employees salary is " + accountant.overallSalary(employee));
System.out.printf("Programmer %4$s with salary %1$d worked %2$d hours - salary is %3$d \n", employee[0].getSalary(), employee[0].getHours(), (int) employee[0].calculateSalary(), employee[0].getName());
System.out.printf("Programmer %4$s with salary %1$d worked %2$d hours - salary is %3$d \n", employee[1].getSalary(), employee[1].getHours(), (int) employee[1].calculateSalary(), employee[1].getName());
System.out.printf("Accountant %4$s with salary %1$d worked %2$d hours - salary is %3$d \n", employee[2].getSalary(), employee[2].getHours(), (int) employee[2].calculateSalary(), employee[2].getName());
System.out.printf("Accountant %4$s with salary %1$d worked %2$d hours - salary is %3$d \n", employee[3].getSalary(), employee[3].getHours(), (int) employee[3].calculateSalary(), employee[3].getName());
System.out.printf("Manger %4$s with salary %1$d worked %2$d hours - salary is %3$d \n", employee[4].getSalary(), employee[4].getHours(), (int) employee[4].calculateSalary(), employee[4].getName());
System.out.printf("Manger %4$s with salary %1$d worked %2$d hours - salary is %3$d \n", employee[5].getSalary(), employee[5].getHours(), (int) employee[5].calculateSalary(), employee[5].getName());


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package school.lemon.changerequest.java.FractionNumber;

/**
* Created by lbrdev on 04.01.2017.
* Project: oop.pr1
*/
public class FractionNumber {
private int dividend;
private int divisor;
private static int DEFAULT_DIVISOR = 1;
public static final FractionNumber ONE = new FractionNumber(1);
public static final FractionNumber ZERO = new FractionNumber(0);

public FractionNumber(int dividend, int divisor) {
if (divisor == 0) throw new IllegalArgumentException("divisor must not be zero");
if (dividend == 0) {
this.dividend = dividend;
this.divisor = DEFAULT_DIVISOR;
} else {
this.dividend = dividend;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to assign these fields. They will be overridden on the next few lines.

this.divisor = divisor;
int nod = gcd(this.dividend, this.divisor);
this.dividend = dividend / nod;
this.divisor = divisor / nod;
}
}

private int gcd(int dividend, int divisor) {
int maxValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to assign values like:

 int maxValue = dividend;
 int minValue = divisor;

And remove one condition.

int minValue;
int nod;
if (dividend > divisor) {
maxValue = dividend;
minValue = divisor;
} else {
maxValue = divisor;
minValue = dividend;
}
for (nod = maxValue; nod >= minValue; nod--) {
if (dividend % nod == 0 & divisor % nod == 0)
return nod;
}
return 1;
}


public FractionNumber(int dividend) {
this.dividend = dividend;
this.divisor = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use default value instead of 1

}

public int getDividend() {
return this.dividend;
}

public int getDivisor() {
return this.divisor;
}

public double decimalValue() {
return ((double) dividend) / ((double) divisor);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only one cast will be enough.

}

@Override
public String toString() {
return "" + dividend + "/" + divisor;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package school.lemon.changerequest.java.FractionNumber;

/**
* Created by lbrdev on 04.01.2017.
* Project: oop.pr1
*/
public class FractionNumberOperations {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move all these operations inside the FractionNumber class.

public FractionNumber add(FractionNumber a, FractionNumber b) {

if (a.getDivisor() == b.getDivisor()) {
return new FractionNumber(a.getDividend() + a.getDividend(), a.getDivisor());
} else {
int tmp = 1;

while (tmp <= a.getDivisor() * b.getDivisor()) {
if (tmp % a.getDivisor() == 0 && tmp % b.getDivisor() == 0) {
break;
}
tmp++;
}

int i = a.getDividend() * (tmp / a.getDivisor());
int j = b.getDividend() * (tmp / b.getDivisor());

return new FractionNumber(i + j, tmp);
}
}

public FractionNumber div(FractionNumber a, FractionNumber b) {
return new FractionNumber(a.getDividend() * b.getDivisor(), b.getDividend() * a.getDivisor());
}

public FractionNumber mul(FractionNumber a, FractionNumber b) {
return new FractionNumber(a.getDividend() * b.getDividend(), a.getDivisor() * b.getDivisor());
}

public FractionNumber sub(FractionNumber a, FractionNumber b) {

if (a.getDivisor() == b.getDivisor()) {
return new FractionNumber(a.getDividend() - b.getDividend(), a.getDivisor());
} else {
int tmp = 1;

while (tmp <= a.getDivisor() * b.getDivisor()) {
if (tmp % a.getDivisor() == 0 && tmp % b.getDivisor() == 0) {
break;
}
tmp++;
}

int i = a.getDividend() * (tmp / a.getDivisor());
int j = b.getDividend() * (tmp / b.getDivisor());

return new FractionNumber(i - j, tmp);
}

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package school.lemon.changerequest.java.FractionNumber;

/**
* Created by heroy on 04.01.2017.
*/
public class TestFractionNumber {
public static void main(String[] args) {
FractionNumberOperations operation = new FractionNumberOperations();
FractionNumber number1 = new FractionNumber(1, 4);
FractionNumber number2 = new FractionNumber(5, 1);
System.out.println("GetNumber 1 = " + number1);
System.out.println("GetNumber 2 = " + number2);
System.out.println("Separately - " + number1.getDividend() + " and " + number1.getDivisor());
System.out.println("To string - " + number1.toString());
System.out.println("Object ONE and ZERO - " + FractionNumber.ONE + " " + FractionNumber.ZERO);
System.out.println("With only dividend - " + new FractionNumber(5));
System.out.println("Decimal Value = " + number1.decimalValue());
System.out.println("Num1*Num2= " + operation.mul(number1, number2));
System.out.println("Num1/Num2= " + operation.div(number1, number2));
System.out.println("Num1+Num2= " + operation.add(number1, number2));
System.out.println("Num1-Num2= " + operation.sub(number1, number2));
}
}