TIL(Today I Learned)_230726

Aspyn Choiยท2023๋…„ 7์›” 26์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
11/37

โ˜๐Ÿป์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ

1. ๊ฐ์ฒด ๋ฐฐ์—ด

- ๊ฐ์ฒด ๋ฐฐ์—ด์ด๋ž€

: ๊ฐ์ฒด ์ฐธ์กฐํ˜• ๋ณ€์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด.
: ๋ฐฐ์—ด์˜ ์ž๋ฃŒํ˜•์„ ํด๋ž˜์Šค๋ช…(์‚ฌ์šฉ์ž ์ •์˜ ์ž๋ฃŒํ˜•)์œผ๋กœ ์ง€์ •ํ•˜์—ฌ ํ™œ์šฉ

- ๊ฐ์ฒด๋ฐฐ์—ด ์˜ˆ์ œ

package edu.kh.oarr.model.service;

import java.util.Arrays;
import java.util.Scanner;

import edu.kh.oarr.model.vo.Member;

public class MemberService {
	
	Scanner sc = new Scanner(System.in);
	
	// Member ๋‹ค์„ฏ์นธ์งœ๋ฆฌ ๊ฐ์ฒด๋ฐฐ์—ด ์„ ์–ธ ๋ฐ ํ• ๋‹น
	private Member[] memberArr = new Member[5];
	// ํ˜„์žฌ ๋กœ๊ทธ์ธํ•œ ํšŒ์›์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ ์„ ์–ธ
	private Member loginMember = null;
	
	public MemberService() {
		//memberArr ๋ฐฐ์—ด 0,1,2๋ฐฐ์—ด ์ธ๋ฑ์Šค ์ดˆ๊ธฐํ™”
		memberArr[0] = new Member("user01", "pass01", "ํ™๊ธธ๋™", 30, "์„œ์šธ");
		memberArr[1] = new Member("user02", "pass02", "๊ณ„๋ณด๋ฆฐ", 25, "๊ฒฝ๊ธฐ");
		memberArr[2] = new Member("user03", "pass03", "๊ณ ๊ธธ๋™", 45, "๊ฐ•์›");
	}
	
	// ๋ฉ”๋‰ด ์ถœ๋ ฅ์šฉ ๋ฉ”์„œ๋“œ
	public void displayMenu() {
		// ๋ฐ˜ํ™˜ํ˜•
		
		int menuNum = 0; // ๋ฉ”๋‰ด ์„ ํƒ์šฉ ๋ณ€์ˆ˜
		
		// ๋ฌด์กฐ๊ฑด ํ•œ๋ฒˆ์€ ๋ฐ˜๋ณต -> do  while
		do {
			System.out.println("====ํšŒ์› ์ •๋ณด ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ v.2====");
			System.out.println("1. ํšŒ์›๊ฐ€์ž…");
			System.out.println("2. ๋กœ๊ทธ์ธ");
			System.out.println("3. ํšŒ์›์ •๋ณด ์กฐํšŒ");
			System.out.println("4. ํšŒ์›์ •๋ณด ์ˆ˜์ •");
			System.out.println("5. ํšŒ์› ๊ฒ€์ƒ‰(์ง€์—ญ)");
			System.out.println("0. ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
			
			System.out.print("๋ฉ”๋‰ด ์ž…๋ ฅ : ");
			menuNum = sc.nextInt();
			sc.nextLine(); // ์ž…๋ ฅ ๋ฒ„ํผ์— ๋‚จ์€ ๊ฐœํ–‰๋ฌธ์ž ์ œ๊ฑฐ์šฉ
			
			switch(menuNum) {
			case 1 : System.out.println(signup()); break;
			case 2 : System.out.println(login()); break;
			case 3 : System.out.println(selectMember()); break;
			case 4 : 
				int result = updateMember();
				if(result == -1) {
					System.out.println("๋กœ๊ทธ์ธ ํ›„ ์ด์šฉํ•ด์ฃผ์„ธ์š”.");
				}else if(result ==0) {
					System.out.println("ํšŒ์›์ •๋ณด ์ˆ˜์ • ์‹คํŒจ(๋น„๋ฐ€๋ฒˆํ˜ธ ๋ถˆ์ผ์น˜");
				}else {
					System.out.println("ํšŒ์›์ •๋ณด๊ฐ€ ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
				}
				; break;
			case 5 : searchRegion(); ; break;
			case 0 : System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ..."); break;
			default : System.out.println("์ž˜๋ชป์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
			}
			
			
		}while(menuNum != 0); // menuNum์ด 0์ด๋ฉด ๋ฐ˜๋ณต ์ข…๋ฃŒ
		
	}
	
	// memberArr์— ๋น„์–ด์žˆ๋Š” index๋ฒˆํ˜ธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
	// ๋‹จ, ๋น„์–ด์žˆ๋Š” ์ธ๋ฑ์Šค๊ฐ€ ์—†๋‹ค๋ฉด -1 ๋ฐ˜ํ™˜
	public int emptyIndex() {
		
		for(int i = 0; i < memberArr.length; i++) {
			if(memberArr[i] == null) {
				return i;
			}
		}
		return -1;
	}
	
	// ํšŒ์›๊ฐ€์ž… ๋ฉ”์„œ๋“œ
	public String signup() {
		
		System.out.println("\n************ํšŒ์›๊ฐ€์ž…************");
		
		// ๊ฐ์ฒด๋ฐฐ์—ด memberArr์— ๊ฐ€์ž…ํ•œ ํšŒ์› ์ •๋ณด๋ฅผ ์ €์žฅ
		// -> ์ƒˆ๋กœ์šด ํšŒ์›์ •๋ณด๋ฅผ ์ €์žฅํ•  ๊ณต๊ฐ„์ด ์žˆ๋Š”์ง€ ํ™•์ธ
		// 		๋นˆ ๊ณต๊ฐ„์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ์–ป์–ด์˜ค๊ธฐ -> ์ƒˆ๋กœ์šด ๋ฉ”์„œ๋“œ๋กœ ์ž‘์„ฑ
		
		int index = emptyIndex(); // memberArr๋ฐฐ์—ด์—์„œ ๋น„์–ด์žˆ๋Š” ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ๋ฐ˜ํ™˜
		
		if(index == -1) { // ๋น„์–ด์žˆ๋Š” ์ธ๋ฑ์Šค ์—†์„ ๊ฒฝ์šฐ > ํšŒ์›๊ฐ€์ž… ๋ถˆ๊ฐ€
			return "ํšŒ์›๊ฐ€์ž…์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.(์ธ์› ์ˆ˜ ์ดˆ๊ณผ)";
		}
		
		System.out.print("์•„์ด๋”” : ");
		String memberId = sc.next();
		
		System.out.print("๋น„๋ฐ€๋ฒˆํ˜ธ : ");
		String memberPw = sc.next();
		
		System.out.print("๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ : ");
		String memberPw2 = sc.next();
		
		System.out.print("์ด๋ฆ„ : ");
		String memberName = sc.next();
		
		System.out.print("๋‚˜์ด : ");
		int memberAge = sc.nextInt();

		System.out.print("์ง€์—ญ : ");
		String region = sc.next();
		
		if(memberPw.equals(memberPw2)) {
			
			// Member ๊ฐ์ฒด ์ƒ์„ฑํ•ด์„œ ํ• ๋‹น๋œ ์ฃผ์†Œ๋ฅผ memberArr์˜ ๋น„์–ด์žˆ๋Š” ์ธ๋ฑ์Šค์— ๋Œ€์ž…
			memberArr[index] = new Member(memberId, memberPw2, memberName, memberAge, region);
		//	memberInfo = memberArr[index];
					
			return "ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต!!";
			
		}else { // ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
			return "ํšŒ์›๊ฐ€์ž… ์‹คํŒจ!! (๋น„๋ฐ€๋ฒˆํ˜ธ ๋ถˆ์ผ์น˜)";
		}
	}
	
	// ๋กœ๊ทธ์ธ ๋ฉ”์„œ๋“œ
	public String login() {
		System.out.println("\n************๋กœ๊ทธ์ธ************");
		
		System.out.print("์•„์ด๋”” ์ž…๋ ฅ : ");
		String memberId = sc.next();
		System.out.print("๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ : ");
		String memberPw = sc.next();
		
		// 1) memberArr๋ฐฐ์—ด ๋‚ด ์š”์†Œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ ‘๊ทผํ•˜์—ฌ null์ด ์•„๋‹Œ์ง€ ํ™•์ธ
		
		for(int i = 0; i < memberArr.length; i++) {
			
			if(memberArr[i] != null) {
				// 2) ํšŒ์›์ •๋ณด์˜ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ์ž…๋ ฅ๋ฐ›์€ ๋‚ด์šฉ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธ
				if(memberId.equals(memberArr[i].getMemeberId()) && memberPw.equals(memberArr[i].getMemeberPw())) {
					
					// 3) ๋กœ๊ทธ์ธ ํšŒ์›์ •๋ณด ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ณ€์ˆ˜ loginMember์—
					// ์ž…๋ ฅ๋ฐ›์€ ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธ
					loginMember = memberArr[i];
					break;
				}
			}		
		}
		
		if(loginMember == null) {
			return "์•„์ด๋”” ๋˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.";
		}else {
			return loginMember.getMemeberName() + "๋‹˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.";
		}
		
		
	}

	// ์ •๋ณด ์กฐํšŒ ๋ฉ”์„œ๋“œ
	public String selectMember() {
		
		System.out.println("\n*********ํšŒ์› ์ •๋ณด ์กฐํšŒ*********");
		
		if(loginMember == null) {
			return "๋กœ๊ทธ์ธ ํ›„ ์ด์šฉํ•ด์ฃผ์„ธ์š”";
		}
		
		String str = "์ด๋ฆ„ : " + loginMember.getMemeberName();
		str += "\n์•„์ด๋”” : " + loginMember.getMemeberId();
		str += "\n๋‚˜์ด : " + loginMember.getMemeberAge() + "์„ธ";
		
		return str;
	}

	// ์ •๋ณด ์ˆ˜์ • ๋ฉ”์„œ๋“œ
	public int updateMember() {
		
		System.out.println("\n**********ํšŒ์› ์ •๋ณด ์ˆ˜์ •**********");
		
		if(loginMember == null) {
			return -1;
		}
		
		System.out.print("์ˆ˜์ •ํ•  ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : ");
		String memberName = sc.next();
		System.out.print("์ˆ˜์ •ํ•  ๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : ");
		int memberAge = sc.nextInt();
		System.out.print("๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. : ");
		String memberPw = sc.next();
		
		if(memberPw.equals(loginMember.getMemeberPw())) {
			
			loginMember.setMemeberName(memberName);
			loginMember.setMemeberAge(memberAge);
			return 1;
		}else {
			return 0;
		}
	}
	
	// ํšŒ์› ๊ฒ€์ƒ‰(์ง€์—ญ)
	public void searchRegion() {
		
		System.out.println("\n********ํšŒ์› ๊ฒ€์ƒ‰(์ง€์—ญ)********");
		
		System.out.print("์ง€์—ญ ์ž…๋ ฅ : ");
		String inputRegion = sc.next();
		
		boolean flag = true; // ๊ฒ€์ƒ‰๊ฒฐ๊ณผ ์‹ ํ˜ธ์šฉ ๋ณ€์ˆ˜
		
		// 1) memberArr ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ ์ˆœ์ฐจ ์ ‘๊ทผ
		for(int i = 0; i < memberArr.length; i++) {
			
			// 2) memberArr[i] ์š”์†Œ๊ฐ€ null์ด๋ฉด ๋ฐ˜๋ณต ์ข…๋ฃŒ]
			if(memberArr[i] == null) {
				break;
			}
			
			// 3) memberArr[i] ์ €์žฅ๋œ ์ง€์—ญ์ด ์ž…๋ ฅ๋ฐ›์€์ง€์—ญ๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ
			// ํšŒ์›์˜ ์•„์ด๋””, ์ด๋ฆ„ ์ถœ๋ ฅ
			
			if(inputRegion.equals(memberArr[i].getRegion())) {
				
				System.out.printf("์•„์ด๋”” : %s, ์ด๋ฆ„ : %s\n", memberArr[i].getMemeberId(), memberArr[i].getMemeberName());
				
				flag = false;
			}
			
		}
		
		if(flag) {
			System.out.println("์ผ์น˜ํ•˜๋Š” ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
		}
	}
}

0๊ฐœ์˜ ๋Œ“๊ธ€