[Java] Solution to puzzles

Joy🌱·2022λ…„ 12μ›” 28일
0

🧩 Coding Challenges

λͺ©λ‘ 보기
6/20
post-thumbnail

🧩 클래슀 λ‹€μ΄μ–΄κ·Έλž¨μ— 따라 λ©”μ†Œλ“œ μž‘μ„±ν•˜κΈ°

πŸ’β€ λ°˜ν™˜ν˜•κ³Ό λ§€κ°œλ³€μˆ˜ μœ λ¬΄μ— 따라 λ©”μ†Œλ“œλ₯Ό μž‘μ„±ν•˜κ³ , 이λ₯Ό ν˜ΈμΆœν•˜κ±°λ‚˜ 좜λ ₯ν•˜μ„Έμš”.


🚩 Example Output

null null null 0 0.0
μžλ°”μ˜ 정석 λ„μš°μΆœνŒ 남ꢁ성 0 0.0
μž‘μ§€λ§Œ μœ„λŒ€ν•œ 일듀 νš¨μ—°μΆœνŒ μ‘°λ”” ν”Όμ½” 15000 0.5

β—Ό BookDTO Class

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(μƒμ„±μžλ§Œ ν˜ΈμΆœν•˜λŠ”)μ—μ„œ μ‚¬μš©λ˜μ§€λŠ” μ•ŠμŒ.
  ν•˜μ§€λ§Œ λ‚˜μ€‘μ— μƒˆ 책이 λ“€μ–΄μ™€μ„œ λ“±λ‘ν•˜κ±°λ‚˜, 가격이 λ‹¬λΌμ§€λŠ” λ“± 값을 λ³€κ²½ν•  λ•Œ κΌ­ ν•„μš”ν•œ λ©”μ†Œλ“œ

β—Ό Application Class

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
성별 : μ—¬

β—Ό StudentDTO Class

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);
	}
}

β—Ό Application Class

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둜 μˆ˜μ •ν–ˆλ‹€. μ •ν™•ν•œ μ΄μœ λŠ” νŒŒμ•…ν•˜μ§€ λͺ» ν–ˆμœΌλ‚˜, μž…λ ₯을 ν•˜κΈ°λ„ 전에 였λ₯˜κ°€ λ°œμƒν–ˆμœΌλ―€λ‘œ 식 μžμ²΄κ°€ 
  잘λͺ»λœ 것이라 μƒκ°ν•œλ‹€.

profile
Tiny little habits make me

0개의 λŒ“κΈ€