Thu/Jan/3 [Java] day4 String Method, Exercising Class basics, ArrayList, Random

교육 4일차
String Method, Exercising Class basics, ArrayList, Random


String 입사일 variable 이용해 평균 근속연수 구하기

<My Solution>

MainClass
1
2
3
4
5
6
7
8
9
10
11
12
public class MainClass {
    public static void main(String[] args) {
        DataClass dc = new DataClass();
        // System.out.println(dc.ic[13].hireDate);
        double avg = dc.workyear() / (dc.ic.length * 1.0);
        System.out.println("The Average of working years is : " + (avg));
    }
}
cs


InfoClass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class InfoClass {
    int empNo, sale, commission, deptNo;
    String eName, job, hireDate;
    public InfoClass() {
    }
    public InfoClass(int no, String n, String j, String d, int s, int c, int dn) {
        this.empNo = no;
        this.eName = n;
        this.job = j;
        this.hireDate = d;
        this.sale = s;
        this.commission = c;
        this.deptNo = dn;
    }
}
cs


DataClass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class DataClass {
    InfoClass ic[] = new InfoClass[14];
    public DataClass() {
        ic[0] = new InfoClass(1001, "송민호", "사원", "2007-03-01", 300, 0, 20);
        ic[1] = new InfoClass(1002, "이영민", "대리", "2007-04-02", 250, 80, 30);
        ic[2] = new InfoClass(1003, "김민우", "과장", "2005-02-10", 500, 100, 30);
        ic[3] = new InfoClass(1004, "장희재", "부장", "2003-09-02", 600, 0, 20);
        ic[4] = new InfoClass(1005, "오종호", "과장", "2005-04-07", 450, 200, 30);
        ic[5] = new InfoClass(1006, "최형우", "부장", "2003-10-09", 480, 0, 30);
        ic[6] = new InfoClass(1007, "윤가람", "부장", "2004-01-08", 520, 0, 10);
        ic[7] = new InfoClass(1008, "이명희", "차장", "2004-03-08", 500, 0, 30);
        ic[8] = new InfoClass(1009, "송종영", "사장", "1996-10-04", 1000, 0, 20);
        ic[9] = new InfoClass(1010, "황영호", "과장", "2005-04-07", 500, 0, 10);
        ic[10] = new InfoClass(1011, "김현정", "사원", "2007-03-01", 280, 0, 30);
        ic[11] = new InfoClass(1012, "강정혜", "사원", "2007-08-09", 300, 0, 20);
        ic[12] = new InfoClass(1013, "박민훈", "부장", "2002-10-09", 560, 0, 20);
        ic[13] = new InfoClass(1014, "조인성", "사원", "2007-11-09", 250, 0, 10);
        // System.out.println("DataClass operation");
        workyear();
        // System.out.println(workyears());
    }
    public int workyear() {
        int[] y = new int[ic.length];
        int sum = 0;
        for (byte i = 0; i < ic.length; i++) {
            String hd = ic[i].hireDate;
            String[] m = hd.split("-");
            y[i] = 2020 - Integer.parseInt(m[0]);
        }
        for (byte i = 0; i < y.length; i++) {
            sum = sum + y[i];
        }
        return sum;
    }
}
cs

<강사님's Solution 1 : MainClass에서 한번에>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MainClass {
    public static void main(String[] args) {
        DataClass dc = new DataClass();
        int sum = 0 ; String hd ; String[] s;
        for (int i = 0; i<dc.ic.length; i++) {
            hd = dc.ic[i].hireDate;
            s = hd.split("-");
            sum = sum + (2020-Integer.parseInt(s[0]));
        }
        double avg = sum / (dc.ic.length * 1.0);
        System.out.println("The Average of working years is : " + (avg));
    }
}
cs

<강사님's Solution 2 : OperClass 추가>

MainClass
1
2
3
4
5
6
7
8
9
10
public class MainClass {
    public static void main(String[] args) {
        DataClass dc = new DataClass();
        OperClass oc = new OperClass();
        oc.avgHireDate(dc.ic);
    }
}
cs

OperClass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class OperClass {
        public OperClass () {
            
        }
        
        public void avgHireDate(InfoClass[] ic) {
            int sum = 0 ; String hd ; String[] s;
            for (int i = 0; i<ic.length; i++) {
                hd = ic[i].hireDate;
                s = hd.split("-");
                sum = sum + (2020-Integer.parseInt(s[0]));
            }
            double avg = sum / (ic.length * 1.0);
            
            System.out.println(avg);
        }
}
cs


<최대값 구하는 방법 : for-if, 비교연산>
 - 최저값은 부호만 반대로

1
2
3
4
5
6
7
8
9
int [] n = {10, 15, 33, 5, 7, 20};  int temp = 0;
        
for (byte i=0 ; i < n.length; i++) {
    if(temp < n[i]) {
        temp = n [i];
    }
}
System.out.println("the Maximum number is " + temp);
cs

<특정년도 동기 구하기>
<특정부서 명단 구하기>
<최고 연간수입 구하기>

MainClass

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class MainClass {
    public static void main(String[] args) {
        DataClass dc = new DataClass();
        // System.out.println(dc.ic[13].hireDate);
        OperClass oc = new OperClass();
        oc.avgHireDate(dc.ic);
        YearClass yc = new YearClass();
        yc.sameyear2(dc.ic, 2007);
        DeptClass dpc = new DeptClass();
        dpc.sameDept(dc.ic, 30);
        
        IncomeClass income = new IncomeClass();
        System.out.println("\nMaximun of annual income is \n" + income.maxIncome(dc.ic));
        
    }
}
cs



YearClass : 동기 구하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class YearClass {
    public YearClass() {
    }
    public int[] years(InfoClass[] ic) {
        int y[] = new int[ic.length];
        for (byte i = 0; i < ic.length; i++) {
            String x[] = ic[i].hireDate.split("-");
            y[i] = Integer.parseInt(x[0]);
        }
        return y;
    }
    public void sameyear(InfoClass[] ic) {
        int year[] = years(ic);
        for (byte i = 0; i < ic.length; i++) {
            for (byte j = 0; j < ic.length; j++) {
                if (year[j] == year[i] && j != i) {
                    System.out.println("personnel " + ic[j].eName + "'s year is same with " + ic[i].eName);
                }
            }
        }
    }
    
    public void sameyear2(InfoClass[] ic, int y){
        int year[] = years(ic);
        int l = ic.length;
        System.out.println(y + "년도 동기들");
        for(byte i=0 ; i <l ; i++){
            if(y==year[i]){
                System.out.println(ic[i].eName);
            }
        }
    }
}
cs

DeptClass : 특정부서 직원 명단 구하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class DeptClass {
    public DeptClass() {
    }
    public void sameDept(InfoClass[] ic, int d) {
        int l = ic.length;
        System.out.println("\nPersonnels in depart No.\n" + d);
        for (byte i = 0; i < l; i++) {
            if (ic[i].deptNo == d) {
                System.out.println(ic[i].eName);
            }
        }
    }
}
cs


IncomeClass : 1년 급여, 최고 급여 구하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class IncomeClass {
    public IncomeClass() {
    }
    public int[] annIncome(InfoClass[] ic) {
        int l = ic.length;
        int pay[] = new int[l];
        int income[] = new int[l];
        for (byte i = 0; i < l; i++) {
            pay[i] = ic[i].sale * 13;
            income[i] = pay[i] + ic[i].commission;
        }
        return income;
    }
    public int maxIncome(InfoClass[] ic) {
        int m = 0;
        int l = ic.length;
        int income[] = new int[l];
        for (byte i = 0; i < l; i++) {
            income[i] = annIncome(ic)[i];
            if (m < income[i]) {
                m = income[i];
            }
        }
        return m;
    }
}
cs

이것저것 하다보니 Class - method 호출도 적응되는듯..

<END>

댓글

가장 많이 본 글