신용권님의 ''이것이 자바다'' 공부 기록
책을 보면서 내용을 정리했습니다. 이것이 자바다 커뮤니티 : https://cafe.naver.com/thisisjava
public class Person {
final String nation = "Korea";
final String ssn;
String name;
public Person(String ssn, String name) {
this.ssn = ssn;
this.name = name;
}
}
접근 제한 | 적용 대상 | 접근할 수 없는 클래스 |
---|---|---|
public | 클래스, 필드, 생성자, 메소드 | 없음 |
protected | 필드, 생성자, 메소드 | 자식 클래스가 아닌 다른 패키지에 소속된 클래스 |
default | 클래스, 필드, 생성자, 메소드 | 다른 패키지에 소속된 클래스 |
private | 필드, 생성자, 메소드 | 모든 외부 클래스 |
public class Person {
final String nation = "Korea";
final String ssn;
String name;
public Person(String ssn, String name) {
this.ssn = ssn;
this.name = name;
}
}
접근 제한 | 적용 대상 | 접근할 수 없는 클래스 |
---|---|---|
public | 클래스, 필드, 생성자, 메소드 | 없음 |
protected | 필드, 생성자, 메소드 | 자식 클래스가 아닌 다른 패키지에 소속된 클래스 |
default | 클래스, 필드, 생성자, 메소드 | 다른 패키지에 소속된 클래스 |
private | 필드, 생성자, 메소드 | 모든 외부 클래스 |
package com.kosta.Quiz06;
//13.
public class Member {
String name;
String id;
String password;
int age;
//14.
Member(String name, String id) {
this.name = name;
this.id = id;
}
}
package com.kosta.Quiz06;
//13.
public class MemberService {
boolean lgoin(String id, String password) {
boolean result;
if (id.equals("hong")) result = true;
else result = false;
if (password.equals("12345")) result = true;
else result = false;
return result;
}
void logout(String id) {
System.out.println(id + "님이 로그아웃 되었습니다.");
}
}
package com.kosta.Quiz06;
//15.
public class MemberServiceExample {
public static void main(String[] args) {
MemberService memberService = new MemberService();
boolean result = memberService.lgoin("hong", "12345");
if(result) {
System.out.println("로그인 되었습니다.");
memberService.logout("hong");
}else {
System.out.println("id 또는 password가 올바르지 않습니다.");
}
}
}
package com.kosta.Quiz06;
public class Printer {
// 16.
// public void println(int i) {
// System.out.println(i);
// }
// public void println(boolean b) {
// System.out.println(b);
// }
// public void println(double d) {
// System.out.println(d);
// }
// public void println(String s) {
// System.out.println(s);
// }
// 17. (static으로 정적메소드로 만들어서 객체 생성없이 접근 가능하게 하기)
static public void println(int i) {
System.out.println(i);
}
static public void println(boolean b) {
System.out.println(b);
}
static public void println(double d) {
System.out.println(d);
}
static public void println(String s) {
System.out.println(s);
}
}
package com.kosta.Quiz06;
public class PrinterExample {
public static void main(String[] args) {
// 16.
// Printer printer = new Printer();
// printer.println(10);
// printer.println(true);
// printer.println(5.7);
// printer.println("홍길동");
// 17. (객체 선언하지 않고 메소드 호출)
Printer.println(10);
Printer.println(true);
Printer.println(5.7);
Printer.println("홍길동");
}
}
package com.kosta.Quiz06;
//18.
public class ShopService {
static ShopService shopService;
private ShopService() { }
static ShopService getInstance() {
if(shopService == null) shopService = new ShopService();
return shopService;
}
}
package com.kosta.Quiz06;
//18.
public class ShopServiceExample {
public static void main(String[] args) {
ShopService obj1 = ShopService.getInstance();
ShopService obj2 = ShopService.getInstance();
if(obj1 == obj2) {
System.out.println("같은 ShopService 객체입니다.");
}else {
System.out.println("다른 ShopService 객체입니다.");
}
}
}
package com.kosta.Quiz06;
//19 & 20.
public class Account {
static final int MIN_BALANCE = 0;
static final int MAX_BALANCE = 1000000;
private String ano;
private String owner;
private int balance;
public Account() {
}
public Account(String ano, String owner, int balance) {
this.ano = ano;
this.owner = owner;
this.balance = balance;
}
public String getAno() {
return ano;
}
public void setAno(String ano) {
this.ano = ano;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
if (balance > MIN_BALANCE && balance < MAX_BALANCE)
this.balance = balance;
}
@Override
public String toString() {
return ano + " " + owner + " " + balance;
}
}
package com.kosta.Quiz06;
//19.
public class AccountExample {
public static void main(String[] args) {
Account account = new Account();
account.setBalance(10000);
System.out.println("현재 잔고: " + account.getBalance());
account.setBalance(-100);
System.out.println("현재 잔고: " + account.getBalance());
account.setBalance(2000000);
System.out.println("현재 잔고: " + account.getBalance());
account.setBalance(300000);
System.out.println("현재 잔고: " + account.getBalance());
}
}
package com.kosta.Quiz06;
import java.util.Scanner;
public class BankApplication {
static Scanner sc = new Scanner(System.in);
static Account[] accountArray = new Account[100];
public static void main(String[] args) {
boolean run = true;
while (run) {
System.out.println("------------------------------------------");
System.out.println("1.계좌생성 | 2.계좌목록 | 3.예금 | 4.출금 | 5.종료");
System.out.println("------------------------------------------");
System.out.print("선택> ");
int selectNo = sc.nextInt();
if (selectNo == 1) {
createAccount();
} else if (selectNo == 2) {
accountList();
} else if (selectNo == 3) {
deposit();
} else if (selectNo == 4) {
withraw();
} else if (selectNo == 5) {
run = false;
}
}
System.out.println("프로그램 종료");
}
//계좌생성하기
private static void createAccount() {
String ano;
String owner;
int balance;
System.out.println("------");
System.out.println("계좌생성");
System.out.println("------");
System.out.print("계좌번호: ");
ano = sc.next();
System.out.print("계좌주: ");
owner = sc.next();
System.out.print("초기입금: ");
balance = sc.nextInt();
for (int i = 0; i < accountArray.length; i++) {
if (accountArray[i] == null) {
accountArray[i] = new Account(ano, owner, balance);
System.out.println("결과: 계좌가 생성되었습니다.");
break;
}
}
}
//계좌목록보기
private static void accountList() {
System.out.println("------");
System.out.println("계좌목록");
System.out.println("------");
for (Account account : accountArray) {
if (account != null) {
System.out.println(account.toString());
}
}
}
//예금하기
private static void deposit() {
System.out.println("------");
System.out.println("예금");
System.out.println("------");
System.out.print("계좌번호: ");
String ano = sc.next();
System.out.print("예금액: ");
int depositAmount = sc.nextInt();
Account account = findAccount(ano);
if(account == null) {
System.out.println("계좌가 없습니다.");
}
if (account != null) {
account.setBalance(account.getBalance() + depositAmount);
}
System.out.println("예금이 성공되었습니다.");
}
//출금하기
private static void withraw() {
System.out.println("------");
System.out.println("출금");
System.out.println("------");
System.out.print("계좌번호: ");
String ano = sc.next();
System.out.print("출금액: ");
int depositAmount = sc.nextInt();
Account account = findAccount(ano);
if(account == null) {
System.out.println("계좌가 없습니다.");
}
if (account != null) {
account.setBalance(account.getBalance() - depositAmount);
}
System.out.println("출금이 성공되었습니다.");
}
//Account 배열에서 ano와 동일한 Account 객체 찾기
private static Account findAccount(String ano) {
for (Account account : accountArray) {
if (account.getAno().equals(ano)) {
return account;
}
}
return null;
}
}