본문 바로가기
Web Programming/Code

[Workshop] 영화 등록/업데이트/검색

by Classic! 2020. 5. 3.

- 영화 정보를 담은 Movie 클래스

- MovieMgr로 Movie클래스 관리

- MovieTest로 MovieMgr 클래스 실행

 

[MovieMgr 클래스]

movie 등록, 검색, 삭제 프로그램

package com.service;
import com.vo.Movie;

public class MovieMgr {
	private Movie[ ] movies = new Movie[100];
	private int index;

	//싱글톤
	private static MovieMgr mmgr=new MovieMgr();
	private MovieMgr() {}
	public static MovieMgr getInstance() {
		return mmgr;
	}		
	
	public void add(Movie m) {
			movies[index++]=m;
	}
	
	/**
	 * @return nmovies
	 */
	public Movie[] search() {
		Movie[] nmovies= new Movie[index];
		for(int i=0;i<index;i++)
			nmovies[i]=movies[i];
		return nmovies;
	}
	
	/**
	 * @param title
	 * @return improved_search_movie
	 */
	public Movie[] search(String title) { //Movie[] search() 메서드 오버로딩
		Movie[] search_movie=new Movie[index]; 
		int temp = 0;
		for (Movie m:movies) {
			if(m.getTitle().contains(title))
				search_movie[temp++]=m;
		}
		Movie[] improved_search_movie=new Movie[temp];
		for (int i=0;i<temp;i++) improved_search_movie[i]=search_movie[i];
		return improved_search_movie;
	}
	
	/**
	 * @param name
	 * @return search_director
	 */
	public Movie[] searchDirector(String name) {
		int size = 0;
		for (int i=0;i<index;i++) {
			if(movies[i].getDirector().contains(name)) size++;
			}
		Movie[] search_director=new Movie[size];
		for(int i=0;i<index;i++) {
			if (movies[i].getDirector().contains(name))
				search_director[--size]=movies[i];
		}
		return search_director;
	}
	
	/**
	 * @param genre
	 * @return search_genre
	 */
	public Movie[] searchGenre(String genre) {
		int temp=0;
		Movie[] search_genre=new Movie[index]; 
		for (Movie m:movies)
			if(m.getGenre().contains(genre)) 
				search_genre[temp++]=movies[temp];
		return search_genre;
	}
	
	/**
	 * @param title
	 */
	public void delete(String title) {
		for (int i=0;i<index;i++) {
			if(movies[i].getTitle().contains(title)) 
				for(int k=i; k<index; k++) {
					movies[k] = movies[k+1];
				}
				movies[index] = null;
				index--;			
		}	
	}
	/**
	 * @return index
	 */
	public int getSize() {
		return index;
	}
}

 

[MoveTest 클래스]

package com.test;

import java.util.Arrays;
import java.util.Scanner;
import com.service.MovieMgr;
import com.vo.Movie;

public class MovieTest {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("<<영화 관리 프로그램>>");
		MovieMgr mvManager = MovieMgr.getInstance();
		
		boolean flag = true;
		while(flag) {
			System.out.println("1. 영화 정보 입력");
			System.out.println("2. 영화 정보 전체 검색");
			System.out.println("3. 영화명 검색");
			System.out.println("4. 영화 장르별 검색");
			System.out.println("5. 영화 정보 삭제");
			System.out.println("0. 종료");
			
			int selectNum = sc.nextInt();
			switch(selectNum) {
			case 1:
				System.out.print("영화 명을 입력해주세요 : ");
				String mvName = sc.next();
				System.out.print("감독 명을 입력해주세요 : ");
				String mvDir = sc.next();
				System.out.print("등급을 입력해주세요 : ");
				int mvGr = sc.nextInt();
				System.out.print("장르를 입력해주세요 : ");
				String mvGre = sc.next();
				System.out.print("요약을 입력해주세요 : ");
				String mvSumm = sc.next();
				mvManager.add(new Movie(mvName, mvDir, mvGr, mvGre, mvSumm));
				System.out.println();
				break;
			case 2:
				System.out.println(Arrays.toString(mvManager.search()));
				break;
			case 3:
				System.out.println("찾고자하는 제목을 입력하세요 : ");
				mvName = sc.next();
				System.out.println(Arrays.toString(mvManager.search(mvName)));
				break;
			case 4:
				System.out.print("찾고자하는 장르를 입력하세요 : ");
				mvGre = sc.next();
				System.out.println(Arrays.toString(mvManager.searchGenre(mvGre)));
				break;
			case 5:
				System.out.print("삭제하고자하는 제목을 입력하세요 : ");
				mvName = sc.next();
				mvManager.delete(mvName);
				System.out.println("삭제완료");
				break;
			case 0:
				flag=false;
				break;
			}			
		}		
	}
}

'Web Programming > Code' 카테고리의 다른 글

[Workshop] Book 등록/검색/수정/삭제  (0) 2020.05.01

댓글