TIL-1주차 코드

 

1. 성적 입력, 출력 프로그램 작성(콘솔 입/출력, 파일 입/출력)

package ex1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

public class FileIOApp {

	//static int kor1, kor2, kor3;
	public static void main(String[] args) throws IOException {

		//ConsoleOutputStream out = new ConsoleOutputStream();	//
		int answer = 1;
		int total;
		double avg;
		int kor1=0, kor2=0, kor3=0;
		int menu;
		
		while(answer == 1) {
			
			{
				System.out.println("┌──────────────────────┐");
				System.out.println("│	메인 메뉴	       │");
				System.out.println("└──────────────────────┘");
				System.out.println("1. 성적 입력");
				System.out.println("2. 성적 출력");
				System.out.println("3. 파일 입력");
				System.out.println("4. 파일 저장");
				System.out.println("5. 종료");
				System.out.println("선택 > ");
				
				Scanner scan = new Scanner(System.in);
				menu = Integer.parseInt(scan.nextLine());
			}
			
			System.out.println("1번 검사 전");
			if(menu == 1){
				
				// 콘솔 입력 부분
				Scanner scan = new Scanner(System.in);
				
				System.out.println("┌──────────────────────┐");
				System.out.println("│	성적 입력	       │");
				System.out.println("└──────────────────────┘");
				
//				System.out.print("kor1 : ");
//				kor1 = scan.nextInt();
				do {
					System.out.print("kor1 : ");
					kor1 = scan.nextInt();
					
					if(kor1 < 0 || kor1 > 100) {
						System.out.println("kor1는 0~100 의 값을 입력해주세요.");
						kor1 = scan.nextInt();
					}
					
				} while (kor1 < 0 || kor1 > 100); 
				
//				System.out.print("kor2 : ");
//				kor2 = scan.nextInt();
				do {
					System.out.print("kor2 : ");
					kor2 = scan.nextInt();
					
					if(kor2 < 0 || kor2 > 100) {
						System.out.println("kor2는 0~100 의 값을 입력해주세요.");
						kor2 = scan.nextInt();
					}
					
				} while (kor2 < 0 || kor2 > 100); 
				
//				System.out.print("kor3 : ");
//				kor3 = scan.nextInt();
				do {
					System.out.print("kor3 : ");
					kor3 = scan.nextInt();
					
					if(kor3 < 0 || kor3 > 100) {
						System.out.println("kor3는 0~100 의 값을 입력해주세요.");
						kor3 = scan.nextInt();
					}
					
				} while (kor3 < 0 || kor3 > 100); 
				
				scan.nextLine();			// 에러발생 줄이려고, 버퍼를 마지막에 다 비워준다.
				
				continue;
			}
			
			//---------------------------------------------------------------------------
			
			System.out.println("2번 검사 전");
			if(menu == 2) {	// 콘솔 출력 부분
				total = kor1 + kor2 + kor3;
				avg = total / 3.0;				//  요즘은 double을 많이 사용하는 추세이다.(용량 증가로)
				
				System.out.println("┌──────────────────────┐");
				System.out.println("│	성적 출력	       │");
				System.out.println("└──────────────────────┘");
		
				System.out.printf("국어1 : %3d\n", kor1);
				System.out.printf("국어2 : %3d\n", kor2);
				System.out.printf("국어3 : %3d\n", kor3);
				
				System.out.printf("총점 : %3d\n", total);
				System.out.printf("평균 : %6.2f\n", avg);
				
				System.out.println("────────────────────────");
	
			// .out.println(total);		// 변수가 사용되면 값이 출력된다. 240이 "240"으로 바뀌어 출력된다.
			
			
			// out.close();	// 하부 시스템이 리소스를 차지하고 있어서 사용하고나서 항상 닫아줘야 한다.
				
				continue;
			}
			
			//---------------------------------------------------------------------------
			
			System.out.println("3번 검사 전");
			// 파일 입력 부분
			if(menu == 3){
				FileInputStream fis = new FileInputStream("res/data.csv");
				Scanner scanner = new Scanner(fis);
				
				String line = scanner.nextLine();
				String[] kors = line.split(",");
				
				kor1 = Integer.parseInt(kors[0]);
				kor2 = Integer.parseInt(kors[1]);
				kor3 = Integer.parseInt(kors[2]);
				
//				scan.close();
				fis.close();
				
				continue;
			}
			
			//---------------------------------------------------------------------------
			
			System.out.println("4번 검사 전");	
			if(menu == 4){
				// 파일 출력 부분
				FileOutputStream fos = new FileOutputStream("res/data.csv");
				PrintStream out = new PrintStream(fos);
				
				out.printf("%d,%d,%d", kor1, kor2, kor3);
	
				fos.close();
				
				System.out.println("작업완료");
				
				continue;
			}
			//fos.close();	// 하부 시스템이 리소스를 차지하고 있어서 사용하고나서 항상 닫아줘야 한다.
							// close의 위치도 중요하다. 가장 마지막에 close 해줄 것
			
			
			//---------------------------------------------------------------------------
			
			System.out.println("5번 검사 전");
			if(menu == 5){
				/*
				 * System.out.println("계속하시겠습니까?(계속:1/종료:0)\n"); Scanner scan = new
				 * Scanner(System.in); int answer = Integer.parseInt(scan.nextLine());
				 */
				System.out.println("계속하시겠습니까?(계속:1/종료:0)\n");
				Scanner scan = new Scanner(System.in);
				answer = Integer.parseInt(scan.nextLine());
				
				if(answer == 0) 
					break;
				
				continue;
				
			}
			
		}
		System.out.println("Bye~!");		// 조건식이 많을 수록 좋은 코드가 아니라서 이게 옳다.
	}
}