πβ λ°ννκ³Ό 맀κ°λ³μ μ 무μ λ°λΌ λ©μλλ₯Ό μμ±νκ³ , μ΄λ₯Ό νΈμΆνκ±°λ μΆλ ₯νμΈμ.
π© Example Output
null null null 0 0.0
μλ°μ μ μ λμ°μΆν λ¨κΆμ± 0 0.0
μμ§λ§ μλν μΌλ€ ν¨μ°μΆν μ‘°λ νΌμ½ 15000 0.5
public class BookDTO {
/* νλ μμ± */
private String title;
private String publisher;
private String author;
private int price;
private double discountRate;
/* μμ±μ */
// κΈ°λ³Έ μμ±μ
public BookDTO() {
}
// νλ 3κ°μ§ μ΄κΈ°νν μμ±μ
public BookDTO(String title, String publisher, String author) {
this.title = title;
this.publisher = publisher;
this.author = author;
}
// λͺ¨λ νλλ₯Ό μ΄κΈ°νν μμ±μ
public BookDTO(String title, String publisher, String author, int price,
double discountRate) {
this(title, publisher, author);
this.price = price;
this.discountRate = discountRate;
}
/* λ©μλ μ μΈλΆ */
// setter
public void setTitle(String title) {
this.title = title;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(int price) {
this.price = price;
}
public void setDiscountRate(double discountRate) {
this.discountRate = discountRate;
}
// getter
public String getTitle() {
return title;
}
public String getPublisher() {
return publisher;
}
public String getAuthor() {
return author;
}
public int getPrice() {
return price;
}
public double getDiscountRate() {
return discountRate;
}
// νλκ° μΆλ ₯μ© λ©μλ
public void printInformation() {
System.out.println(this.title + " " + this.publisher + " " + this.author +
" " + this.price + " " + this.discountRate);
}
}
π Ref.
* setter
public void setνλλͺ
(맀κ°λ³μ) {
νλ = 맀κ°λ³μ;
}
* getter
public λ°νν getνλλͺ
() {
return νλλͺ
;
}
* setterμ getterλ νμ¬ Application(μμ±μλ§ νΈμΆνλ)μμ μ¬μ©λμ§λ μμ.
νμ§λ§ λμ€μ μ μ±
μ΄ λ€μ΄μμ λ±λ‘νκ±°λ, κ°κ²©μ΄ λ¬λΌμ§λ λ± κ°μ λ³κ²½ν λ κΌ νμν λ©μλ
public class Application {
public static void main(String[] args) {
// κΈ°λ³Έ μμ±μ νΈμΆ
BookDTO book1 = new BookDTO();
book1.printInformation();
// νλ 3κ°μ§ μ΄κΈ°νν μμ±μ νΈμΆ
BookDTO book2 = new BookDTO("μλ°μ μ μ", "λμ°μΆν", "λ¨κΆμ±");
book2.printInformation();
// λͺ¨λ νλλ₯Ό μ΄κΈ°νν μμ±μ νΈμΆ
BookDTO book3 = new BookDTO("μμ§λ§ μλν μΌλ€", "ν¨μ°μΆν", "μ‘°λ νΌμ½", 15000, 0.5);
book3.printInformation();
}
}
πβ λ°ννκ³Ό 맀κ°λ³μ μ 무μ λ°λΌ λ©μλλ₯Ό μμ±νκ³ , μ΄λ₯Ό νΈμΆνκ±°λ μΆλ ₯νμΈμ.
π© Example Input & Output
νλ
μ μ
λ ₯ν΄μ£ΌμΈμ :
λ°μ μ
λ ₯ν΄μ£ΌμΈμ :
μ΄λ¦μ μ
λ ₯ν΄μ£ΌμΈμ :
ν€λ₯Ό μ
λ ₯ν΄μ£ΌμΈμ :
μ±λ³μ μ
λ ₯ν΄μ£ΌμΈμ :
νλ
: 6
μ΄λ¦ : 1
λ° : μ‘°ν¨μ°
ν€ : 160.0
μ±λ³ : μ¬
package com.greedy.level02.normal.student.model.dto;
public class StudentDTO {
/* νλ μμ± */
private int grade;
private int classroom;
private String name;
private double height;
private char gender;
/* μμ±μ */
// κΈ°λ³Έ μμ±μ
public StudentDTO() {
}
// λͺ¨λ νλλ₯Ό μ΄κΈ°ννλ μμ±μ
public StudentDTO(int grade, int classroom, String name, double height, char gender) {
this.grade = grade;
this.classroom = classroom;
this.name = name;
this.height = height;
this.gender = gender;
}
/* λ©μλ μ μΈλΆ */
// setter
public void setGrade(int grade) {
this.grade = grade;
}
public void setClassroom(int classroom) {
this.classroom = classroom;
}
public void setName(String name) {
this.name = name;
}
public void setHeight(double height) {
this.height = height;
}
public void setGender(char gender) {
this.gender = gender;
}
// getter
public int getGrade() {
return grade;
}
public int getClassroom() {
return classroom;
}
public String getName() {
return name;
}
public double getHeight() {
return height;
}
public char getGender() {
return gender;
}
// νλκ° μΆλ ₯μ© λ©μλ
public void printInformation() {
System.out.println("νλ
: " + this.grade);
System.out.println("λ° : " + this.classroom);
System.out.println("μ΄λ¦ : " + this.name);
System.out.println("ν€ : " + this.height);
System.out.println("μ±λ³ : " + this.gender);
}
}
public class Application {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("νλ
μ μ
λ ₯ν΄μ£ΌμΈμ : ");
int grade = sc.nextInt();
System.out.print("λ°μ μ
λ ₯ν΄μ£ΌμΈμ : ");
int classroom = sc.nextInt();
sc.nextLine();
System.out.print("μ΄λ¦μ μ
λ ₯ν΄μ£ΌμΈμ : ");
String name = sc.nextLine();
System.out.print("ν€λ₯Ό μ
λ ₯ν΄μ£ΌμΈμ : ");
double height = sc.nextDouble();
System.out.print("μ±λ³μ μ
λ ₯ν΄μ£ΌμΈμ : ");
char gender = sc.next().charAt(0);
StudentDTO student = new StudentDTO(grade, classroom, name, height, gender);
student.printInformation();
}
}
π¬ Overall Comment
* Application ν΄λμ€μμ sc.nextLine()μ μμμλ€κ° λ°μ μ
λ ₯ν λ€μ μν°κ° λμ΄κ°λ²λ¦¬λ νμμ΄
λ°μνλ€. μ΄μ μ μ μλκ»μ μ£ΌμνλΌκ³ μλ €μ£Όμ λΆλΆμ΄μκΈ° λλ¬Έμ μ¬μ΄μ sc.nextLine()λ₯Ό μΆκ°νμ¬
μ μμ μΌλ‘ μλλλλ‘ νλ€. μ¬κΈ°μ μ€ν°λ νμλΆμ΄ μλ €μ£Όμ νλ μμλλ°, nextLine λμ nextλ₯Ό
μ¬μ©νλ κ²λ ν΄κ²°λ°©λ²μ΄μλ€.
* λν StudentDTO ν΄λμ€μ μΆλ ₯μ© λ©μλκ° voidμ¬μ λ°νκ°μ΄ μλ€λ©΄, μ§μ κ·Έ λ©μλμ μΆλ ₯λ¬Έμ λ£κ³
Application ν΄λμ€μμ μ§μ νΈμΆμ νλ κ²μ΄ λ°©λ²μ΄λ€.
* κ·Έλ¦¬κ³ char gender = sc.next().charAt(0); μμμ μ²μμ nextLineμ μ¬μ©νλ€κ° μ€λ₯κ° λ°μνμ¬
nextλ‘ μμ νλ€. μ νν μ΄μ λ νμ
νμ§ λͺ» νμΌλ, μ
λ ₯μ νκΈ°λ μ μ μ€λ₯κ° λ°μνμΌλ―λ‘ μ μ체κ°
μλͺ»λ κ²μ΄λΌ μκ°νλ€.