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


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. 입력 받은 년/월 의 달력을 출력.


 

//java MyCalendar 2011 1
public class MyCalendar{
 public static void main(String[] args) {
  int year = Integer.parseInt(args[0]);
  int month =Integer.parseInt(args[1]);
  int day = 1;
  int sum = 0;
  int maxDay = 0;
  System.out.println("일\t월\t화\t수\t목\t금\t토\t");
  for (int i=1; i<year ;i++ ){
   if (i%400 ==0 || (i%4==0&&i%100!=0)){
    sum+=366;
   }
   else{
    sum+=365;
   }
  }
  if(year%400 ==0 || (year%4==0&&year%100!=0)){
   int[] array = {31,29,31,30,31,30,31,31,30,31,30,31};
   for (int i=0 ; i<month-1 ; i++ ){
    sum+=array[i];
   }
  }
  else{//윤년이 아니면
   int[] array1 = {31,28,31,30,31,30,31,31,30,31,30,31};
   for (int i=0 ; i<month-1 ; i++ ){
    sum+=array1[i];
  }
  }
  sum++;
 // System.out.println("총 일수="+sum);
  int space = sum%7;
  // System.out.println("무슨 요일?="+space);
  switch(month){
   case 1: ;
   case 3: ;
   case 5: ;
   case 7: ;
   case 8: ;
   case 10: ;
   case 12: maxDay = 31; break;
   case 4 : ;
   case 6 : ;
   case 9 : ;
   case 11 :  maxDay = 31; break;
   case 2 : 
    if(year%400 ==0 || (year%4==0&&year%100!=0)){
    maxDay = 29;
   }
   else{
    maxDay = 28;
   }
  }
  int count = 0;
  // 공백부터출력
  for (int i =0 ; i  <space ; i++ ){
   System.out.print("★\t");  
   count++;
  }
  for (int i =1 ; i  <=maxDay ; i++ ){
   System.out.print(i+"\t");
   count++;
   if (count%7==0){
    System.out.println();
   }
  }  
  //밑단 별
  if(((space+maxDay)%7)!=0){
   for (int i =0 ; i  <7-((space+maxDay)%7) ; i++ ){
    System.out.print("☆\t"); 
   }
  }
 }
}
 코드 실행결과는 아래와 같다.
알파벳으로 피라미드 탑을 쌓아보자.
public class pyramid{
 public static void main(String[] args){
  
  int cnt = 7;
  int alp = 65;
  for (int i = 1; i<=7 ;i++ ){
   for (int j = 0; j<cnt ;j++ )
   {
    System.out.print("  ");
   } 
   for (int k=0; k< i; k++ ){    
    System.out.printf("%c   ",alp++);
   }System.out.println("");
   cnt--;
  }
  System.out.println("");
 }
}

실행결과는 아래와같다.
Z 뒤에 아스키코드 다음값인 특수문자게 나오게 되었다. 이건 없애야하는건가 모르겠넹,


 

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

자바 급여 관리 프로그램 설계  (3) 2011.04.04
자바 전화요금 명세서 프로그램 작성  (1) 2011.04.04
자바 달력만들기  (2) 2011.04.04
자바 다이아몬드 출력하기  (1) 2011.04.04

 import javax.swing.JOptionPane;

public class Diamond3 {
 public static void main(String[] args) {
  int re =0; // 재 입력 반복
  while(re==0){
  String size1 = JOptionPane.showInputDialog("피라미드 크기?(홀수입력)");
  int size = Integer.parseInt(size1);
  int i = (size/2)+1; //윗부분 기준
  int j = size-i; //아랫부분 기준
  
  if (size%2==0){
   System.out.print("다시 입력하세요\n");
  }
  else{
  // 윗면
   for (int k=1; k<=i ; k++ ) {
      for (int m=1;i-k>=m ;m++ ) {
      System.out.print("  ");
      }
      for (int n=7;7-k<n ;n-- ) {      
       System.out.print("★  ");
      }      
      System.out.println();
    }  
  //아랫면
  for (int k2=1; k2<=j ; k2++ ) {
      for (int m1=1;m1<=k2 ;m1++ ) {
      System.out.print("  ");
      }
      for (int n1=0;n1<(j+1)-k2 ;n1++ ){      
    System.out.print("★  ");
      }      
      System.out.println();
     }  
     re=2; //프로그램 반복 끝
  }  
  }
 }

}

 

 

 




ps. 출력 환경에 따라 다이아몬드가 살짝 삐뚤어져 보일수도있다. 

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

자바 급여 관리 프로그램 설계  (3) 2011.04.04
자바 전화요금 명세서 프로그램 작성  (1) 2011.04.04
자바 달력만들기  (2) 2011.04.04
자바 알파벳 피라미드 만들기  (3) 2011.04.04

+ Recent posts