쌍용교육(JAVA)/JAVA

쌍용교육 -JAVA 수업 9일차 응용(phone),(Bank)

구 승 2024. 4. 11. 17:23

phone

package kr.s16.object.extention;

//부모 클래스
public class Phone {
	protected String number;
	protected String model;
	protected String color;
	//우클릭 source -> generate getters and setters
	//select getters만 체크
	public String getNumber() {
		return number;
	}
	public String getModel() {
		return model;
	}
	public String getColor() {
		return color;
	}
	
	
	

	
}

package kr.s16.object.extention;

public class FeaturePhone extends Phone{
	private int pixel; //화소수
	
	public FeaturePhone(String number, String model, String color
			, int pixel) {
		this.number = number;
		this.model = model;
		this.color = color;
		this.pixel = pixel;
	}
	public int getPixel() {
		return pixel;
	}
}

package kr.s16.object.extention;

public class SmartPhone extends Phone{
	private String os;
	
	public SmartPhone(String number, String model, 
			String color, String os) {
		this.number = number;
		this.model = model;
		this.color = color;
		this.os = os;
	}

	public String getOs() {
		return os;
	}
	
}

package kr.s16.object.extention;

public class PhoneMain {
	public static void main(String[] args) {
		//객체 선언 및 생성
		SmartPhone sp = new SmartPhone("010-1234","A1001","white","안드로이드");
		FeaturePhone fp = new FeaturePhone("010-5678","B1001","black",500);
		
		System.out.println("-------------------------------------------");
		System.out.println("번호\\t모델\\t색상\\t특징(OS/화소수");
		System.out.println("-------------------------------------------");
		System.out.print(sp.getNumber()+ "\\t");
		System.out.print(sp.getModel()+ "\\t");
		System.out.print(sp.getColor()+"\\t");
		System.out.print(sp.getOs()+"\\n");
		
		System.out.print(fp.getNumber()+"\\t");
		System.out.print(fp.getModel()+"\\t");
		System.out.print(fp.getColor()+"\\t");
		System.out.print(fp.getPixel()+"\\n");
	}
}
	

-------------------------------------------
번호	모델	색상	특징(OS/화소수
-------------------------------------------
010-1234	A1001	white	안드로이드
010-5678	B1001	black	500

 

bank

package kr.s17.object.overriding;

public class BankAccount {
	protected String number;
	protected String password;
	protected String  name;
	protected long balance;
	
	//생성자
	public BankAccount(String number, String password, String name, long balance) {
		
		//멤버변수=지역변수
		this.number = number;
		this.password = password;
		this.name = name;
		this.balance = balance;
		System.out.println(number+"계좌가 개설되었습니다.");
	}
	//예금하기
	public void deposit(long amount) {
		if(amount <=0 ) {
			System.out.println("0이하의 금액은 입금할 수 없습니다.");
			return;
		}
		balance += amount;
		System.out.printf("%,d원 이 입금되었습니다.%n",amount);
	}
	//출금하기
	public void withdraw(long amount) {
		if(amount <0 ) {
			System.out.println("0이하의 금액은 출금할 수 없습니다.");
			return;
		}
		if (balance < amount) {
			System.out.println("잔액이 부족합니다.");
			return;
		}
		balance -= amount;
		System.out.printf("%d,원 이 출금되었습니다.%n",amount);
	}
	//계좌정보 출력하기
	public void printAccount() {
		System.out.println("(일반)계좌번호 : "+number);
		System.out.println("비밀번호 : "+password);
		System.out.println("예금주"+name);
		System.out.printf("계좌잔액 :%,d원 %n",balance);
		System.out.println("----------------------------------");
	}
}

123-456계좌가 개설되었습니다.
(일반)계좌번호 : 123-456
비밀번호 : 1234
예금주김연아
계좌잔액 :10,000원 
----------------------------------
입금 : 100000
100,000원 이 입금되었습니다.
(일반)계좌번호 : 123-456
비밀번호 : 1234
예금주김연아
계좌잔액 :110,000원 
----------------------------------
출금 : 110000
110000,원 이 출금되었습니다.
(일반)계좌번호 : 123-456
비밀번호 : 1234
예금주김연아
계좌잔액 :0원 
----------------------------------

package kr.s17.object.overriding;

public class MinusAccount extends BankAccount{
/*
 * [실습]
 * 1.한도를 의미하는 minusLimit 멤버변수 정의
 * 2.BankAccount를 상속 받는다
 * 3.생성자에서 number,password,name,balance,minusLimit
 * 를 전달 받아서 멤버변수에 저장.
 * 4.withdraw,printAccount 메서드 재정의
 */
	protected long minusLimit;
	//생성자
	public MinusAccount(String number, String password, String name, 
			long balance,long minusLimit) {
		super(number, password, name, balance);
		this.minusLimit = minusLimit;
	}
		//출금하기 재정의
		//계좌 정보 출력 재정의
		@Override
		public void withdraw(long amount) {
			
			if (amount <=0) {
				System.out.println("0이하의 금액은 출금할 수 없습니다");
				return;
			}
			if (balance + minusLimit < amount) {
				System.out.println("한도초과로 출금되지 않습니다");
				return;
			}
			balance -= amount;
			System.out.printf("%d,원 이 출금되었습니다.%n",amount);
		}
		//계좌정보 출력하기
		public void printAccount() {
			System.out.println("(마이너스)계좌번호 : "+number);
			System.out.println("비밀번호 : "+password);
			System.out.println("예금주"+name);
			System.out.printf("계좌잔액 :%,d원 %n",balance);
			System.out.printf("마이너스 한도 :%,d원 %n",minusLimit);
			System.out.println("----------------------------------");
		}
		
	
	
}

package kr.s17.object.overriding;

import java.util.Scanner;
public class BankMain {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		//일반계좌 생성
		BankAccount ba =new BankAccount("123-456","1234","김연아",10000L);  
		
		//계좌정보 출력
		ba.printAccount();
		//입금하기
		System.out.print("입금 : ");
		long money = input.nextLong();
		ba.deposit(money);
		ba.printAccount();
		//출금하기
		System.out.print("출금 : ");
		money =input.nextLong();
		ba.withdraw(money);
		ba.printAccount();
		input.close();
	}
}

123-456계좌가 개설되었습니다.
(일반)계좌번호 : 123-456
비밀번호 : 1234
예금주김연아
계좌잔액 :10,000원 
----------------------------------
입금 : 10000
10,000원 이 입금되었습니다.
(일반)계좌번호 : 123-456
비밀번호 : 1234
예금주김연아
계좌잔액 :20,000원 
----------------------------------
출금 : 20000
20000,원 이 출금되었습니다.
(일반)계좌번호 : 123-456
비밀번호 : 1234
예금주김연아
계좌잔액 :0원 
----------------------------------

package kr.s17.object.overriding;

import java.util.Scanner;
public class BankMain2 {
	public static void main(String[] args) {  
		Scanner input = new Scanner(System.in);     

		MinusAccount ma = new MinusAccount("123-456","1234","이상화",10000L,100000L);

		ma.printAccount();
		//입금하기
		System.out.print("입금 : ");
		long money = input.nextLong();
		ma.deposit(money);
		ma.printAccount();
		//출금하기
		System.out.print("출금 : ");
		money =input.nextLong();
		ma.withdraw(money);
		ma.printAccount();
		
		input.close();
	}
}

123-456계좌가 개설되었습니다.
(마이너스)계좌번호 : 123-456
비밀번호 : 1234
예금주이상화
계좌잔액 :10,000원 
마이너스 한도 :100,000원 
----------------------------------
입금 : 1000000
1,000,000원 이 입금되었습니다.
(마이너스)계좌번호 : 123-456
비밀번호 : 1234
예금주이상화
계좌잔액 :1,010,000원 
마이너스 한도 :100,000원 
----------------------------------
출금 : 10000000
한도초과로 출금되지 않습니다
(마이너스)계좌번호 : 123-456
비밀번호 : 1234
예금주이상화
계좌잔액 :1,010,000원 
마이너스 한도 :100,000원 
----------------------------------