👨‍💻Programming/JAVA & C언어 예제

JAVA- 응용SW 기초기술 활용 TEST - 코딩 테스트 시험 2차

코너(Corner) 2020. 11. 29.
반응형

JAVA- 응용SW 기초기술 활용 TEST - 코딩 테스트 시험 2차

 

 

코딩 테스트 시험 

문제 :  배달 현황 프로그램, 상속 및 다형성을 사용

강남,강북,강동,강서지구 총 4개의 지구가 있으며 각 지구엔 배달 담당자가 1명씩 고정 되어있다.

강동 : 장동욱

강서 : 박영수

강남 : 이기영

강북 : 김찬영  

 배달 단가표는 이렇다.

배달 시간이 1시간 이내-> 배달 요금 1000원

배달시간 1~2시간 이내 -> 배달 요금 2000원

배달시간 2~3시간 이내 -> 배달 요금 3000원 

배달시간 3시간 이상 -> 4000원

 

0~100개 배달량 => 배달 단가 1000원

100~200개 배달량 => 배달 단가 2000원

200~300개 배달량 => 배달 단가 3000원

300개 이상 배달량 => 배달 단가 4000원

 

배달 시간이 3이고 50개를 배달하면 => 4천원이다.

배달 시간이 2시간이고 350개를 배달하면 => 7천원이다.

 

 

배달담당자는 바뀌지 않으며, 판매량은 누적되지 않고 입력해서 추가한 만큼의 요금을 보여주면 된다.

각 지구별로 현황 출력문을 보여주고, 전체 지구의 현황을 출력하라.

 

UML과  

콘솔 실행은 javac를 이용할 것

 

 


출력문

 

UML


소스 코드

// business 클래스

import java.util.Scanner;

public class Business {
	Scanner sc = new Scanner(System.in);
	Seoul[] delivery = new Seoul[40]; // 서울 배열
	Seoul[] north = new Seoul[10]; // 강북
	Seoul[] south = new Seoul[10]; // 강남
	Seoul[] west = new Seoul[10]; // 강서
	Seoul[] east = new Seoul[10]; // 강동
	 int n=0,s=0,w=0,e=0,i=0; // 배열 인덱스 체크
	 int act; // 입력용 변수 
	
	public void input() { // 입력문
		System.out.println("배달 지구(1.강동지구|2.강서지구|3.강남지구|4.강북지구");
		do {
			System.out.println("1~4번중 선택하세요.");
			act = sc.nextInt();
		} while (act < 1 && act > 4 );
		
		if(act == 1 ) {
			Seoul s = new East(); // 강동지구 선택
			System.out.print("배달 시간 ? > ");
			s.time = sc.nextInt(); // 배달 시간
			System.out.print("배달량 개수 ? > ");
			s.cnt = sc.nextInt();

			deliveryPay(s); 	// 배달 요금 계산 메소드 자리
			Seoul east1 = new East("장동욱", s.time, s.cnt, s.pay);
			east[e++] = east1; // 배열에 저장
			delivery[i++] = east1;
		} else if ( act == 2 ) {
			Seoul s1 = new West(); // 강서지구 선택
			System.out.print("배달 시간 ? > ");
			s1.time = sc.nextInt(); // 배달 시간
			System.out.print("배달량 개수 ? > ");
			s1.cnt = sc.nextInt();

			deliveryPay(s1);
			s1 = new West("박영수", s1.time, s1.cnt,s1.pay);
			west[w++] = s1;
			delivery[i++] = s1;
		} else if ( act == 3 ) {
			Seoul s2 = new South(); 
			System.out.print("배달 시간 ? > ");
			s2.time = sc.nextInt(); // 배달 시간
			System.out.print("배달량 개수 ? > ");
			s2.cnt = sc.nextInt();

			deliveryPay(s2);
			s2 = new South("이기영", s2.time, s2.cnt, s2.pay);
			south[s++] = s2;
			delivery[i++] = s2;
		} else if ( act == 4 ) {
			Seoul s3 = new North();
			System.out.print("배달 시간 ? > ");
			s3.time = sc.nextInt(); // 배달 시간
			System.out.print("배달량 개수 ? > ");
			s3.cnt = sc.nextInt();

			deliveryPay(s3);
			s3 = new North("김찬영", s3.time, s3.cnt, s3.pay);
			north[n++] = s3;
			delivery[i++] = s3;
		}
		return;
		
	}
	public void Out(Seoul[] e) { // 개별 출력문
		System.out.println("------------------");
		System.out.println(e[0]+" 입니다.");
		for(int j = 0; j < east.length; j++ ) {
			if(e[j] == null ) break;
			System.out.println(e[j].name+"\t"+e[j].time+"\t"+e[j].cnt+"\t"+e[j].pay+"\n"); 
		}
		System.out.println("------------------");
	}
	public void deliveryPay(Seoul s) { // 배달 계산 메소드  
		
		// 배달 시간이 1시간 이내
		if (s.time <= 1 && s.time > 0) {
			s.pay = 1000; // 배달요금 천원
		} else if ( s.time > 1 && s.time < 2 ) { // 1~2
			s.pay = 2000;
		} else if ( s.time >= 2 && s.time < 3 ) { // 2~3
			s.pay = 3000;
		} else if ( s.time >= 3) { //3시간 이상
			s.pay = 4000;
		} else {
			System.out.println("ERROR 잘못된 시간!!");
		}
		
		if (s.cnt > 0 && s.cnt <= 100 ) { // 배달량  계산 
			s.val = 1000; // 단가 지정
		} else if ( s.cnt > 100 && s.cnt <= 200 ) {
			s.val = 2000;
		} else if ( s.cnt > 200 && s.cnt <= 300 ) {
			s.val = 3000;
		} else if ( s.cnt > 300 ) {
			s.val = 4000;
		} else { 
			System.out.println("-ERROR 잘못된 개수-");
		}
		s.pay = s.val + s.pay;
		
	}
	 
	
}

 

// main 클래스
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Business bs = new Business(); // 인스턴스화 
		
		int act; // 선택 변수
		
		System.out.println("------------------");
		System.out.println("배달 현황 프로그램");
		System.out.println("------------------");
		System.out.println("Programmer : by_천기훈");
			while(true) {
			do {
				System.out.println("--------------------------------");
				System.out.println("1. 입력 | 2. 출력 | 3. 종료");
				System.out.println("--------------------------------");
				act = sc.nextInt();
			} while( act < 1 && act > 3 );
			
			switch (act) {
			case 1: bs.input() ;break;
			case 2: 
			System.out.println("1. 개별 지구 확인 | 2. 모든 지구 | 3. 종료");
			act = sc.nextInt();
			if (act == 1 ) {
				System.out.println("1.강동 2.강서 3.강남 4.강북");
				act = sc.nextInt();
				if(act ==1 ) { bs.Out(bs.east); break; }
				if(act ==2 ) { bs.Out(bs.west); break; }
				if(act ==3 ) { bs.Out(bs.south); break; }
				if(act ==4 ) { bs.Out(bs.north); break; }
			}
			else if(act == 2 ) { 
				System.out.println("--------------------");
				bs.Out(bs.east);
				bs.Out(bs.west);
				bs.Out(bs.south);
				bs.Out(bs.north);
				break;
			} else { 	}
			case 3: System.out.println("프로그램 종료"); 
			sc.close(); System.exit(0);
			}
		}
		
		}
	}
 

 

// 부모, 자식 클래스
// 부모 클래스
// 1시간 이내 천원, 2시간 이내 2천원, 2~3시간이내 3천원, 3시간 이상 4천원
// 배달 단가 = 배달량 + 배달량 단가 + 배달시간
public class Seoul {
	Seoul s; // 배달 담당자 명 , toString 오버라이딩
	String name; // 배달 담당자 
	int time; // 배달 시간
	int val; // 배달 단가
	int cnt; // 배달량 
	int pay; // 배달 요금 
	public Seoul() {	}
	public Seoul(String name, int time,int cnt,int pay) {}
	public Seoul(String name, int cnt,int val,int time,int pay) {
		this.name=name;  this.time=time; this.cnt=cnt; this.val=val;
		this.pay=pay;
	}
}
class East extends Seoul { // 강동지구
	public East() {	}
	public East(String name, int time,int cnt,int pay) { this("장동욱", cnt, cnt, time, pay);}
	public East(String name, int cnt,int val,int time,int pay) {
		super("장동욱", cnt, val, time,pay);
	}
	@Override
		public String toString() {
		return "강동지구";
		}
}

class West extends Seoul { // 강서지구
	public West() {	}
	public West(String name, int time,int cnt,int pay) {
		this("박영수", cnt, cnt, time, pay);
	}
	public West(String name, int cnt,int val,int time,int pay) {
		super("박영수", cnt, val, time,pay);
	}
	@Override
		public String toString() {
		return "강서지구";
		}
}

class South extends Seoul { // 강남지구
	public South() {	}
	public South(String name, int time,int cnt,int pay) {
		this("이기영", cnt, cnt, time, pay);
	}
	public South(String name, int cnt,int val,int time,int pay) {
		super("이기영", cnt, val, time,pay);
	}
	@Override
		public String toString() {
		return "강남지구";
		}
}

class North extends Seoul { // 강북 지구 
	public North() {	}
	public North(String name, int time,int cnt,int pay) {
		this("김찬영", cnt, cnt, time, pay);
	}
	public North(String name, int cnt,int val,int time, int pay) {
		super("김찬영", cnt, val, time, pay);
	}
	@Override
		public String toString() {
		return "강북지구";
		}
}

 


 

반응형

댓글