본문 바로가기
JAVA

사원 연봉 계산기 #2 (+코드 공유)

by mingutistory 2020. 3. 16.
728x90

https://blog.naver.com/ndb796/220515854664

 

1. 자바 초보 프로젝트 - 객체 지향 설계하기

컴퓨터 공부를 할 때에는 무작정 공부를 하는 것 보다는 하나의 프로젝트를 할 때 더 많은 지식을또한 더 ...

blog.naver.com

 

안경잡이개발자님 블로그 > 사원 연봉 계산기 문제

 

사실 학원 다니면서 한 번 풀었었지만 푼 거에만 의의를 두고 코드를 어디에 업로드 안 해놨길래 다시 풀어봤다.

아직 많이 부족하다는 거 한번 더 느꼈고 이 코드가 마음에 안 드는데 일단은 이게 내 한계다. 

 

Employee.java

package salaryCal;

public class Employee {

	String name;
	double salary; 
	double annual_salary;
	

	public String toString() {
		return "<<<Employee>>> \n "
				+ "Name : " + name + "\n"
				+ "M_salary : " + salary + "\n"
				+ "AnnualSalary : " + salary * 12 + "\n";
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getSalary() {
		return salary;
	}

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

	public double getAnnual_salary() {
		return annual_salary;
	}

	public void setAnnual_salary(double salary) {
		this.annual_salary = salary * 12;
	}
}

Salesman.java

package salaryCal;

public class Salesman extends Employee {

	double annual_sales;
	
	public double annualSalary() {
		if(annual_sales * 0.02 > 20000) {
		annual_salary = (salary * 12) + 20000;
		} else {
			annual_salary = (salary * 12) + (annual_sales * 0.02);  
		}
		
		return annual_salary;
	}
	
	public String toString() {
		return "<<<Salesman>>> \n "
				+ "Name : " + name + "\n"
				+ "M_salary : " + salary + "\n"
				+ "AnnualSales : " + annual_sales + "\n"
				+ "AnnualSalary : " + annual_salary + "\n";
	}

	public double getAnnual_sales() {
		return annual_sales;
	}

	public void setAnnual_sales(double annual_sales) {
		this.annual_sales = annual_sales;
	}

	public double getAnnual_salary() {
		return annual_salary;
	}

	public void setAnnual_salary(double annual_salary) {
		this.annual_salary = annual_salary;
	}
}

Executive.java

package salaryCal;

public class Executive extends Employee {
	
	double stock_price;
	double bonus = 0;
	
	public double annualSalary() {
		
		if(stock_price >= 50) {
			bonus = 30000;
		}else {
			bonus = stock_price; 
		}
		
		annual_salary = salary * 12 + bonus;
		
		return annual_salary;
	}
	
	public String toString() {
		return "<<<Executive>>> \n "
				+ "Name : " + name + "\n"
				+ "M_salary : " + salary + "\n"
				+ "Stock_Price : " + stock_price + "\n"
				+ "AnnualSalary : " + annual_salary + "\n";
	}


	public double getStock_price() {
		return stock_price;
	}

	public void setStock_price(double stock_price) {
		this.stock_price = stock_price;
	}

	public double getAnnual_salary() {
		return annual_salary;
	}

	public void setAnnual_salary(double annual_salary) {
		this.annual_salary = annual_salary;
	}	
}

main.java

package salaryCal;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class main {

	public static void main(String[] args) {
        BufferedReader bReader = null;
        
        try {
            
            String str;
            File file = new File("src/sal.txt");
            bReader = new BufferedReader(new FileReader(file));
            
            Employee[][] arr = new Employee[10][10];
            int i = 0, j = 0;
            double sum_2014 = 0, sum_2015 = 0; 
      
            while((str = bReader.readLine()) != null) {
            	String[] str_arr = str.split(" ");

            	switch(str_arr[1]) {
            	case "Employee" : {
            		Employee emp = new Employee();
            		emp.setName(str_arr[2]);
            		emp.setSalary(Double.parseDouble(str_arr[3]));
            		emp.setAnnual_salary(Double.parseDouble(str_arr[3]));
            		if(str_arr[0].equals("2014")) {
            			arr[0][i] = emp;
            			sum_2014 += emp.getAnnual_salary();
            			i++;
            		}else {
            			arr[1][j] = emp;
            			sum_2015 += emp.getAnnual_salary();
            			j++;
            		}
            		break;
            	}
            	case "Salesman" : {
            		Salesman sales = new Salesman();
            		sales.setName(str_arr[2]);
            		sales.setSalary(Integer.parseInt(str_arr[3]));
            		sales.setAnnual_sales(Double.parseDouble(str_arr[4]));
            		sales.setAnnual_salary(sales.annualSalary());
            		if(str_arr[0].equals("2014")) {
            			arr[0][i] = sales;
            			sum_2014 += sales.getAnnual_salary();
            			i++;
            		}else {
            			arr[1][j] = sales;
            			sum_2015 += sales.getAnnual_salary();
            			j++;
            		}
            		break;
            	}
            	
            	case "Executive" : {
            		Executive exe = new Executive();
            		exe.setName(str_arr[2]);
            		exe.setSalary(Integer.parseInt(str_arr[3]));
            		exe.setStock_price(Double.parseDouble(str_arr[4]));
            		exe.setAnnual_salary(exe.annualSalary());
            		if(str_arr[0].equals("2014")) {
            			arr[0][i] = exe;
            			sum_2014 += exe.getAnnual_salary();
            			i++;
            		}else {
            			arr[1][j] = exe;
            			sum_2015 += exe.getAnnual_salary();
            			j++;
            		}
            		break;
            	}
            	} 	
            }
            
            System.out.println("[ 2014 ]");
            for(int k = 0; k < i; k++) {
            	System.out.println(arr[0][k]);
            }
            System.out.println("-The average of all salaries for all employees for 2014 : " + sum_2014 / i);
            
            System.out.println("[ 2015 ]");
            for(int k = 0; k < j; k++) {
            	System.out.println(arr[1][k]);
            }
            System.out.println("-The average of all salaries for all employees for 2015 : " + sum_2015 / j);
            
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(bReader != null) bReader.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
	}

}

 

중복되는 코드들 참 마음에 안든다.

300x250

댓글