map을 이용한 비디오 대여 프로그램(JAVA)

Soozoo·2024년 6월 25일

JAVA

목록 보기
23/41

비디오 관리 프로그램 작성 및 설명

이 프로그램은 비디오를 관리하는 기능을 제공하는 간단한 콘솔 애플리케이션입니다. 사용자는 메뉴를 선택하여 비디오를 추가, 삭제, 수정, 대여, 반납하는 기능을 수행할 수 있습니다. 또한 대여된 비디오의 목록을 볼 수 있는 기능도 제공됩니다.

코드설명:

Video 클래스

package Quiz;

public class Video {
    private String no;
    private String title;
    private String category;
    private String lendName;
    private String lendDate;
    private boolean lend;

    public Video(String no, String category, String title) {
        this.no = no;
        this.title = title;
        this.category = category;
        this.lendName = "";
        this.lendDate = "";
        this.lend = false;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getLendName() {
        return lendName;
    }

    public void setLendName(String lendName) {
        this.lendName = lendName;
    }

    public String getLendDate() {
        return lendDate;
    }

    public void setLendDate(String lendDate) {
        this.lendDate = lendDate;
    }

    public String getNo() {
        return no;
    }

    public boolean isLend() {
        return lend;
    }

    public void setLend(boolean lend) {
        this.lend = lend;
    }

    @Override
    public String toString() {
        return "Video [no=" + no + ", title=" + title + ", category=" + category +
                ", lend=" + lend + ", lendName=" + lendName + ", lendDate=" + lendDate + "]";
    }
}

VideoManage

package Quiz;

import java.util.*;

public class VideoManage {

	HashMap<String, Video> vd = new HashMap();
	String title, category, lendName, no, lendDate;
	boolean lend;
	Scanner sc = new Scanner(System.in);

	public void star() {
		System.out.println("***************************************************");
	} // 경계선 출력

	public void add() {
		System.out.println("비디오의 고유번호를 입력하세요.");
		no = sc.nextLine();
		System.out.print("비디오의 제목을 입력하세요.");
		title = sc.nextLine();
		System.out.println("비디오의 장르를 입력하세요.");
		category = sc.nextLine();

		Video video = new Video(no, category, title);

		vd.put(no, video);
		System.out.println(
				"비디오를 추가 했습니다." + "비디오 번호:" + video.no + " 비디오 이름:" + video.title + " 비디오 장르:" + video.category);

	} // add END

	public void del() {
		System.out.println("삭제할 비디오 고유번호를 입력하세요.");
		no = sc.nextLine();
		if (vd.containsKey(no)) {
			vd.remove(no);
			System.out.println("비디오가 삭제되었습니다.");
		} else {
			System.out.println("등록된 비디오가 없습니다.");
		}
	} // del END

	public void print() {
		if (vd.isEmpty()) {
			System.out.println("등록된 비디오가 없습니다.");
		} else {
			for (Video video : vd.values()) {
				System.out.println("비디오 번호:" + video.no + " 비디오 이름:" + video.title + " 비디오 장르:" + video.category);
			}
		}
	} // print END

	public void set() {
		int num;
		System.out.println("변경할 비디오의 고유번호를 입력하세요.");
		String no = sc.nextLine();
		if (vd.containsKey(no)) {
			Video video = vd.get(no);
			System.out.println("수정 1.비디오 이름 2.비디오 장르");
			num = sc.nextInt();
			sc.nextLine();

			if (num == 1) {
				star();
				System.out.println("변경할 비디오의 이름을 입력하세요.");
				String newtitle = sc.nextLine();
				video.setTitle(newtitle);
			} // SECOND if END

			else if (num == 2) {
				star();
				System.out.println("변경할 비디오의 장르를 입력하세요.");
				String newcategory = sc.nextLine();
				video.setCategory(newcategory);
				System.out.println("비디오의 장르가 변경되었습니다.");
			} // else if END

		} // FIRST if END
		else {
			System.out.println("등록된 비디오가 없습니다.");
		}

	} // set END

	public void vlend() {
		System.out.println("대여하실 비디오의 고유번호를 입력하세요.");
		String no = sc.nextLine();
		if (vd.containsKey(no)) {
			Video video = vd.get(no);
			if (!video.isLend()) {
				System.out.println("대여자 이름을 입력하세요.");
				lendName = sc.nextLine();
				System.out.println("대여날짜를 입력하세요.");
				lendDate = sc.nextLine();
				video.setLend(true);
				video.setLendName(lendName);
				video.setLendDate(lendDate);
			} // SECOND IF END
			else if (video.isLend()) {
				System.out.println("비디오가 대여중입니다.");
			}
		} // Frist If END
		else {
			System.out.println("등록된 비디오가 없습니다.");
		}
	} // vlend END

	public void vreturn() {
		System.out.println("반납하실 비디오의 고유번호를 입력하세요.");
		String no = sc.nextLine();
		if (vd.containsKey(no)) {
			Video video = vd.get(no);
			if (video.isLend()) {
				System.out.println("반납이 완료되었습니다.");
				video.setLend(false);
			} // SECOND IF END
		} // Frist If END
		else {
			System.out.println("등록된 비디오가 없습니다.");
		}
	} // vreturn END

	public void vlist() {
		if (!vd.isEmpty()) {
			for (Video video : vd.values()) {
				if (video.isLend()) {
					System.out.println(video);
				}
			}
		} else {
			System.out.println("등록된 비디오가 없습니다.");
		}
	}
}

mainEntry

package Quiz;

import java.util.*;

public class MainEntry {
	public static void main(String[] args) {
		
		VideoManage vm = new VideoManage();
		Scanner sc = new Scanner(System.in);

		while (true) {
			System.out.println("1. 비디오 추가 2.비디오 삭제 3.비디오 리스트 출력 4.비디오 수정 "
					+ "5.비디오 대여 6.비디오 반납 7.대여한 비디오 리스트 출력 8.프로그램 종료");
			int choice;
			choice = sc.nextInt();
			switch (choice) {
			case 1:
				System.out.println("비디오추가를 선택하셨습니다.");
				vm.add();// add
				break;
			case 2:
				System.out.println("비디오삭제를 선택하셨습니다."); // del
				vm.del();
				break;
			case 3:
				System.out.println("비디오 리스트 출력을 선택하셨습니다."); //print
				vm.print();
				break;
			case 4:
				System.out.println("비디오 수정을 선택하셨습니다."); //set
				vm.set();
				break;
			case 5:
				System.out.println("비디오 대여를 선택하셨습니다."); // vlend
				vm.vlend();
				break;
			case 6:
				System.out.println("비디오 반납을 선택하셨습니다."); //vreturn
				vm.vreturn();
				break;
			case 7:
				System.out.println("대여한 비디오 리스트 출력을 선택하셨습니다."); //vlist
				vm.vlist();
				break;
			case 8:
				System.out.println("프로그램 종료합니다.");
				System.exit(0);
				break;
			default:
				System.out.println("잘못된 경로 입니다. 숫자를 다시 입력하세요.");
			}
		}
	}
}
profile
넙-죽

0개의 댓글