20/01/02 3일차 Class 활용 예제_총수입 구하기
20/01/02 3일차
Class 활용 예제_총수입 구하기
<1차 시도 : 실패>
MainClass
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class MainClass {
public static void main(String[] args) {
TestClass result = new TestClass();
int[] totalArray = result.totalArray();
for(byte i = 0 ; i<totalArray.length ; i++){
System.out.println(totalArray[i]);
}
}
}
| cs |
InfoClass
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
45
46
47
48
|
public class InfoClass {
String name, local, work;
int age, annsal, monsal, totalsal, lvpay, year;
public InfoClass(){
}
public InfoClass(String n, int a, String l, int y, String w){
name = n; age = a; local = l; year = y; work = w;
salaryOperator();
}
//기본급 연봉 구하기 - method 활용
public void salaryOperator() {
if(work.equals("정규")){
monsal = 250;
}
else if(work.equals("비정규")){
monsal = 200;
}
annsal = monsal*13;
// 생활비 method
if(local.equals("US")){
lvpay = 70;
}
else if (local.equals("JP")){
lvpay = 50;
}
else if (local.equals("EU")){
lvpay = 100;
}
// 총임금 구하기
if(work.equals("정규")){
totalsal = age*(annsal + lvpay*12);
}
else if (work.equals("비정규")){
totalsal = age*(annsal + lvpay*12);
}
}
}
| cs |
TestClass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class TestClass {
InfoClass[] ic = new InfoClass[10];
int icl = ic.length;
int totalArray[] = new int[icl];
public TestClass(){
ic[0] = new InfoClass("A", 28, "KR", 1, "정규");
ic[1] = new InfoClass("B", 31, "JP", 3, "비정규");
ic[2] = new InfoClass("C", 28, "JP", 1, "정규");
ic[3] = new InfoClass("D", 29, "KR", 2, "정규");
ic[4] = new InfoClass("E", 28, "US", 2, "비정규");
ic[5] = new InfoClass("F", 32, "US", 5, "정규");
ic[6] = new InfoClass("G", 28, "KR", 1, "비정규");
ic[7] = new InfoClass("H", 29, "EU", 2, "비정규");
ic[8] = new InfoClass("I", 33, "EU", 6, "정규");
ic[9] = new InfoClass("J", 28, "KR", 1, "비정규");
for(int m=0 ; m< ic.length ; m++){
totalArray[m] = ic[m].totalsal;
}
}
}
| cs |
<2차 시도 : 성공>
MainClass에서 TestClass 함수 불러오는게 항상 잘 안되는 듯..MainClass
1
2
3
4
5
6
7
8
9
10
11
12
|
public class MainClass {
public static void main(String[] args) {
TestClass tc = new TestClass();
int t[] = tc.income();
for(byte i=0; i <tc.ic.length ; i++) {
System.out.println("Total Annual Income of " + (i+1) + " is : " + t[i]);
}
}
}
| cs |
InfoClass
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class InfoClass {
String name, local, type;
int age = 0, year = 0;
public InfoClass() {
}
public InfoClass(String n, int a, String l, int y, String t) {
name = n; age = a; local = l; year = y; type = t;
}
}
| cs |
TestClass
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
public class TestClass {
InfoClass ic[] = new InfoClass[10];
public TestClass() {
ic[0] = new InfoClass("A", 28, "KR", 1, "정규");
ic[1] = new InfoClass("B", 31, "JP", 3, "비정규");
ic[2] = new InfoClass("C", 28, "JP", 1, "정규");
ic[3] = new InfoClass("D", 29, "KR", 2, "정규");
ic[4] = new InfoClass("E", 28, "US", 2, "비정규");
ic[5] = new InfoClass("F", 32, "US", 5, "정규");
ic[6] = new InfoClass("G", 28, "KR", 1, "비정규");
ic[7] = new InfoClass("H", 29, "EU", 2, "비정규");
ic[8] = new InfoClass("I", 33, "EU", 6, "정규");
ic[9] = new InfoClass("J", 28, "KR", 1, "비정규");
/*
* for(byte i = 0 ; i < ic.length ; i++) {
* System.out.println(ic[i].type);
* }
*/
income();
}
public int[] income() {
int totalincome[] = new int[ic.length];
int annrev = 0, monrev=0;
int annlc = 0, lc = 0;
int annincome = 0;
for(int i=0; i < ic.length ; i++) {
if(ic[i].type.equals("정규") ) {
monrev = 250;
}
else if(ic[i].type.equals("비정규")) {
monrev = 200;
}
annrev = monrev*13;
if(ic[i].local.equals("US")) {
lc = 70;
}
else if(ic[i].local.equals("JP")) {
lc = 50;
}
else if(ic[i].local.equals("EU")) {
lc = 100;
}
annlc = lc*12;
annincome = annrev + annlc;
totalincome[i] = annincome;
}
return totalincome;
}
}
| cs |
댓글
댓글 쓰기