728x90
반응형
<title></title>

package lang_p;
import java.util.Scanner;


 

//예외처리클래스
class MyExcep extends Exception // 예외처리 클래스 
{
  public MyExcep() { 
    super();
  }
  public void printStackTrace() 
  {
    System.out.println("잘못입력하셨습니다. 처음부터 다시 입력해주세요");
  }
}

//메인 클래스
public class Day0526_Homework_1 {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    Scanner sc = new Scanner(System.in);
    
    String [] str={"가위","바위","보"};
    
    while(true)
    {
      try
      {
        System.out.println("가위바위보(하나빼기)게임");
        for(int i=0; i<str.length;i++)
          System.out.print(i+"."+str[i]+" / ");
        System.out.println("3.종료하기");
        
        System.out.println("첫번째 낼 것을 선택해주세요")// 첫번째 낼것 input1 에 저장. 
        int input1=sc.nextInt();
        
        if(input1==3)
        {
          System.out.println("게임종료")
          break;
        }        
        else if(input1!=&& input1!=&& input1!=// 예외처리 
          throw new MyExcep();
                
        System.out.println("두번째 낼 것을 선택해주세요")// 두번째 낼것 input2에 저장 
        int input2=sc.nextInt();
        
        if(input2==3)
        {
          System.out.println("게임종료")
          break;
        }        
        else if(input2!=&& input2!=&& input2!=// 예외처리
          throw new MyExcep();
                
        int com1=(int)(Math.random()*3);
        int com2=(int)(Math.random()*3);
        
        System.out.println("게이머 : 1번["+str[input1]+"],2번["+str[input2]+"]");
        System.out.println("컴퓨터 : 1번["+str[com1]+"],2번["+str[com2]+"]");
        System.out.println("둘 중 어떤것을 내시겠습니까?");
        int input3=sc.nextInt();
    
        if(input3!=&& input3!=// 예외처리 
          throw new MyExcep();
        
        int input4=input1;
        if(input3==2)   // 둘 중 하나 고른것 input4 에 저장. 
          input4=input2;
        
        int com4=com1;
        if(Math.random()>5// 랜덤함수를 사용해서 컴퓨터가 2개 낸것 중 하나 고르도록. 
          com4=com2;
        
        String res="졌습니다.";
        
        if(input4==com4)
          res="비겼습니다.";
        else if(input4==&& com4==2)
          res="이겼습니다.";
        else if(input4==&& com4==0)
          res="이겼습니다.";
        else if(input4==&& com4==1)
          res="이겼습니다.";
        
        System.out.println("*****게임 결과*****");
        System.out.println("게이머 : "+str[input4]);
        System.out.println("컴퓨터 : "+str[com4]);
        System.out.println("결과 : "+res);
        System.out.println("");      
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    }//while문 종료
  }
}

// 입력부분에서 오류시 예외처리 발생 하도록 하였습니다.
// 예외처리 클래스, 입력클래스, 출력클래스 로 나누어 만들었습니다. 

package util_p;

import java.util.Calendar;
import java.util.Scanner;

//예외처리 클래스
class MyExcep extends Exception // 예외처리 클래스 
{
  int n;
  String []msg=
    {"잘못 입력하였습니다. 다시 입력 바랍니다.",
     "입사 날짜 부분을 잘못 입력하셨습니다. 다시 입력 바랍니다.",
     "부서 입력 부분을 잘못 입력하셨습니다. 다시 입력 바랍니다.",
     "직급 입력 부분을 잘못 입력하셨습니다. 다시 입력 바랍니다."
    };
  public MyExcep(int n) { 
    super();
    this.n=n;
  }
  public void printStackTrace() 
  {
    System.out.println(msg[n]);
  }
}

//입력 클래스
class InputInfo // 입력받는 클래스 
{
  String input1;
  int monthCnt[] 0,31,28,31,30,31,30,31,31,30,31,30,31 }
  
  public InputInfo() 
  {
    Scanner sc = new Scanner(System.in);
        
    while(true)
    {      
      try
      {
        System.out.println("");
        System.out.println("******************************************");
        System.out.println("사원번호를 입력해주세요.");
        System.out.println("ex)20020320a2lsh");
        System.out.println("입사일 :20020320, a - 부서명, 2-직급, lsh - 이름");    
        System.out.println("부서명 : a 영업, b 기획, c 생산, d 개발 ");
        System.out.println("0 사장, 1 부장, 2 과장, 3 대리, 4 사원");
        System.out.print(">>>");
          
        input1=sc.next();
        if(input1.length()!=13// 총 자릿수가 13자리가 아닐시 예외처리 
          throw new MyExcep(0);
        
        int month=Integer.parseInt(input1.substring(4,6));
        int date=Integer.parseInt(input1.substring(6,8));
        char team=input1.charAt(8);
        char position=input1.charAt(9);
        
        if(month>12 || month==0// 달이 1~12월이 아닐때 예외처리 
          throw new MyExcep(1);
        
        if(date>monthCnt[month|| date==0// 날짜가 1~30일(31일)이 아닐때 예외처리
          throw new MyExcep(1);
        
        if(team!='a'&& team!='b'&& team!='c' && team!='d'// 부서입력부분 에러일때
          throw new MyExcep(2);
        
        if(position!='0' && position!='1' && position!='2' //직급입력부분 에러일때
            && position!='3' && position!='4')
          throw new MyExcep(3);
              
        break;      
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    }  
  }
  
  String output() // 출력클래스로 String input1 전달 메소드
  {
    return input1;
  }
}

//개인정보 저장 및 출력 클래스
class PersonInfo // 개인정보가 저장 및 출력 되는 클래스. 
{
  static String[] daystr={"","일","월","화","수","목","금","토"};
  
  Calendar today=Calendar.getInstance()// 금일날짜 달력생성
  Calendar join=Calendar.getInstance()// 입사날짜 달력생성
  Calendar retire=Calendar.getInstance()//정년퇴임날짜 달력생성
  Calendar party=Calendar.getInstance()// 퇴임식날짜 달력생성 
  
  int jYear, jMonth, jDate, jjam=0; long dDay=0;
  String team="미입력", position="미입력", name;
  
  String[] teamArr={"영업","기획","생산","개발"};
  char[] teamcode={'a','b','c','d'};
  String[] positionArr={"사장","부장","과장","대리","사원"};
  char[] positioncode={'0','1','2','3','4'};
  
  public PersonInfo(String input1// 사원번호가 생성자로 입력됨. 
  {
    jYear=Integer.parseInt(input1.substring(0,4));
    jMonth=Integer.parseInt(input1.substring(4,6));
    jDate=Integer.parseInt(input1.substring(6,8));
         
    for(int i=0; i<teamArr.length; i++)
    {
      if(input1.charAt(8)==teamcode[i])
        team=teamArr[i];
    }  
    for(int i=0; i<positionArr.length; i++)
    {
      if(input1.charAt(9)==positioncode[i])
        position=positionArr[i];
    }
      
    name=input1.substring(10);
        
    join.set(jYear, jMonth-1, jDate)// 입사일 날짜 설정.
    retire.set(jYear+30, jMonth-1, jDate)//정년퇴임 날짜 설정 (30년뒤)
    party.set(jYear+30, jMonth-1, jDate)//퇴임식날짜 설정 
    
    if(party.get(Calendar.DAY_OF_WEEK)!=5// 퇴임식이 목요일이 아닐때
    {
      for(int i=1; i<;i++)
      {
        party.add(Calendar.DATE,-1);
        if(party.get(Calendar.DAY_OF_WEEK)==5// 목요일이 될때까지 날짜당기기
          break;
      }
    }

   dDay=(party.getTimeInMillis()-today.getTimeInMillis())/(1000*60*60*24);
  //디데이 입력

   
    jjam=(today.get(Calendar.YEAR)-join.get(Calendar.YEAR));  // 호봉 입력
    
    printInfo();
  }
  
  void printInfo() // 출력 메소드
  {
    System.out.println("******************************************");
    System.out.println("성명          : "+name);
    System.out.println("부서          : "+team);
    System.out.println("직급          : "+position);
    System.out.println("호봉          : "+jjam+" 호봉");
    System.out.println("입사일       : "+print(join));
    System.out.println("정년퇴임일 : "+print(retire));
    System.out.println("퇴임식       : "+print(party));

    System.out.println("D-DAY       : "+dDay+"일");
    System.out.println("*"+team+"부 "+name+""+position+"님 만수무강하세요~!*");
  }
  
  static String print(Calendar bb// 달력출력용 메소드 
  {
    return bb.get(Calendar.YEAR)+"-"+
        ((bb.get(Calendar.MONTH))+1)+"-"+
        bb.get(Calendar.DATE)+"("+
        daystr[bb.get(Calendar.DAY_OF_WEEK)]+")";
  }
}

//메인 클래스
public class Day0526_Homework2 
{
  public static void main(String[] args)   
  {
    InputInfo p1=new InputInfo();
    
    PersonInfo n1=new PersonInfo(p1.output());        
  }
  
}

 

 

728x90
반응형
블로그 이미지

nineDeveloper

안녕하세요 현직 개발자 입니다 ~ 빠르게 변화하는 세상에 뒤쳐지지 않도록 우리모두 열심히 공부합시다 ~! 개발공부는 넘나 재미있는 것~!

,