- 질문 게시판입니다.
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


목록
번호 제목 이름 날짜 조회 추천
3146 의료/건강머리가 띵합니다. 20 Toby 17/08/03 3902 0
5062 의료/건강정신과 약 처방 중에서 2 psyredtea 18/07/16 3902 0
11726 IT/컴퓨터카메라 훈수 부탁드립니다 (g7x vs m50 m2) 7 ikuk 21/06/14 3902 0
2005 게임아재들 바둑 배울라믄 어딜루 가야돼요(...) 3 진준 16/12/31 3901 0
3700 기타다이어리 추천 받을 수 있을까요? 3 Homo_Skeptic 17/11/17 3901 0
5371 경제합리적인 부동산 정책?? 31 메존일각 18/09/02 3900 0
15543 기타광고사진 모델 이름이 궁금합니다 5 바이엘 24/01/13 3900 0
3534 IT/컴퓨터자바 질문입니다 3 二ッキョウ니쿄 17/10/19 3899 0
4122 연애어떻게 하면 여자친구랑 오래 갈수 있을까요? 30 [익명] 18/02/08 3899 0
5745 여행광주 초행인 사람에게 추천해주실 식당 있으실까요. 10 Messi 18/10/26 3899 0
6177 가정/육아친구 출산 선물 추천부탁드립니다. 16 [익명] 18/12/28 3899 0
6563 의료/건강뇌진탕 이후의 성격이나 사고의 변화 7 [익명] 19/02/15 3899 0
7308 IT/컴퓨터윈도우에서 무료 IDE로 리눅스 타겟의 어플리케이션을 개발하는 방법 2 흥차넷 19/06/13 3899 0
11536 과학헤르만 격자 착시현상과 관련한 질문 4 주디 21/05/14 3899 0
9309 법률사람 습격하는 개 어떻게 처리할 수 있나요 10 [익명] 20/05/01 3898 0
13654 IT/컴퓨터와이파이 구매 추천 부탁드립니다. 3 아재 22/07/19 3898 0
927 의료/건강예방의학과는 무슨일을 하나요? 4 스키너 16/03/17 3897 0
2698 문화/예술영화 추천 부탁드립니다. 11 Liebe 17/04/23 3897 0
5215 기타영어 문법 (가정법 true/untrue) 질문입니다. 7 [익명] 18/08/07 3897 0
6795 철학/종교두려움으로 인해 질문해요 13 [익명] 19/03/19 3897 0
7774 게임스팀 한글화 게임, 비 가챠 핸드폰 겜 정보를 볼 수 있는 곳 2 불타는밀밭 19/08/30 3897 0
8063 법률경찰 고소 취하 관련 질문드립니다. 4 [익명] 19/10/18 3897 0
8151 연애고양이님께 예쁨 받고 싶습니다.. 9 [익명] 19/10/30 3897 0
11142 체육/스포츠달리기 중 통증관련 질문 3 [익명] 21/03/06 3897 0
440 기타목소리가 좋아지려면? 4 집정관 15/11/10 3896 0
목록

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

댓글