[과제] 코드 Fitness(배열 전)
FitnessService
import java.util.Scanner;
public class FitnessService {
Scanner keyin = new Scanner(System.in);
Fitness member;
public FitnessService() {
int choice;
while(true) {
choice = menu();
switch(choice) {
case 1:
create();
break;
case 2:
retrieve();
break;
case 3:
update();
break;
case 4:
delete();
break;
case 0: System.out.println("*** 프로그램을 종료합니다. ");
return;
default: System.out.println();
}
}
}
public int menu() {
System.out.println(" [[SCIT Fitness]] ");
System.out.println("====================");
System.out.println(" 1) 회원가입");
System.out.println(" 2) 회원조회");
System.out.println(" 3) 정보수정");
System.out.println(" 4) 탈 퇴");
System.out.println(" 0) 종 료");
System.out.println("====================");
System.out.println(" >> 선 택");
int choice = keyin.nextInt();
return choice;
}
public void create() {
if(member != null) {
System.out.println("*** 회원가입을 할 수 없습니다.");
return;
}
System.out.println(" [[회원가입]] ");
System.out.println(" 아이디 입력 :");
String userid = keyin.next();
System.out.println(" 이름 입력 :");
String name = keyin.next();
System.out.println(" 키 입력: ");
double height = keyin.nextDouble();
System.out.println(" 몸무게 입력: ");
double weight = keyin.nextDouble();
member = new Fitness(userid, name, height, weight);
System.out.println("*** 회원가입이 완료되었습니다.");
}
public void retrieve() {
if(member == null) {
System.out.println("*** 조회할 정보가 없습니다.");
return;
}
System.out.println("\n [[조회된 정보]]");
member.output();
}
public void update() {
if(member == null) {
System.out.println("*** 조회할 정보가 없습니다.");
return;
}
System.out.println(" [[정보 수정]] ");
System.out.print(" 키 입력: ");
double height = keyin.nextDouble();
System.out.print(" 몸무게 입력: ");
double weight = keyin.nextDouble();
member.setHeight(height);
member.setWeight(weight);
System.out.println("*** 수정이 완료되었습니다. ");
}
public void delete() {
if(member == null) {
System.out.println("*** 삭제할 정보가 없습니다.");
return;
}
System.out.println(" [[ 회원탈퇴 ]] ");
member.output();
String answer;
System.out.println("정말로 탈퇴하시겠습니까? (Y/n)");
answer = keyin.next();
if(answer.equals("Y")) {
System.out.println("** 탈퇴 완료되었습니다. ");
member = null;
}
System.out.println("** 탈퇴 처리 취소되었습니다.");
}
}
Fitness
public class Fitness {
private String userid;
private String name;
private double weight;
private double height;
private double bmi;
private String result;
public Fitness() {}
public Fitness(String userid, String name, double weight, double height) {
super();
this.userid = userid;
this.name = name;
this.weight = weight;
this.height = height;
calcBmi();
calcResult();
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
calcBmi();
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
calcBmi();
}
public double getbmi() {
return bmi;
}
public void calcBmi() {
double temp = height/100;
this.bmi = this.weight/(temp*temp);
}
public void calcResult() {
if(this.bmi >= 35) this.result = "고도비만";
else if(this.bmi >= 30) this.result = "중도비만";
else if(this.bmi >= 25) this.result = "경도비만";
else if(this.bmi >= 23) this.result = "과체중";
else if(this.bmi >= 18.5) this.result = "정상";
else this.result = "저체중";
}
public String getResult() {
return result;
}
public void output() {
System.out.printf("%s %s %.2fkg %.2fcm %.2f %s", userid, name, weight, height, bmi, result);
System.out.println();
}
}
FitnessTest
public class FitnessTest {
public static void main(String[] args) {
new FitnessService();
}
}