실습문제 1
package edu.kh.oop.practice.model.vo;
public class Book {
private String title;
private int price;
private double discountRate;
private String author;
public Book() {}
public Book(String title, int price, double discountRate, String author) {
this.title = title;
this.price = price;
this.discountRate = discountRate;
this.author = author;
}
public String toString() {
String str = getTitle() + " / " + getPrice() + " / " +
getDiscountRate() + " / " + getAuthor();
return str;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return price;
}
public void setDiscountRate(double discountRate) {
this.discountRate = discountRate;
}
public double getDiscountRate() {
return discountRate;
}
public void setAuthor(String author) {
this.author = author;
}
public String getAuthor() {
return author;
}
}
package edu.kh.oop.practice.model.service;
import edu.kh.oop.practice.model.vo.Book;
public class BookService {
public void practice() {
Book book1 = new Book();
Book book2 = new Book("자바의 정석", 30000, 0.2, "남궁성");
System.out.println(book1.toString());
System.out.println(book2.toString());
System.out.println("============================");
System.out.println("수정된 결과 확인");
book1.setTitle("C언어");
book1.setPrice(30000);
book1.setDiscountRate(0.1);
book1.setAuthor("홍길동");
System.out.println(book1.toString());
System.out.println("============================");
int discountPrice1 = (int)(book1.getPrice() - (book1.getPrice() * book1.getDiscountRate()));
int discountPrice2 = (int)(book2.getPrice() - (book2.getPrice() * book2.getDiscountRate()));
System.out.printf("도서명 = %s\n", book1.getTitle());
System.out.printf("할인된 가격 = %d원\n", discountPrice1);
System.out.printf("도서명 = %s\n", book2.getTitle());
System.out.printf("할인된 가격 = %d원\n", discountPrice2);
}
}
package edu.kh.oop.practice.run;
import edu.kh.oop.practice.model.service.BookService;
public class PracticeRun {
public static void main(String[] args) {
BookService bookser = new BookService();
bookser.practice();
}
}
실습문제 2
package com.kh.practice.snack.model.vo;
public class Snack {
private String kind;
private String name;
private String flavor;
private int numOf;
private int price;
public Snack() {}
public Snack(String kind, String name, String flavor, int numOf, int price) {
this.kind = kind;
this.name = name;
this.flavor= flavor;
this.numOf = numOf;
this.price = price;
}
public String informaiton() {
return kind + "(" + name + " - " + flavor + ") " + numOf + "개 " + price + "원";
}
public void setKind(String kind) {
this.kind = kind;
}
public String getKind() {
return kind;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setFlavor(String flavor) {
this.flavor = flavor;
}
public String getFlavor() {
return flavor;
}
public void setNumOf(int numOf) {
this.numOf = numOf;
}
public int getNumOf() {
return numOf;
}
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return price;
}
}
package com.kh.practice.snack.controller;
import com.kh.practice.snack.model.vo.Snack;
public class SnackController {
Snack s = new Snack();
public SnackController() {}
public String saveData(String kind, String name, String flavor, int numOf, int price) {
s.setKind(kind);
s.setName(name);
s.setFlavor(flavor);
s.setNumOf(numOf);
s.setPrice(price);
return "저장 완료되었습니다.";
}
public String confirmData() {
return s.informaiton();
}
}
package com.kh.practice.snack.view;
import java.util.Scanner;
import com.kh.practice.snack.controller.SnackController;
public class SnackMenu {
Scanner sc = new Scanner(System.in);
SnackController scr = new SnackController();
public void menu() {
System.out.println("스낵류를 입력하세요.");
System.out.print("종류 : ");
String kind = sc.next();
System.out.print("이름 : ");
String name = sc.next();
System.out.print("맛 : ");
String flavor = sc.next();
System.out.print("개수 : ");
int numOf = sc.nextInt();
System.out.print("가격 : ");
int price = sc.nextInt();
System.out.println(scr.saveData(kind, name, flavor, numOf, price));
System.out.print("저장한 정보를 확인하시겠습니까?(y/n) : ");
String choice = sc.next();
if(choice.equals("y")) {
System.out.println(scr.confirmData());
}
}
}
package com.kh.practice.snack.run;
import com.kh.practice.snack.view.SnackMenu;
public class Run {
public static void main(String[] args) {
SnackMenu snackm = new SnackMenu();
snackm.menu();
}
}