2013-05-03 2 views
0

큰 프로그램에서 작업하며 파일에서 읽고 다른 프로그램으로 인쇄합니다. 인쇄가 잘된 것 같지만 레이블이 필요합니다. 이 같은 우리의 프로그램을 인쇄 :Java 프로그래밍, 표에 레이블 및 합계 추가

package hotelreport; 

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.Date; 
import java.util.Scanner; 

public class HotelReport { 

    static File hotelFile = new File("store.txt"); 
    static Path outputFilePath = Paths.get("storeAnswer.txt"); 
    static int invoiceID; 
    static int serviceCode; 
    static String invoiceDate; 
    static double saleAmount; 
    static int rows = 13; 
    static int columns = 6; 
    static int[][] table; 

    public static void main(String[]args){ 

      getData(); 
      PrintWriter out; 
      File file = new File("storeAnswer.txt"); 

      try { 
       out = new PrintWriter("storeAnswer.txt"); 
       for(int i =0;i<=11;i++){ 
        for(int j=0;j<=4;j++){ 
         out.print(table[i][j]); 
         out.print("\t"); 
        } 
        out.println(""); 
       } 
       out.close(); 
      } catch (FileNotFoundException e) { 
       System.out.println("File not found"); 
      } 



    } 

    public static void getData(){ 
     table = new int[rows][columns]; 

     try { 
      Scanner sc = new Scanner(hotelFile); 

      while(sc.hasNext()){ 
       invoiceID = sc.nextInt(); 
       serviceCode = sc.nextInt(); 
       String invoiceDate = sc.next(); 
       saleAmount = sc.nextDouble(); 
       switch(serviceCode){ 
        case 0: 
         if(invoiceDate.startsWith("01")){ 
          table[0][0]+=saleAmount; 
         }else if(invoiceDate.startsWith("02")){ 
          table[1][0]+=saleAmount; 
         }else if(invoiceDate.startsWith("03")){ 
          table[2][0]+=saleAmount; 
         }else if(invoiceDate.startsWith("04")){ 
          table[3][0]+=saleAmount; 
         }else if(invoiceDate.startsWith("05")){ 
          table[4][0]+=saleAmount; 
         }else if(invoiceDate.startsWith("06")){ 
          table[5][0]+=saleAmount; 
         }else if(invoiceDate.startsWith("07")){ 
          table[6][0]+=saleAmount; 
         }else if(invoiceDate.startsWith("08")){ 
          table[7][0]+=saleAmount; 
         }else if(invoiceDate.startsWith("09")){ 
          table[8][0]+=saleAmount; 
         }else if(invoiceDate.startsWith("10")){ 
          table[9][0]+=saleAmount; 
         }else if(invoiceDate.startsWith("11")){ 
          table[10][0]+=saleAmount; 
         }else if(invoiceDate.startsWith("12")){ 
          table[11][0]+=saleAmount; 
         }; 
        case 1: 
         if(invoiceDate.startsWith("01")){ 
          table[0][1]+=saleAmount; 
         }else if(invoiceDate.startsWith("02")){ 
          table[1][1]+=saleAmount; 
         }else if(invoiceDate.startsWith("03")){ 
          table[2][1]+=saleAmount; 
         }else if(invoiceDate.startsWith("04")){ 
          table[3][1]+=saleAmount; 
         }else if(invoiceDate.startsWith("05")){ 
          table[4][1]+=saleAmount; 
         }else if(invoiceDate.startsWith("06")){ 
          table[5][1]+=saleAmount; 
         }else if(invoiceDate.startsWith("07")){ 
          table[6][1]+=saleAmount; 
         }else if(invoiceDate.startsWith("08")){ 
          table[7][1]+=saleAmount; 
         }else if(invoiceDate.startsWith("09")){ 
          table[8][1]+=saleAmount; 
         }else if(invoiceDate.startsWith("10")){ 
          table[9][1]+=saleAmount; 
         }else if(invoiceDate.startsWith("11")){ 
          table[10][1]+=saleAmount; 
         }else if(invoiceDate.startsWith("12")){ 
          table[11][1]+=saleAmount; 
         }; 
        case 2: 
         if(invoiceDate.startsWith("01")){ 
          table[0][2]+=saleAmount; 
         }else if(invoiceDate.startsWith("02")){ 
          table[1][2]+=saleAmount; 
         }else if(invoiceDate.startsWith("03")){ 
          table[2][2]+=saleAmount; 
         }else if(invoiceDate.startsWith("04")){ 
          table[3][2]+=saleAmount; 
         }else if(invoiceDate.startsWith("05")){ 
          table[4][2]+=saleAmount; 
         }else if(invoiceDate.startsWith("06")){ 
          table[5][2]+=saleAmount; 
         }else if(invoiceDate.startsWith("07")){ 
          table[6][2]+=saleAmount; 
         }else if(invoiceDate.startsWith("08")){ 
          table[7][2]+=saleAmount; 
         }else if(invoiceDate.startsWith("09")){ 
          table[8][2]+=saleAmount; 
         }else if(invoiceDate.startsWith("10")){ 
          table[9][2]+=saleAmount; 
         }else if(invoiceDate.startsWith("11")){ 
          table[10][2]+=saleAmount; 
         }else if(invoiceDate.startsWith("12")){ 
          table[11][2]+=saleAmount; 
         }; 
        case 3: 
         if(invoiceDate.startsWith("01")){ 
          table[0][3]+=saleAmount; 
         }else if(invoiceDate.startsWith("02")){ 
          table[1][3]+=saleAmount; 
         }else if(invoiceDate.startsWith("03")){ 
          table[2][3]+=saleAmount; 
         }else if(invoiceDate.startsWith("04")){ 
          table[3][3]+=saleAmount; 
         }else if(invoiceDate.startsWith("05")){ 
          table[4][3]+=saleAmount; 
         }else if(invoiceDate.startsWith("06")){ 
          table[5][3]+=saleAmount; 
         }else if(invoiceDate.startsWith("07")){ 
          table[6][3]+=saleAmount; 
         }else if(invoiceDate.startsWith("08")){ 
          table[7][3]+=saleAmount; 
         }else if(invoiceDate.startsWith("09")){ 
          table[8][3]+=saleAmount; 
         }else if(invoiceDate.startsWith("10")){ 
          table[9][3]+=saleAmount; 
         }else if(invoiceDate.startsWith("11")){ 
          table[10][3]+=saleAmount; 
         }else if(invoiceDate.startsWith("12")){ 
          table[11][3]+=saleAmount; 
         }; 
        case 4: 
         if(invoiceDate.startsWith("01")){ 
          table[0][4]+=saleAmount; 
         }else if(invoiceDate.startsWith("02")){ 
          table[1][4]+=saleAmount; 
         }else if(invoiceDate.startsWith("03")){ 
          table[2][4]+=saleAmount; 
         }else if(invoiceDate.startsWith("04")){ 
          table[3][4]+=saleAmount; 
         }else if(invoiceDate.startsWith("05")){ 
          table[4][4]+=saleAmount; 
         }else if(invoiceDate.startsWith("06")){ 
          table[5][4]+=saleAmount; 
         }else if(invoiceDate.startsWith("07")){ 
          table[6][4]+=saleAmount; 
         }else if(invoiceDate.startsWith("08")){ 
          table[7][4]+=saleAmount; 
         }else if(invoiceDate.startsWith("09")){ 
          table[8][4]+=saleAmount; 
         }else if(invoiceDate.startsWith("10")){ 
          table[9][4]+=saleAmount; 
         }else if(invoiceDate.startsWith("11")){ 
          table[10][4]+=saleAmount; 
         }else if(invoiceDate.startsWith("12")){ 
          table[11][4]+=saleAmount; 
         }; 


       } 
      } 

     } catch (FileNotFoundException e) { 
      System.out.println("File not found. Check Location"); 
     } catch (IOException e1) { 
      System.out.println("File coud not be created"); 
     } 
    } 



} 
: 여기

enter image description here

는 우리의 코드입니다

40851 50497 813082 1118366 1120629 
34400 45547 824439 1425116 1429306 
39249 48833 809627 1101561 1103908 
57939 67308 821564 1126250 1128620 
64000 76037 827086 1424632 1431210 
34200 40240 554391 767810 772107 
89310 101149 2351871 2971044 2980458 
84370 95851 2470295 3096137 3105516 
59700 71190 2290032 2905223 2909869 
67600 79212 2419095 3031704 3034002 
60200 71594 2348818 2969848 2972035 
46000 56050 824092 1300904 1303163 

우리는 (숫자를 무시)과 같이 합계와 라벨을 인쇄 할 필요가

+2

코드에 불필요한 중복이 많이 있습니다. 당신은 코드를 단순화 할 수 있습니다 * 많은 *, 그리고 과정에서 훨씬 쉽게 디버깅하고 우리가 읽고 이해할 수 있습니다. –

+2

그리고 뭐가 문제입니까? 왜냐하면 지금 당장 유일하게 명백한 문제는 필요한 포맷팅을하는 코드가 빠져 있기 때문입니다 ... – fvu

+0

'InvoiceDate.substring (0, 2)'를 분석하기 위해서는'Integer.parseInt (...) '를 사용해야합니다. 코드를 90 % 이상 줄이려면이 코드를 사용하십시오. –

답변

0

speadsheet에서 인쇄 된 것처럼 멋져 보이시겠습니까? 그렇다면 JasperReports이 필요합니다. http://community.jaspersoft.com/project/jasperreports-library

자주 iReport 디자이너를 사용하며 매우 유용합니다.

또 다른 옵션은 HTML 마크 업과 함께 데이터를 출력하여 브라우저에서 볼 수 있습니다.

원하는 경우 TSV 텍스트 만 표시하면 일부 라벨이 인쇄됩니다! 출력 개월

out.write("Label 1\tLabel 2\tLabel 3\t ..."); 

:

  String[] months; 
      months[0] = "January"; 
      months[1] = "February"; 
      //... 

      for(int i =0;i<=11;i++){ 
       out.print(months[i]+"\t"); 
       for(int j=0;j<=4;j++){ 
        out.print(table[i][j]); 
        out.print("\t"); 
       } 
       out.println(""); 
      } 
+0

월 및 각 행과 열의 합계에 대한 레이블을 어떻게 추가합니까? 그게 문제 야. – LClarke27

+0

@ LClarke27 답변을 좀 더 추가했습니다. –

1

당신은 당신이 (out.print)

관련없는 인쇄 명령을 호출 할 때마다 각각의 값을 출력하는 내용을 변경,하지만 당신이 ' 재 일 :

    if(invoiceDate.startsWith("01")){ 
         table[0][0]+=saleAmount; 
        }else if(invoiceDate.startsWith("02")){ 
         table[1][0]+=saleAmount; 
        }else if(invoiceDate.startsWith("03")){ 
         table[2][0]+=saleAmount; 
        }else if(invoiceDate.startsWith("04")){ 
         table[3][0]+=saleAmount; 
        }else ... 

이유는 단지 루프?

를 사용하지

물론 더 많은 개선이있을 수 있지만, 적어도 시작일 것입니다.

편집 :

아, 지금 테이블을 추가하는 것이이 좀 더 의미가 있습니다. 데이터의 루프 다음에 새 행을 인쇄하는 달을 추가하고 열 값의 초기 인쇄가 필요할 가능성이 높습니다.