문제 : 전화요금 명세서 프로그램 작성


1. 입력양식 (data.txt)  //첨부파일 

구분     전화번호       이름         통화량


2. 출력양식

구분    전화번호        이름        기본요금       통화료      세금       합계


3. 처리조건

 1)입력되는 구분의 값이 1이면 영업용, 2면 관청용, 3이면 가정용

 2)기본요금은 영업용이 6000원, 관정용이 4800원, 가정용이 4000원이다.

 3)통화료는 1통화에 40원이다.

 4)세금은 기본 요금과 통화료를 합한 금액의 10%를 부과한다.

 5)출력결과 인쇄시 5줄마다 한 줄씩 띄운다.


-----------------------------------------------------------------------------------

-----------------------------------------------------------------------------------

해결..






0). 각자 임무대로 클래스를 나눈다.

  1)메인, 2)고객정보, 3)입력, 4)요금계산, 5) 기본 요금(static) 6)출력




1) 메인

 public class Main {

/**

* 전화요금 명세서 프로그램

*/

public static void main(String[] args) {

Customer [] array = new Customer[10];

Input input = new Input(array);

input.input();

Calc calc = new Calc(array);

calc.calc();

Output output = new Output(array);

output.display();

}

}

 data.txt 파일에 10명의 정보가 들어있다.

 고객 정보를 배열로 저장하여 각 클래스에 넘겨준다.

 입력과 계산, 출력 모두 Customer에서 생성되는 고객 정보의 주소를 입력으로 사용한다. (Call by reference)



2)고객정보

 public class Customer {

private int gubun, tongwha, fee;

private double sum, tax;

private String telno, name;

public Customer(int gubun, String telno, String name, int tongwha) {

this.gubun = gubun;

this.tongwha = tongwha;

this.telno = telno;

this.name = name;

}

public int getFee() {

return fee;

}

public void setFee(int fee) {

this.fee = fee;

}

public double getSum() {

return sum;

}

public void setSum(double sum) {

this.sum = sum;

}

public double getTax() {

return tax;

}

public void setTax(double tax) {

this.tax = tax;

}

public int getGubun() {

return gubun;

}

public int getTongwha() {

return tongwha;

}

public String getTelno() {

return telno;

}

public String getName() {

return name;

}

}


  고객의 기본정보는 private로 보호하며 접근하기위해 겟터와 셋터 (get/set)메소드를 생성한다.


 Customer 생성메소드를 재정의 (Overriding)



 3)입력

 import java.util.Scanner;

import java.io.File;

import java.io.FileNotFoundException;

import javax.swing.JOptionPane;

public class Input {

private Customer [] array;

Scanner scan;

public Input (Customer[] array){

this.array = array;

File file = new File("D:\\JavaRoom\\전화요금프로젝트\\data.txt");

try {

scan = new Scanner(file);

} catch (FileNotFoundException e) {

JOptionPane.showMessageDialog(null, "File Not Found");

System.exit(-1);

}

}

public void input(){

for (int i = 0; i < this.array.length; i++) {

String line = this.scan.nextLine().trim(); //데이터 한 줄의 양 끝 공백을 지운다.

String [] array2 = line.split("\\s+");

this.array[i] = new Customer(Integer.parseInt(array2[0]),array2[1],array2[2],

Integer.parseInt(array2[3]));

}

}

}


File과 Scanner를 이용한 입력.

-경로는 한글이 아닌 영문을 권유한다.(한글이 안되는건 아닌데 문제가 있을 수 있나보다..)

파일이 없을경우를 대비해 예외처리를 해준다.

 

System.exit(-1); // 프로그램 종료메소드로 0은 정상종료이고 그 외의 값은 비정상 종료를 의미한다.

 

 

 



 

 4)요금계산

 

 


public class Calc {

private Customer [] array;

public Calc(Customer [] array){

this.array = array;

}

public void calc(){

for (int i = 0; i < this.array.length; i++) {

int fee = calcFee(this.array[i].getTongwha());

double tax = calcTax(this.array[i].getGubun(),fee);

double sum = calcSum(this.array[i].getGubun(), fee, tax);

this.array[i].setFee(fee);

this.array[i].setTax(tax);

this.array[i].setSum(sum);

}

}

private int calcFee(int tongwha){

return tongwha*40;

}

private double calcTax(int gubun, int fee){

return (Default.getDefault(gubun)+fee)*0.1;

}

private double calcSum(int gubun, int fee, double tax){

return Default.getDefault(gubun)+fee+tax;

}

}


 클래스와 메소드는 분할할 수록 좋다,

구분에 따라 기본 가격이 다르기때문에 이러한 일을 따로 Default 클래스에서 처리 해 주도록 하여 사용하였다.

 



 5) 기본 요금

 // 기본요금

public class Default {

public static final int SALES = 6000; //영업용

public static final int GOVERnMENT = 4800; //관청용

public static final int HOME = 3000; //가정용

public static int getDefault(int gubun){

int _default = 0;

switch (gubun) {

case 1: _default = Default.SALES;

break;

case 2: _default = Default.GOVERnMENT;

break;

case 3: _default = Default.HOME;

break;

}

return _default;

}

}

 구분에 따른 기본요금은 기본적인 switch case문으로 해결


 6)출력

 


public class Output {

private Customer [] array;

public Output(Customer [] array){

this.array = array;

}

public void display(){

printLabel();

int count =0;

for (Customer c : this.array){

System.out.printf("%2d%12s%7s%,6d%10d%,12.2f%,12.2f\n", //숫자 자리수 앞에 콤마를 넣으면 3자리마다 콤마를 찍는다.

c.getGubun(),c.getTelno(),c.getName(),Default.getDefault(c.getGubun()),c.getFee(),

c.getTax(),c.getSum());

count ++;

if (0==count%5) {

System.out.println();

}

}

}

private void printLabel(){

System.out.println(" 전화요금 명세서");

System.out.println(" 구분        전화번호        이름  기본요금         통화료            세금              합계");

System.out.println("___________________________________________________________________");

}

}

 출력값의 정렬을 위해 \t을 사용하는것이 보통이지만 숫자의 경우 오른쪽 정렬이기때문에 간격이 안맞을 수 있다. 이럴때 출력의 변환문자 앞에 정수를 넣어 자리수를 확보 해 준다.(실수는 소수점)

 5줄마다 생기는 공백라인은 count 를 사용





출력 확인!!

 

프로젝트 소스파일

'Project > JAVA' 카테고리의 다른 글

자바 급여 관리 프로그램 설계  (3) 2011.04.04
자바 달력만들기  (2) 2011.04.04
자바 알파벳 피라미드 만들기  (3) 2011.04.04
자바 다이아몬드 출력하기  (1) 2011.04.04

1. 프로젝트 만들기

 -이미 앞에서 이클립스에 안드로이드 플러그인을 설치하고 SDK의 경로까지 지정했기때문에 일반 자바 프로젝트 만들듯이 안드로이드 프로젝트도 만들 수 있다.

 


 

 - New Project에서 안드로이드 프로젝트를 선택하면 위와같은 창이뜨는데, 프로젝트 명을 적고 아래 목록 중에서 실행 할 안드로이드 버전을 선택(현재는 2.2버전에서 개발을 많이 하고있다.)

 - Properties항목들이 중오한다. 가장먼저 어플 이름을 설정할 수 있고, 패키지 이름은 클래스 파일으 저장될 경로를 설정 하는 것으로 최대한 중복을 방지하기위해 2개 이상의 구분(점)을 필요로 한다. 그리고 액티비티 생성은 화면구성을 할 것인지에 대한 것으로 앞서 설명했듯 현재 생성할 프로그램이 서비스 프로그램이라면 액티비티가 필요 없는 것이다.

마지막으로 SDK버전은 API버전을 말하는것으로 위 목록에서 안드로이드2.2는 8버전을 사용한다고 되어있기 때문에 8이라고 적고 finish 하면 생성이 완료된다.

 

 

2. 프로젝트 구성요소

 - 아래와 같은 목록이 기본적으로 생성되며 하나씩 살펴 보도록 하자.


 

 1) src/ : 프로그램의 기본 소스 파일이 저장되는 공간으로 프로그램의 동작과 로직에 대한 프로그래밍이 이루어 진다.

 2) gen/R.java : 프로젝트 내에 각종 개체에 접근 할 수 있는 ID를 정의하는 곳으로 사용자가 임의로 수정할 수 없다.

 3) android 2.2 : 응용프로그램이 참조하는 안드로이드의 기본 라이브러리가 포함된다.

 4) assets/ : 리소스(자원) 폴더로 비디오나 오디오등의 파일을 저장하는 곳이다.

 5) res/drawable/ : 이미지 파일이 저장될 곳으로 해상도에따라 크기별로 저장도 가능하다.

 6) res/layout/ : 개체들의 화면을 구성할 수 있도록 레이아웃의 디자인이 정의 되는 곳이다.

 7) res/values/Strings.xml : 프로젝트에서 사용할 문자열을 xml 형태로 정의하여 관리하는 곳이다.

 8) AndroidManifest.xml : 프로젝트의 버전이나 이름, 구성 등에 대한 정보를 갖는다.

 9) default.properties : 프로젝트의 빌드 타깃이 명시되어있다.

 

+ Recent posts