본문 바로가기

.etc/Review

[210928~29] 사용자 입력 예외 처리 연습

Q. 사용자가 입력한 사원정보(사번, 이름, 부서명, 직무명) 를 저장한다. 

사용자가 입력한 파일명으로 사원정보 파일이 저장되도록 한다. 파일은 CSV 파일로 저장하여 엑셀로 열릴 수 있게 한다.

 

package com.kh.exam1;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

class Emp {
	private String fileName;
	
	private int empID;
	private String name;
	private String dept;
	private String job;
	
	public Emp (int empID, String name, String dept, String job) {
		this.empID = empID;
		this.name = name;
		this.dept = dept;
		this.job = job;
	}
	
	public Emp (String fileName) {
		this.fileName = fileName;
	}
	
	
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public int getEmpID() {
		return empID;
	}
	public void setEmpID(int empID) {
		this.empID = empID;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDept() {
		return dept;
	}
	public void setDept(String dept) {
		this.dept = dept;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	
	@Override
	public String toString() {
		return "Emp [empID=" + empID + ", name=" + name + ", dept=" + dept + ", job=" + job + "]";
	}

	
}

public class Sample3 {
	static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args) {
		// 사용자 입력을 활용하여 입력한 부서코드에 해당하는 사원정보(사번, 이름, 부서명, 직무명)
		// 입력한 파일명으로 저장되도록 한다.(파일은 CSV 파일로 저장하여 엑셀로 열릴 수 있게 한다.)
		String fileName;
		
		int empID;
		String name, dept, job;
		
		String[] input = new String[4];
		List<Emp> eList = new LinkedList<Emp>();
		
		while(true) {
			System.out.print("저장할 파일의 이름을 입력하세요 : ");
			fileName = sc.next(); 	sc.nextLine();
			if(fileName.contains("\\") || fileName.contains("/") ||
				fileName.contains(":") || fileName.contains("*") || 
				fileName.contains("?") || fileName.contains("\"") || 
				fileName.contains("<") || fileName.contains(">") ||
				fileName.contains("|")) {	
				System.out.println("파일 이름에는 다음 문자를 사용할 수 없습니다"
						+ " -> \\ / : * ? \" < > |");
			} else new Emp(fileName); break;
		}
		
	while(true) {
		System.out.print("사번, 이름, 부서명, 직무명을 입력하세요 (구분자 , ) : ");
		
		if(!sc.nextLine().contains(",")) {
			System.out.println("구분자를 포함하여 모든 정보를 입력해 주세요.");
			continue;
		}
		break;
	}
		System.out.print("사번, 이름, 부서명, 직무명을 입력하세요 (구분자 , ) : ");
		input = sc.nextLine().split(","); 
		
		int i = 0;
		for(; i < input.length; i++) {
			input[i] = input[i].trim();
			if(i != input.length - 1) {
				System.out.println("정보를 끝까지 입력하지 않으셨습니다.");
				System.out.print("사번, 이름, 부서명, 직무명을 입력하세요 (구분자 , ) : ");
			}
			
		}
			
		empID = Integer.parseInt(input[0]);
		name = input[1]; 
		dept = input[2];
		job = input[3];
		
		eList.add(new Emp(empID, name, dept, job));
		
		System.out.println(Arrays.toString(input));
		System.out.println(eList);
		
		FileWriter fw;
		try {
			fw = new FileWriter(fileName + ".csv");
				
		BufferedWriter bw = new BufferedWriter(fw);
		bw.write("EMP_ID,NAME,DEPARTMENT,JOB\r\n"); 

		for(Emp e : eList) {		
			bw.write(e.getEmpID() + "," + e.getName() + "," + e.getDept() 
			+ "," + e.getJob() +"\r\n");
		}
		
		bw.close();
		fw.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}	
		
	
	}
}

 

package com.kh.exam1;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

class EmpInfo {
	private String fileName;	
	private int empId;
	private String name;
	private String dept;
	private String job;
	
	public EmpInfo (String fileName, int empId, String name, String dept, String job) {
		this.fileName = fileName;
		this.empId = empId;
		this.name = name;
		this.dept = dept;
		this.job = job;
	}
	
	public EmpInfo (int empId, String name, String dept, String job) {
		this.empId = empId;
		this.name = name;
		this.dept = dept;
		this.job = job;
	}
	
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDept() {
		return dept;
	}
	public void setDept(String dept) {
		this.dept = dept;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}

	@Override
	public String toString() {
		return "EmpInfo [fileName=" + fileName + ", empId=" + empId + ", name=" + name + ", dept=" + dept + ", job="
				+ job + "]";
	}
	
}

public class Sample33 {
	static Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args) {
		// 사용자 입력을 활용하여 입력한 부서코드에 해당하는 사원정보(사번, 이름, 부서명, 직무명)
		// 입력한 파일명으로 저장되도록 한다.(파일은 CSV 파일로 저장하여 엑셀로 열릴 수 있게 한다.)
		String fileName;
		List<EmpInfo> eList = new LinkedList<EmpInfo>();
		
		while(true) {
			System.out.print("저장할 파일의 이름을 입력하세요 : ");
			fileName = sc.next(); 	sc.nextLine();
			if(fileName.contains("\\") || fileName.contains("/") ||
				fileName.contains(":") || fileName.contains("*") || 
				fileName.contains("?") || fileName.contains("\"") || 
				fileName.contains("<") || fileName.contains(">") ||
				fileName.contains("|")) {	
				System.out.println("파일 이름에는 다음 문자를 사용할 수 없습니다"
						+ " -> \\ / : * ? \" < > |");
			} else break;
		}
	
	//사번, 이름, 부서명, 직무명
		System.out.println("사원 정보를 입력하세요.");	
		while(true) {
			System.out.print("사번을 입력하세요(공백제외) : ");
			if(!sc.hasNextInt()) {
				System.out.println("사번은 정수 값이어야만 합니다.");
				sc.nextLine();
			} else break;
		}
		int empId = sc.nextInt(); sc.nextLine();
			
		String cName = null;
		while(true) {
			System.out.print("이름을 입력하세요 : ");
			cName = sc.nextLine().trim();
			cName = cName.replace(" ", "");
			int count = 0; // while문 빠져나갈 근거			
			if(cName.contains("0") || cName.contains("1") ||
				cName.contains("2") || cName.contains("3") ||
				cName.contains("4") || cName.contains("5") ||
				cName.contains("6") || cName.contains("7") ||
				cName.contains("8") || cName.contains("9")) {
				System.out.println("이름에는 숫자를 포함할 수 없습니다.");		
				count++;
			}
			for(int i = 0; i < cName.length(); i++) {
				char resChar = cName.charAt(i);
				switch (resChar) {
					case '\\' : case '/' : case ':' : case '*' : case '?' : 	
					case '\'' : case '<' : case '>' : case '|' : case '!' :	
					case '@' : case '#' : case '$' : case '%' : case '^' :	
					case '&' : case '(' : case ')' : case '-' :	case '_' : 
					case '=' : case '+' : case ';' : case '"' :
						System.out.println("이름에는 특수문자를 포함할 수 없습니다.");
						count++;
   		         }						
			} 
			if(count == 0) {
				break;				
			}
		}
		String name = cName;	
	
		System.out.print("부서명을 입력하세요 : ");
		String dept = sc.nextLine().trim();
		System.out.print("직무명을 입력하세요 : ");
		String job = sc.nextLine().trim();
		
		//EmpInfo emp = new EmpInfo(empId, name, dept, job);
		eList.add(new EmpInfo(fileName, empId, name, dept, job));
		System.out.println(eList);
		
		
		FileWriter fw;
		try {
			fw = new FileWriter(fileName + ".csv");
				
		BufferedWriter bw = new BufferedWriter(fw);
		bw.write("EMP_ID,NAME,DEPARTMENT,JOB\r\n"); 

		for(EmpInfo e : eList) {		
			bw.write(e.getEmpId() + "," + e.getName() + "," + e.getDept() 
			+ "," + e.getJob() +"\r\n");
		}
		
		bw.close();
		fw.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}	
		
	
	}
}