Kim님이 4000으로 별 다방 아메리카노를 구입했습니다.
Lee님이 4500으로 콩 다방 라떼를 구입했습니다.
public class Menu {
public static final int STARAMERICANO = 4000;
public static final int STARLATTE = 4300;
public static final int BEANAMERICANO = 4100;
public static final int BEANLATTE = 4500;
}
public class StarCoffee {
int money;
public String bewing(int money) {
this.money += money;
if (money == Menu.STARAMERICANO) {
return "별 다방 아메리카노를 구입했습니다.";
}
else if(money == Menu.STARLATTE){
return "별 다방 라떼를 구입했습니다.";
}
else return null;
}
}
public class BeanCoffee {
int money;
public String bewing(int money) {
this.money += money;
if (money == Menu.BEANAMERICANO) {
return "콩 다방 아메리카노를 구입했습니다.";
}
else if(money == Menu.BEANLATTE){
return "콩 다방 라떼를 구입했습니다.";
}
else return null;
}
}
public class Person {
String name;
int money;
Person(String name, int money){
this.name = name;
this.money = money;
}
public void buyStarCoffee(StarCoffee sCoffee, int money){
String message = sCoffee.bewing(money);
if(message != null){
this.money -= money;
System.out.println(name + "님이 " + money + "으로 " + message);
}
}
public void buyBeanCoffee(BeanCoffee bCoffee, int money){
String message = bCoffee.bewing(money);
if(message != null){
this.money -= money;
System.out.println(name + "님이 " + money + "으로 " + message);
}
}
}
public class CoffeeTest {
public static void main(String[] args){
Person personKim = new Person("Kim", 10000);
Person personLee = new Person("Lee", 20000);
StarCoffee starCoffee = new StarCoffee();
BeanCoffee beanCoffee = new BeanCoffee();
personKim.buyStarCoffee(starCoffee, Menu.STARAMERICANO);
personLee.buyBeanCoffee(beanCoffee, Menu.BEANLATTE);
}
}
자동차 공장이 있습니다. 자동차 공장은 유일한 객체이고, 이 공장에서 생산되는 자동차는 제작될 때마다 고유의 번호가 부여됩니다. 자동차 번호가 10001부터 시작되어 자동차가 생산될 때마다 10002, 10003 이렇게 번호가 붙도록 자동차 공장 클래스, 자동차 클래스를 구현하세요.
다음 CarFactory.java 테스트 코드가 수행되도록 합니다.
public class Car {
private static int seralNum = 10000;
private int carNum;
public Car() {
seralNum++;
carNum = seralNum;
}
public int getCarNum() {
return carNum;
}
}
public class CarFactory {
// Factory는 한개
private static CarFactory instance = new CarFactory();
// default 생성자가 어차피 생기니까 만들어주고
private CarFactory() {
}
public static CarFactory getInstance() {
if (instance == null) {
instance = new CarFactory();
}
return instance;
}
public Car createCar() {
Car car = new Car();
return car;
}
}
public class CarFactoryTest {
public static void main(String[] args) {
CarFactory factory = CarFactory.getInstance();
Car mySonata = factory.createCar();
Car yourSonata = factory.createCar();
System.out.println(mySonata.getCarNum()); // 10001 출력
System.out.println(yourSonata.getCarNum()); // 10002 출력
}
}
1001학번 Lee와 1002학번 Kim, 두 학생이 있습니다.
Lee 학생은 국어와 수학 2과목을 수강했고, Kim 학생은 국어, 수학, 영어 3 과목을 수강하였습니다.
Lee 학생은 국어 100점, 수학 50점입니다.
Kim 학생은 국어 70점, 수학 85점, 영어 100점입니다.
Student와 Subject 클래스를 만들고 ArrayList를 활용하여 두 학생의 과목 성적과 총점을 출력하세요
import java.util.ArrayList;
public class Student {
int studentID;
String studentName;
ArrayList<Subject> subjectList;
public Student(int studentID, String studentName) {
this.studentID = studentID;
this.studentName = studentName;
subjectList = new ArrayList<Subject>();
}
public void addSubject(String name, int score) {
Subject subject = new Subject();
subject.setName(name);
subject.setScorePoint(score);
subjectList.add(subject);
}
public void showStudentInfo() {
int total = 0;
for (Subject s : subjectList) {
total += s.getScorePoint();
System.out.println("학생 " + studentName + "의 " + s.getName() + " 과목 성적은 " + s.getScorePoint() + "입니다.");
}
System.out.println("학생 " + studentName + "의 총점은" + total + "입니다.");
}
}
public class Subject {
private String name;
private int scorePoint;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScorePoint() {
return scorePoint;
}
public void setScorePoint(int scorePoint) {
this.scorePoint = scorePoint;
}
}
public class StudentTest {
public static void main(String[] args) {
Student studentLee = new Student(1001, "Lee");
studentLee.addSubject("국어", 100);
studentLee.addSubject("수학", 90);
Student studentKim = new Student(1001, "Kim");
studentKim.addSubject("국어", 85);
studentKim.addSubject("수학", 100);
studentKim.addSubject("영어", 95);
studentLee.showStudentInfo();
System.out.println("==============================");
studentKim.showStudentInfo();
}
}