Book
package com.mywork.ex;
public class Book {
String title;
String writer;
int price;
int salesVolume;
boolean isBestSeller;
Book(String title, int price){
this(title, "작자미상", price);
}
Book(String title, String writer, int price){
this.title = title;
this.writer = writer;
this.price = price;
}
void setSalesVolume(int salesVolume) {
this.salesVolume = salesVolume;
if (salesVolume >= 1000) {
isBestSeller = true;
} else {
isBestSeller = false;
}
}
void output() {
System.out.println("책 제목 : " + title);
System.out.println("책 저자 : " + writer);
System.out.println("책 가격 : " + price);
System.out.println("책 판매량 : " + salesVolume);
System.out.println("베스트셀러 유무 : " + (isBestSeller ? "베스트셀러" : "일반서적"));
}
}
BookMain
package com.mywork.ex;
public class BookMain {
public static void main(String[] args) {
Book book1 = new Book("콩쥐팥쥐", 10000);
Book book2 = new Book("자바의 정석", "남궁성", 35000);
book1.setSalesVolume(500);
book2.setSalesVolume(1500);
book1.output();
book2.output();
}
}
Local
package com.mywork.ex;
public class Local {
String name;
int age;
String sn;
boolean isKorean;
Local(String name, int age) {
this(name, age, null);
}
Local(String name, int age, String sn) {
this.name = name;
this.age = age;
this.sn = sn;
if(sn != null) {
this.isKorean = sn.charAt(7) <= '4' ? true : false;
}
}
void output() {
System.out.println("이름 : " + name);
System.out.println("나이 : " + age);
System.out.println("주민등록번호 : " + (sn == null ? "없음" : sn));
System.out.println(isKorean ? "한국인" : "외국인");
}
}
LocalMain
package com.mywork.ex;
public class LocalMain {
public static void main(String[] args) {
Local person1 = new Local("홍길동", 20, "901215-1234567");
Local person2 = new Local("응우엔티엔", 21, "911215-6789123");
Local person3 = new Local("james", 22);
person1.output(); System.out.println("-----");
person2.output(); System.out.println("-----");
person3.output();
}
}
Rect
package com.mywork.ex;
public class Rect {
int width;
int height;
Rect(int width, int height) {
this.width = width;
this.height = height;
}
void setWidth(int width) {
this.width = width;
}
void setHeight(int height) {
this.height = height;
}
void output() {
System.out.println("너비 : " + width);
System.out.println("높이 : " + height);
}
}
RectMain
package com.mywork.ex;
public class RectMain {
public static void main(String[] args) {
Rect nemo = new Rect(1, 2);
nemo.output();
nemo.setWidth(10);
nemo.setHeight(20);
nemo.output();
}
}