우당탕탕 개발일지
9일차_과제 본문
반응형
1. 과일 판매량 구하기
월별 매출합계도 구하시오
[클래스]
Fruit
[필드]
pum, jan, feb, mar, tot
sumJan, sumFeb, sumMar
[메소드]
생성자(품명, 1월, 2월, 3월)
calcTot()
display()
public static void output()
[클래스]
FruitMain
[실행결과]
---------------------------------------
PUM JAN FEB MAR TOT
---------------------------------------
사과 100 80 75 255
포도 30 25 10 xxx
딸기 25 30 90 xxx
---------------------------------------
xxx xxx xxx output()로 처리
package homework03;
import homework03.FruitDTO;
class FruitDTO {
String pum; // 품명
int jan, feb, mar, tot; // 개수
static int sumJan, sumFeb, sumMar; // 합계
public FruitDTO(String pum, int jan, int feb, int mar) {
this.pum = pum;
this.jan = jan;
this.feb = feb;
this.mar = mar;
}
public void calcTot() {
this.tot = jan + feb + mar;
this.sumJan += jan;
this.sumFeb += feb;
this.sumMar += mar;
}
public void display() {
System.out.println(pum + "\t" + jan + "\t" + feb + "\t" + mar + "\t" + tot);
}
public static void output() {
System.out.println("\t" + sumJan + "\t" + sumFeb + "\t" + sumMar);
}
}
public class FruitMain {
public static void main(String[] args) {
System.out.println("----------------------------------------");
System.out.println("PUM\tJAN\tFEB\tMAR\tTOT");
System.out.println("----------------------------------------");
// FruitDTO[] f = new FruitDTO[3];
// f[0] = new FruitDTO("사과", 100, 80, 75);
// f[1] = new FruitDTO("포도", 30, 25, 10);
// f[2] = new FruitDTO("딸기", 25, 30, 90);
// FruitDTO[] f = { new FruitDTO("사과", 100, 80, 75),
// new FruitDTO("포도", 30, 25, 10),
// new FruitDTO("딸기", 25, 30, 90)};
FruitDTO[] f = new FruitDTO[] { new FruitDTO("사과", 100, 80, 75),
new FruitDTO("포도", 30, 25, 10),
new FruitDTO("딸기", 25, 30, 90)};
for(int i=0; i<f.length; i++) {
f[i].calcTot();
f[i].display();
}
System.out.println("----------------------------------------");
f[0].output();
}
}
반응형