Answer the question
In order to leave comments, you need to log in
I solved the problem in Java, but I'm a beginner, and my solution is "naive", maybe someone has better options?
I am new to Java but I am trying very hard. Here is a task in Java:
Write an evaluate method: there is an office and a person who is hired, the office can offer X money, and the person wants Y money, there should be two classes, an office and a person, in each parameter: the name of the office \ person, the amount of money asks \ can offer, and so that if a person asks for less or as much as the office can offer, it will be deduced whether the office can hire him ("We can hire Petya") or not.
package com.company;
public class Task {
public static void main (String [] args) {
Employer google = new Employer("Google", 500);
google.displayInfo();
Employee vasya = new Employee("Vasya", 400);
vasya.displayInfo();
evaluate (); {
if (google.salaryProposed >= vasya.salaryDesired) {
System.out.println("\nWe can hire Vasya! :)");
} else {
System.out.println("\nWe can't :(");
}
}
}
private static void evaluate() {
}
}
class Employer {
String name;
int salaryProposed;
Employer (String name, int salaryProposed) {
this.name = name;
this.salaryProposed = salaryProposed;
}
void displayInfo () {
System.out.printf ("Employer name: %s \t Employer salary proposed: %d \n", name, salaryProposed);
}
}
class Employee {
String name;
int salaryDesired;
Employee (String n, int sD) {
name = n;
salaryDesired = sD;
}
void displayInfo () {
System.out.printf ("Employee name: %s \t Employee salary desired: %d \n", name, salaryDesired);
}
}
Answer the question
In order to leave comments, you need to log in
Здравствуйте!
1) метод displayInfo()
можно заменить на метод toString()
так как по факту он именно это и делает.
2) Имеется синт. ошибка в коде и не совсем понятен смысл
evaluate (); {
if (google.salaryProposed >= vasya.salaryDesired) {
System.out.println("\nWe can hire Vasya! :)");
} else {
System.out.println("\nWe can't :(");
}
}
private static void evaluate() {
}
public class Main {
public static void main(String[] args) {
Employer google = new Employer("Google", 500);
System.out.println(google);
Employee vasya = new Employee("Vasya", 400);
System.out.println(vasya);
System.out.println("Result: " + SalaryService.evaluate(google.salaryProposed, vasya.salaryDesired));
}
}
class Employer {
String name;
int salaryProposed;
Employer (String name, int salaryProposed) {
this.name = name;
this.salaryProposed = salaryProposed;
}
@Override
public String toString() {
return "Employer{" +
"name='" + name + '\'' +
", salaryProposed=" + salaryProposed +
'}';
}
}
class Employee {
String name;
int salaryDesired;
Employee (String n, int sD) {
name = n;
salaryDesired = sD;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salaryDesired=" + salaryDesired +
'}';
}
}
class SalaryService {
public static String evaluate(int salaryProposed, int salaryDesired) {
if (salaryProposed >= salaryDesired) {
return "We can hire Vasya! :)";
} else {
return "We can't :(";
}
}
}
azerphoenix , Jacen11 спасибо за ответ. Прочитала про модификаторы доступа, инкапсуляцию, геттеры/сеттеры, добавила в код. Работает, но я не знаю, насколько это правильно сделано. Это задача с собеседования, я запомнила условия. Из того, что я помню "железно", условия которые должны быть соблюдены: должен быть метод evaluate, он должен выводить в консоль можем/не можем нанять человека, он должен определять, что желаемая зарплата не больше предлагаемой. Должно быть 2 класса: компания и человек. У каждого класса обязательно должны быть параметры: человек - Name, желаемая зарплата, компания - Name, предлагаемая зарплата. Там так и было написано: имена переменных у компании и человека совпадали, Name.
public class Task {
public static void main (String [] args) {
Employer company = new Employer("Godel", 500);
company.setCompanyName("Microsoft");
company.setCompanyName("EPAM");
company.setSalaryProposed(600);
company.setCompanyName("CoolCompany");
company.setSalaryProposed(1000);
Employee person = new Employee("Vasya", 400);
person.setPersonName("Petya");
person.setSalaryDesired(300);
person.setPersonName("Ivan");
person.setSalaryDesired(800);
SalaryService1.evaluate(company.getSalaryProposed(), person.getSalaryDesired(), company.getCompanyName(), person.getPersonName());
}
}
class Employer {
private String name;
private int salaryProposed;
public Employer (String companyName, int salaryProposed) {
this.name = companyName;
this.salaryProposed = salaryProposed;
}
public String getCompanyName () {
return this.name;
}
public void setCompanyName (String companyName) {
this.name = companyName;
}
public int getSalaryProposed () {return this.salaryProposed;}
public void setSalaryProposed (int salaryProposed) {this.salaryProposed = salaryProposed;}
}
class Employee {
private String name;
private int salaryDesired;
public Employee (String personName, int salaryDesired) {
this.name = personName;
this.salaryDesired = salaryDesired;
}
public String getPersonName () {
return this.name;
}
public void setPersonName (String personName) {
this.name = personName;
}
public int getSalaryDesired () {return this.salaryDesired;}
public void setSalaryDesired (int salaryDesired) {this.salaryDesired = salaryDesired;}
}
class SalaryService1 {
public static void evaluate (int salaryProposed, int salaryDesired, String companyName, String personName) {
System.out.println("Company name: " + companyName + "\tSalary proposed: " + salaryProposed + "\nPerson name: " + personName + "\tSalary desired: " + salaryDesired);
if (salaryProposed >= salaryDesired) {
System.out.println("\nWe can hire " + personName + "! :)");
} else {
System.out.println("\nWe can't :(");
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question