- 질문 게시판입니다.
Date 17/10/19 15:33:52
Name   二ッキョウ니쿄
Subject   자바 질문입니다
package com.ex11market;
[메인클래스]
public class MarketMain {
static Goods[] g = { new Goods("g01", "레쓰비", 25, 900), new Goods("g02", "박카스", 25, 700),
new Goods("g03", "ABC초콜릿", 25, 1600), new Goods("g04", "크리넥스", 25, 2000),
new Goods("g05", "전자담배", 25, 90000) };
static Customer[] c = { new Customer("010-1111-1111", "서울이", "10-10"),
new Customer("010-1212-1212", "고려놈", "10-18"), new Customer("010-1313-1313", "연세놈", "01-11"),
new Customer("010-1414-1414", "서강이", "02-27"), new Customer("010-1515-1515", "세종이", "06-30") };

public static void main(String[] args) {
sell("g05", "1515", 3);
sell("g02", "1212", 5);
}

private static void sell(String gCode, String subTel, int su) {
int i, gIndex, cIndex;
for (i = 0; i < g.length; i++) {
if (g[i].getgCode().equals(gCode)) {
break;
}
}
if (i == g.length) {
System.out.println("상품코드가 잘못 되었습니다");
return;
}
gIndex = i; // 상품이 가르키는 곳의 인덱스
for (i = 0; i < c.length; i++) {
String tel = c[i].getTel();// 010-9999-9999
if (tel.substring(9).equals(subTel)) {
break;
}
}
if (i == c.length) {
System.out.println("전화번호가 잘못 되었습니다. 회원가입하시죠");
return;
}
cIndex = i; // 고객을 가르키는 곳의 인덱스
int tempSu = g[gIndex].getStock() - su;
if (tempSu < 0) {
System.out.println("재고량이 부족합니다. 죄송합니다.");
System.out.println("사장님!" + g[gIndex].getgName() + "얼른 주문하세요");
return;
}
g[gIndex].setStock(tempSu);
String gName = g[gIndex].getgName();
int price = g[gIndex].getPrice();
c[cIndex].buy(gName, price, su);
}

}

[상품클래스]
public class Goods {
private String gCode;
private String gName;
private int stock;
private int price;
public Goods() {
}
public Goods(String gCode, String gName, int stock, int price) {
this.gCode = gCode;
this.gName = gName;
this.stock = stock;
this.price = price;
}
public String getgCode() {
return gCode;
}
public void setgCode(String gCode) {
this.gCode = gCode;
}
public String getgName() {
return gName;
}
public void setgName(String gName) {
this.gName = gName;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}

}

[고객클래스]

import java.text.SimpleDateFormat;
import java.util.Date;

public class Customer {
private String tel;
private String cName;
private int money;
private int point;
private String birth;
private boolean vip;
public Customer() {
}
public Customer(String tel, String cName, String birth) {
this.tel = tel;
this.cName = cName;
this.birth = birth;
money = 0; point = 0;
}
public void changedTel(String cName,String tel) {
this.tel = tel;
}
public void buy(String gName, int price, int su) {
if(price<0) {
System.out.println("구매금액이 잘못되었습니다");
return;
}
money += price*su;
int thispoint = (int)(price*su*0.1);
point += thispoint;
if(money>=1000000 && vip == false) {
vip = true;
System.out.println("vip가 되셨습니다");
}
msgBirth();
System.out.println(gName+""+su+"개 *"+price+"원 = "+(price*su));
System.out.println("이번 누적 포인트는 "+thispoint+"원 입니다");
System.out.println(cName+"고객님 포인트 합계 : "+point+"원");
System.out.println("★ ★ ★ 감  사  합  니  다 ★ ★ ★");
}
public void msgBirth() {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
String today = sdf.format(now);
if(birth.equals(today)) {
System.out.println(cName+"님 생일 축하드립니다");
point+=3000;
System.out.println("생일 기념으로 3000포인트가 적립됩니다");
}
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public boolean isVip() {
return vip;
}
public void setVip(boolean vip) {
this.vip = vip;
}
}
[콘솔창 결과물]

전자담배3개 *90000원 = 270000
이번 누적 포인트는 27000원 입니다
세종이고객님 포인트 합계 : 27000원
★ ★ ★ 감  사  합  니  다 ★ ★ ★
박카스5개 *700원 = 3500
이번 누적 포인트는 350원 입니다
고려놈고객님 포인트 합계 : 350원
★ ★ ★ 감  사  합  니  다 ★ ★ ★

=================================================================================

안녕하세요 자바 4주차 공부중인 코린이입니다.
슈퍼마켓에서 배열등을 이용해 콘솔창에서 물건을 사고 재고랑 가격, 포인트 등을 만드는 코드를 짰는데요
제가 배운 영역에서 어떻게 더 리팩토링해서 보기 좋고 더 나은 ? 상태로 만들어야 할지 더 알수가 없어서 질문드립니다.
혹시 어떤식으로 고치는게 좋을까요?
제반지식은 본문에 쓰인 코드랑 싱글턴, 스트레터지 패턴, 대표적인 api 몇가지랑 추상화,인터페이스, 상속, 접근제한자 및 기본적인 변수 연산자 제어문 배열정도입니다..

깔끔하게 올릴 방법을 몰라서 이렇게 올리는데 도움 주시면 감사드리겠습니다.



0


목록
번호 제목 이름 날짜 조회 추천
7879 게임틀래식 애드온 질문(마법부여) leiru 19/09/17 3025 0
8070 의료/건강이런거 정신병원 상담이 효과가 있을까요? 5 [익명] 19/10/19 3025 0
9770 기타질문 - 한국공군 전투기 수 제한 4 [익명] 20/07/15 3025 0
13211 기타통증질문 4 [익명] 22/04/04 3025 0
4847 문화/예술이순신 해전 연구의 대가? 4 메존일각 18/06/18 3026 0
11684 체육/스포츠캐치볼용 글러브 추천 부탁드려요. 3 활활태워라 21/06/07 3026 0
12697 IT/컴퓨터맥 커서 질문 4 토비 21/12/16 3026 0
3784 진로현재의 삶이 싫고 변하고 싶은데요 17 [익명] 17/12/03 3027 3
4640 가정/육아삭제 22 [익명] 18/05/16 3027 0
7774 게임스팀 한글화 게임, 비 가챠 핸드폰 겜 정보를 볼 수 있는 곳 2 불타는밀밭 19/08/30 3027 0
1673 기타헌혈 금지 약물 질문입니다. 3 집정관 16/10/23 3028 0
5786 의료/건강의료소송 가능성 등에 대한 질문 드립니다. 19 [익명] 18/10/31 3028 0
9937 의료/건강팔 통증 질문드립니다. 5 Might 20/08/13 3028 0
13034 기타사람 간의 관계 전반에 관한 질문 32 아마존 22/02/27 3028 0
2444 법률이런 경우 제가 취할 수 있는 법률적 조치가 존재할까요? 10 tannenbaum 17/03/04 3029 0
3534 IT/컴퓨터자바 질문입니다 3 二ッキョウ니쿄 17/10/19 3029 0
3881 기타경주시 관련 질문 24 SpicyPeach 17/12/22 3029 0
6370 의료/건강어지럽고 눈이 부시는데 괜찮을까요? 2 모이 19/01/22 3029 0
8493 여행강릉에서 뭘 먹어야할까요?? 8 whenyouinRome... 19/12/17 3029 0
9789 기타중학교 때 알던 친구 아버님 장례식장에 가시나요? 8 [익명] 20/07/19 3029 0
14170 기타어느 직장이든 6개월 1년이상일때 제일 힘든 시기일까요? 13 셀레네 22/11/22 3029 0
14194 IT/컴퓨터가족끼리 쓸 일정공유앱 있을까요? 14 21700 22/11/28 3029 0
1638 의료/건강동생이 기면증을 걱정하고있습니다. 3 쉬군 16/10/10 3030 0
2252 IT/컴퓨터트위터가 정지되었습니다. 불타는밀밭 17/02/04 3030 0
4544 의료/건강상담이나 치료가 필요한 정도일까요? 3 [익명] 18/04/29 3030 0
목록

+ : 최근 2시간내에 달린 댓글
+ : 최근 4시간내에 달린 댓글

댓글