Pracownicy (zadanie)

0

Cześć, jestem początkującym programistą i mam kłopot z jednym zadaniem do szkoły.

Chcę wykorzystać klasę-konstruktor pracownika którą napisałem:

public class Employee 
{
	private String firstName;
	private String lastName;
	private int age;
	private double salary;
	
	public Employee()
	{
		
	}
	
	public Employee(String firstName, String lastName, int age, double salary)
	{
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
		this.salary = salary;
	}
	
	public String getFirstName()
	{
		return firstName;
	}
	
	public String getLastName()
	{
		return lastName;
	}
	
	public int getAge()
	{
		return age;
	}
	
	public double getSalary()
	{
		return salary;
	}
	
	public double setSalary(double setSalary)
	{
		salary = setSalary;
		return salary;
	}
	
	public double chgSalary(double chgSalary)
	{
		salary = (salary + ((chgSalary / 100.0) * salary));
		return salary;
	}
	
	public String toString()
	{
		return firstName + " " + lastName + ", " + age + ", " + salary;
	}
	
}

Do drugiej części zadania, tak żeby program pytając o te dane sam stworzył obiekt:

public class EmpManager 
{
	public Employee defEmp(String msg)
	{
		Employee pracownik = new Employee(){};
			
		JOptionPane.showMessageDialog(null,msg);
	      
	    String firstName = JOptionPane.showInputDialog("Podaj imię pracownika");
	    pracownik.firstName = firstName;
	    if (firstName == null) firstName = "";
	         
	    String lastName = JOptionPane.showInputDialog("Podaj nazwisko pracownika");
	    pracownik.lastName = lastName;
	    if (lastName == null) lastName ="";
	    
	    String age = JOptionPane.showInputDialog("Podaj wiek pracownika");
	    pracownik.age = Integer.parseInt(age);
	    if (age == null) age = "";
	    
	    String pe = JOptionPane.showInputDialog("Podaj pensje pracownika");
	    pracownik.salary = Double.parseDouble(pe);
	    if (salary == null) salary = "";
	            
	    return pracownik;
	    
	}
	   public void main(String[]argos)
	   {
	      Employee pracownik = new Employee();
	      pracownik.defEmp("podaj dane pracownika");
	      System.out.println(pracownik);
	   }
}

Na razie program nie dostrzega tych elementów w nawiasie które definiują pola. Pomocy!

2

zastąp Twój kod tym:


public class Employee {

    private String firstName;
    private String lastName;
    private int age;
    private double salary;

    public Employee() {
    }

    public Employee(String firstName, String lastName, int age, double salary) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.salary = salary;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

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

    public double changeSalary(double percent) {
        return salary = salary + raiseSalaryByPercent(percent);
    }

    private double raiseSalaryByPercent(double percent){
        return (percent / 100.0) * this.salary;
    }
}

0

Java ciągle chce usunąć private z tego kodu ale dzięki za kod chyba jest trochę lepszy od mojego ;)

1

bo musiałbyś zmienić jeszcze tą klasę:

 public Employee defEmp(String msg) {
        Employee pracownik = new Employee() {
        };

        JOptionPane.showMessageDialog(null, msg);

        String firstName = JOptionPane.showInputDialog("Podaj imię pracownika");
        pracownik.setFirstName(firstName =;
        if (firstName == null) firstName = "";

        String lastName = JOptionPane.showInputDialog("Podaj nazwisko pracownika");
        pracownik.setLastName(lastName =;
        if (lastName == null) lastName = "";

        String age = JOptionPane.showInputDialog("Podaj wiek pracownika");
        pracownik.setAge(age);=Integer.parseInt(age);
        if (age == null) age = "";

        String pe = JOptionPane.showInputDialog("Podaj pensje pracownika");

        try {
            pracownik.setSalary(Double.parseDouble(pe));
        } catch (NumberFormatException ex) {
            // co chcesz zrobić po złapaniu wyjątku???
        }
        return pracownik;
    }

    public void main(String[] argos) {
        
        this.defEmp("podaj dane pracownika");
        Employee pracownik = new Employee();
        System.out.println(pracownik);
    }
}

A tak szczerze, to ta klasa jest do wyj....ania

0

OK tak myślałem, to od czego zacząć pisanie od początku?

1

Klasę Employee już masz:

public class Employee {

    private String firstName;
    private String lastName;
    private int age;
    private double salary;

    public Employee() {
    }

    public Employee(String firstName, String lastName, int age, double salary) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.salary = salary;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

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

    public double raiseSalaryByPercent(double percent) {
        return salary = salary + getPercentageOfSalary(percent);
    }

    private double getPercentageOfSalary(double percent){
        return (percent / 100.0) * this.salary;
    }
}

0

Tak ale mówiłeś, że ta druga jest całkiem do kitu to fajnie byłoby wiedzieć jak się do niej w ogóle zabrać ;)

1

z głową ;) kombinuj, umieszczaj kod, bo ja na pewno za Ciebie go nie będę pisał. Mogę Ci wskazywać błędy itd itp.

2

Czemu stosujesz prywatne pola i publiczne funkcje skoro cała ta klasa odgrywa rolę struktury?

1 użytkowników online, w tym zalogowanych: 0, gości: 1