-
메서드 ATM ||오버로딩 || this.과거⚰️ 2020. 12. 11. 18:13
오버 로딩(over loading)
한 클래스 내에 같은 이름으로 다른 종류의 인자를 받는 메서드를 만드는 것이다.
이름은 같아야하고 매개변수의 순서 타입 개수가 달라야 한다.
오버 라이드와 이름은 비슷하지만 다름 헷갈리지 않게 주의
package Day052; import java.util.Scanner; /* * # 영수증 출력하기 : 클래스[변수] * 1. 햄버거 주문을 받아 영수증을 출력한다. * 2. 출력내용은 각 메뉴의 주문 수량과 총 금액 및 잔돈을 표시한다. */ class BurgerShop{ Scanner scan = new Scanner(System.in); String name = ""; // 가게 이름 int[] arPrice = { 2500, 3800, 1500, 1000}; String[] arMenu = {"치즈버거", "불고기버거", "감자튀김", "콜 라"}; int[] arCount = new int[4]; int total = 0; void showMeu() { System.out.println("===== " + name + " ====="); for(int i=0; i<arMenu.length; i++) { System.out.println((i+1) + "." + arMenu[i] + ": " + arPrice[i] + "원"); } System.out.println("5.종료하기"); } void select1() { arCount[0] += 1; } void select2() { arCount[1] += 1; } void select3() { arCount[2] += 1; } void select4() { arCount[3] += 1; } void getReceipt() { System.out.println("===== 영수증 ====="); for(int i=0; i<arMenu.length; i++) { System.out.println((i + 1) + "." + arMenu[i] + ": " + arCount[i] + "개"); total += arPrice[i] * arCount[i]; } System.out.println("총 금액 : " + total + "원"); } void run() { while(true) { // 메뉴 출력 showMeu(); // 메뉴 선택 System.out.print("메뉴를 선택하세요 : "); int choice = scan.nextInt(); // 치즈버거 if(choice == 1) { select1(); } // 불고기버거 else if(choice == 2) { select2(); } // 감자튀김 else if(choice == 3) { select3(); } // 콜라 else if(choice == 4) { select4(); } // 종료 및 영수증 출력 else if(choice == 5) { getReceipt(); break; } } } } public class Day05209메서드영수증출력 { public static void main(String[] args) { BurgerShop moms = new BurgerShop(); moms.name = "맘츠터치 햄버거"; moms.run(); } } package Day052; import java.util.Scanner; //# ATM class Bank{ Scanner scan = new Scanner(System.in); String name = ""; String[] arAcc = {"1111", "2222", "3333", "", ""}; String[] arPw = {"1234", "2345", "3456", "", ""}; int[] arMoney = {87000, 34000, 17500, 0, 0}; int count = 3; // 로그아웃(-1) int loginCheck = 2; // 3333 로그인 중 void showMember() { for(int i=0; i<count; i++) { System.out.println(arAcc[i] + " : " + arPw[i] + " : " + arMoney[i] + "원"); } } void showMenu() { System.out.print("# 로그인 상태 : "); if(loginCheck == -1){ System.out.println("로그아웃"); }else { System.out.println(arAcc[loginCheck] + "님, 로그인 중..."); } System.out.println("=== " + name + " ==="); System.out.println("1.회원가입"); System.out.println("2.회원탈퇴"); System.out.println("3.로그인"); System.out.println("4.로그아웃"); System.out.println("5.입금하기"); System.out.println("6.이체하기"); } int checkAcc(String myAcc) { int check = 1; for(int i=0; i<count; i++) { if(arAcc[i].equals(myAcc)) { check = -1; } } return check; } int checkPw(int accCheck) { System.out.print("비밀번호를 입력하세요 : "); String myPw = scan.next(); int pwCheck = -1; if(arPw[accCheck].equals(myPw)) { pwCheck = 1; } return pwCheck; } int checkAcc() { System.out.print("탈퇴할 계좌번호를 입력하세요 : "); String myAcc = scan.next(); int accCheck = -1; for(int i=0; i<count; i++) { if(arAcc[i].equals(myAcc)) { accCheck = i; } } return accCheck; } void delMember() { // 회원탈퇴 제약조건 if(count == 0) { System.out.println("탈퇴할 회원정보가 없습니다."); return; } // 계좌번호 유효성 검사 int accCheck = checkAcc(); if(accCheck != -1) { // 비밀번호 유효성 검사 int pwCheck = checkPw(accCheck); if(pwCheck == -1) { System.out.println("비밀번호를 잘 못 입력했습니다."); }else { for(int i=accCheck; i<count - 1; i++) { arAcc[i] = arAcc[i + 1]; arPw[i] = arPw[i + 1]; arMoney[i] = arMoney[i + 1]; } count -= 1; System.out.println("탈퇴되었습니다."); } }else { System.out.println("없는 계좌번호 입니다."); } } void joinMember() { // 회원가입 회원 수는 5명까지만 허용 if(count == 5) { System.out.println("더이상 가입할 수 없습니다."); return; } // 계좌번호 중복검사 System.out.print("가입할 계좌번호를 입력하세요 : "); String myAcc = scan.next(); int check = checkAcc(myAcc); if(check == -1) { System.out.println("계좌번호가 중복됩니다."); }else { System.out.print("비밀번호를 입력하세요 : "); String myPw = scan.next(); arAcc[count] = myAcc; arPw[count] = myPw; arMoney[count] = 1000; count += 1; System.out.println("회원가입을 축하합니다."); } } void login() { if(loginCheck != -1) { System.out.println("로그아웃 후 진행해주세요."); return; } System.out.print("계좌번호를 입력하세요 : "); String myId = scan.next(); System.out.print("패스워드를 입력하세요 : "); String myPw = scan.next(); // 계좌번호와 패스워드 유효성 검사 for(int i=0; i<count; i++) { if(myId.equals(arAcc[i]) && myPw.equals(arPw[i])) { loginCheck = i; } } if(loginCheck == -1) { System.out.println("계좌번호와 패스워드를 확인해주세요."); }else { System.out.println("로그인 성공!"); } } void logOut() { if(loginCheck == -1) { System.out.println("로그인 후 진행해주세요."); }else { loginCheck = -1; System.out.println("로그아웃 되었습니다."); } } void income() { System.out.print("입금할 금액을 입력하세요 : "); int money = scan.nextInt(); arMoney[loginCheck] += money; System.out.println("입금을 완료하였습니다."); } void trans() { System.out.print("이체할 계좌번호를 입력하세요 : "); String transAcc = scan.next(); // 이체 계좌번호 유효성 검사 int check = -1; for (int i = 0; i < count; i++) { if (arAcc[i].equals(transAcc)) { check = i; } } if (check == -1) { System.out.println("계좌번호를 확인해주세요."); } else { System.out.print("이체할 금액을 입력하세요 : "); int transMoney = scan.nextInt(); // 이체 금액 유효성 검사 if (arMoney[loginCheck] >= transMoney) { arMoney[loginCheck] -= transMoney; arMoney[check] += transMoney; System.out.println("이체를 완료하였습니다."); } else { System.out.println("계좌잔액이 부족합니다."); } } } void run() { while(true) { // 전체 회원정보 출력 showMember(); // 메뉴 출력 showMenu(); // 메뉴 선택 System.out.print("메뉴를 선택해주세요 : "); int choice = scan.nextInt(); // 회원가입 if(choice == 1) { joinMember(); } // 회원탈퇴 else if(choice == 2) { delMember(); } // 로그인 else if(choice == 3) { login(); } // 로그아웃 else if(choice == 4) { logOut(); } // 입금 else if(choice == 5) { income(); } // 이체 else if(choice == 6) { trans(); } } } } public class Day05210메서드ATM { public static void main(String[] args) { Bank woori = new Bank(); woori.name = "우리은행"; woori.run(); } } package Day052; /* * # 메서드 오버로딩(overloading)이란? * 메서드를 같은 이름으로 만들어도 * 전달되는 값이 다르면 서로 다른 메서드로 인식하겠다는 의미 */ class MethodOverloading{ int add(int x, int y) { return x + y; } int add(int x, int y, int z) { return x + y + z; } int add(int[] arr) { int total = 0; for(int i=0; i<arr.length; i++) { total += arr[i]; } return total; } } public class Day05211메서드오버로딩 { public static void main(String[] args) { MethodOverloading mol = new MethodOverloading(); int[] arr = {1, 2, 3, 4, 5}; int r1 = mol.add(10, 3); int r2 = mol.add(10, 3, 1); int r3 = mol.add(arr); System.out.println("r1 = " + r1); System.out.println("r2 = " + r2); System.out.println("r3 = " + r3); } } package Day052; class Test { int x; void check() { // check() 메서드 안의 x변수와 메서드 밖의 x는 서로 다른 x이다. int x = 10; // 메서드 안의 x변수와 메서드 밖의 x변수를 구분짓는 방법은 // 메서드 밖의 x변수에 this를 붙여주는 것이다. // 본래 메서드 안에서 메서드 밖의 변수들은 this.을 붙여주는 것이 원칙이지만, 생략이 가능하다. // 하지만 명확성을 위해 붙여주는 것이 좋다. this.x = 100; System.out.println("지역변수 x = " + x); System.out.println("클래스 영역의 x변수 = " + this.x); } } public class Day05212this { public static void main(String[] args) { } }
'과거⚰️' 카테고리의 다른 글
클래스 배열 3, 4 || 학생컨트롤러 || 고객관리 || 문자열 1, 2, 답없음 (0) 2020.12.13 클래스배열 중복숫자금지 || 클래스배열 이론1 || 클래스배열 이론2 || 클래스배열 자리예매 || 클래스배열 틱택토 || 클래스배열 로또한세트 || 클래스 경마게임 || 클래스배열 회원가입 || 클래.. (0) 2020.12.12 메서드 이론 3, 4 || 실습 6, 7, 8, 9 (0) 2020.12.10 메서드 OMR || 영화관 좌석 || 기억력 || 숫자이동 || 틱택토 || 10 to 50 간단 || 사다리 (0) 2020.12.09 메서드 문제 1, 2, 3 (0) 2020.12.08