public class Account {
int no; // 계좌번호
String owner; // 예금주
int balance; // 잔액
int pwd; // 비밀번호
Account(int newNo, String newOwner, int newPwd) {
no = newNo;
owner = newOwner;
pwd = newPwd;
balance = 0;
}
Account(int newNo, String newOwner, int newPwd, int newBalance) {
no = newNo;
owner = newOwner;
pwd = newPwd;
balance = newBalance;
}
// 출력기능
void displayInfo() {
System.out.println("계좌번호 : " + no);
System.out.println("예 금 주 : " + owner);
System.out.println("비밀번호 : " + pwd);
System.out.println("잔 액 : " + balance);
System.out.println();
}
// 입금 기능 : 입금액을 전달 받아서 잔액을 증가시키sms 기능
void deposit(int amount) {
if (amount > 0) {
balance += amount;
System.out.println("입금이 완료되었습니다.");
} else {
System.out.println("입금액이 올바르지 않습니다.");
}
System.out.println();
}
void withdraw(int amount) {
if (amount < balance) {
balance -= amount;
System.out.println("출금이 완료되었습니다.");
} else {
System.out.println("출금액이 올바르지 않습니다.");
}
System.out.println();
}
// 비밀번호 변경 기능 : 이전 비밀번호와 새 비밀번호를 전달받아서 비밀번호를 변경하는 기능
// 옛날 비밀번호와 새 비밀번호가 일치한다면 비밀번호 변경
// 일치하지 않으면 화면에 에러메시지 출력
void changePwd(int oldPwd, int newPwd) {
if (oldPwd == pwd && newPwd != pwd) {
pwd = newPwd;
System.out.println("비밀번호가 정상적으로 변경되었습니다.");
} else if (oldPwd != pwd) {
System.out.println("비밀번호가 일치하지 않습니다.");
} else if (newPwd == pwd) {
System.out.println("기존의 비밀번호와 동일한 비밀번호를 입력하셨습니다.");
}
System.out.println();
}
}
public class AccountApp {
public static void main(String[] args) {
// 객체 생성 후 Account(int, String, int) 생성자 메소드 실행
Account a1 = new Account(10, "홍길동", 1234);
// 객체 생성 후 Account(int, String, int, int) 생성자 메소드 실행
Account a2 = new Account(30, "이순신", 5678, 3000000);
//a1.displayInfo();
//a1.deposit(100000);
//a1.displayInfo();
a1.changePassword(1234, 1111);
a1.displayInfo();
//a2.displayInfo();
//a2.deposit(100000);
//a2.displayInfo();
a2.changePassword(1212, 7777);
a2.displayInfo();
}
}
import java.util.Scanner;
public class AccountApp2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Account[] accounts = new Account[2];
for (;;) {
System.out.println("1. 입력 | 2. 조회 | 3. 입금 | 4. 비번변경 | 0. 종료");
System.out.println();
System.out.println("메뉴 선택> ");
int selectNo = scanner.nextInt();
if (selectNo == 1) {
for (int i=0; i<2; i++) {
// 번호, 소유주, 비번, 잔액을 입력받아서
// Account 객체를 생성하고(매개변수 4개 짜리 생성자 활용)
// accounts 배열에 담기
System.out.println("번호입력> ");
int no = scanner.nextInt();
System.out.println("소유주입력> ");
String owner = scanner.next();
System.out.println("비번입력> ");
int password = scanner.nextInt();
System.out.println("잔액입력> ");
int balance = scanner.nextInt();
Account a = new Account(no, owner, password, balance);
accounts[i] = a;
}
} else if (selectNo == 2) {
for (Account acc : accounts) {
acc.displayInfo();
}
} else if (selectNo == 3) {
for (Account acc : accounts) {
System.out.println("입금액 입력> ");
int a = scanner.nextInt();
acc.deposit(a);
}
} else if (selectNo == 4) {
} else if (selectNo == 0) {
System.out.println("프로그램 종료");
break;
}
System.out.println();
}
}
}
public class Book{
int no;
String title;
String writer;
int price;
Book() {
}
Book(int no, String title, String writer, int price) {
this.no = no;
this.title = title;
this.writer = writer;
this.price = price;
}
void displayBookDetail() {
System.out.println("번호 : " + no);
System.out.println("제목 : " + title);
System.out.println("저자 : " + writer);
System.out.println("가격 : " + price);
}
}
public class BookApp {
public static void main(String[] args) {
Book b1 = new Book();
b1.displayBookDetail();
Book b2 = new Book(10, "이것이 자바다", "신용권", 30000);
b2.displayBookDetail();
Book b3 = new Book(30, "문제로 풀어보는 알고리즘", "황인욱", 28000);
b3.displayBookDetail();
}
}
public class Car {
String color;
// 생성자 정의
Car() {
System.out.println("기본 생성자가 실행됨");
color = "녹턴그레이";
}
Car(String customColor) {
System.out.println("컬러를 전달받는 생성자가 실행됨");
color = customColor;
}
// 필드값을 제공하는 메소드
String getColor(){
return color;
}
// 전달받은 값으로 필드값을 변경하는 메소드
void setColor(String c) {
color = c;
}
}
public class CarApp {
public static void main(String[] args) {
Car car1 = new Car();
System.out.println(car1.color); // 필드에 저장된 값 표현
System.out.println(car1.getColor()); // 조회 메소드를 실행해서 획득한 값 표현
Car car2 = new Car();
System.out.println(car2.color);
car2.color = "실버";
car2.setColor("실버");
System.out.println(car2.color);
Car car3 = new Car("화이트");
System.out.println(car3.color);
Car car4 = new Car("미드나이트 블랙");
System.out.println(car4.color);
}
}
// 사원 정보를 담는 객체
public class Employee {
// 사원 번호, 이름, 소속 부서명, 급여, 근무 연수, 전화번호
// no name dept salary workingYear tel
// 비공개 필드 정의
private int no;
private String name;
private String dept;
private int salary;
private int workingYear;
private String tel;
// 기본 생성자
public Employee() {
}
public Employee(int no, String name, String dept, int salary, int workingYear, String tel) {
this.no = no;
this.name = name;
this.dept = dept;
this.salary = salary;
this.workingYear = workingYear;
this.tel = tel;
}
// getter 메소드
public int getNo() {
return no;
}
public String getName() {
return name;
}
public String getDept() {
return dept;
}
public int getSalary() {
return salary;
}
public int getWorkingYear() {
return workingYear;
}
public String getTel() {
return tel;
}
// setter 메소드
public void setNo(int no) {
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public void setDept(String dept) {
this.dept = dept;
}
public void setSalary(int salary) {
this.salary = salary;
}
public void setWorkingYear(int workingYear) {
this.workingYear = workingYear;
}
public void setTel(String tel) {
this.tel = tel;
}
}
public class EmployeeApp {
public static void main(String[] args) {
// 객체 생성 <-- 기본 생성자 활용
Employee emp1 = new Employee();
// setter를 활용해서 필드 초기화
emp1.setNo(10);
emp1.setName("홍길동");
emp1.setDept("영업1팀");
emp1.setSalary(3000000);
emp1.setWorkingYear(4);
emp1.setTel("010-1111-2222");
// 매개 변수를 전달받는 생성자를 이용해서 필드 초기화
Employee emp2 = new Employee(20, "김유신", "인사팀", 2500000, 3, "010-3333-4444");
// Employee 객체 2개 담을 수 있는 배열 객체를 생성해서 employees에 담기
Employee[] employees = new Employee[2];
// employees가 참조하는 배열의 0번 째와 1번 째에 위에서 생성한 객체를 담기
employees[0] = emp1;
employees[1] = emp2;
// 배열에 저장된 사원들의 사원 번호와 이름, 급여를 화면에 표시하기
for (Employee emp : employees) {
int 사번 = emp.getNo();
String 이름 = emp.getName();
int 급여 = emp.getSalary();
System.out.println("사원 번호 : " + 사번);
System.out.println("이 름 : " + 이름);
System.out.println("급 여 : " + 급여);
}
}
}
public class EmployeeService {
// 새로운 사원 정보를 전달받아서 등록하는 기능
Employee[] database = new Employee[3];
public EmployeeService() {
database[0] = new Employee(10, "김유신", "영업1팀", 250, 2, "010-1234-5678");
database[1] = new Employee(20, "강감찬", "기술팀", 350, 2, "010-1111-1111");
database[2] = new Employee(30, "이순신", "인사팀", 300, 2, "010-1234-2222");
}
// 삭제할 사원 번호를 전달받아서 그 번호에 해당하는 사원 정보를 삭제하는 기능
// 모든 사원 정보를 출력하는 기능
public void printAllEmployees() {
for (Employee e : database) {
int no = e.getNo();
String name = e.getName();
String dept = e.getDept();
int salary = e.getSalary();
int workingYear = e.getWorkingYear();
String tel = e.getTel();
System.out.println("사원번호 : " + no);
System.out.println("사원이름 : " + name);
System.out.println("소속부서: " + dept);
System.out.println("실수령액 : " + salary);
System.out.println("근무연수 : " + workingYear);
System.out.println("전화번호 : " + tel);
}
}
// 조회할 사원 번호를 전달받아서 그 번호에 해당하는 사원 정보를 제공하는 기능
public Employee getEmployee(int empNo) {
Employee emp = null;
for (Employee e : database) {
if (empNo == e.getNo()) {
emp = e;
break;
}
}
return emp;
}
}
public class EmployeeServiceApp {
public static void main(String[] args) {
EmployeeService service = new EmployeeService();
// 모든 사원정보 출력해보기
service.printAllEmployees();
// 10번 사원 찾기
Employee emp10 = service.getEmployee(10);
Employee emp20 = service.getEmployee(20);
System.out.println(emp10.getNo() + ", " +emp10.getName());
System.out.println(emp20.getNo() + ", " + emp20.getName());
}
}
'자바 > oop2' 카테고리의 다른 글
Product(Service(App)), StaticService(App), StringUtils(App), Subject(App).java (0) | 2019.06.07 |
---|