프로그래밍 언어 복습/JAVA
#48 JAVA 매우 조잡한 마트게임
사재원 교수
2022. 6. 4. 21:59
제가 봐도 굉장히 허접하고 노가다처럼 짠 코드네요 ... 허허.. 그래도 일단은 세이브파일 개념으로 올려봅니다 ... !!
package NewProject;
import java.util.Random;
import java.util.Scanner;
//컴퓨터
class Computer{
}
//에어콘
class Aircon{
}
//냉장고
class Icon{
}
//공기 청정기
class AirClear{
}
//마트 클래스
class Mart{
Computer[] cm = new Computer[10]; //컴퓨터 10개
Aircon[] ac = new Aircon[10]; //에어컨 10개
Icon[] ic = new Icon[10]; //냉장고 10개
AirClear[] aic = new AirClear[10]; //공기청정기 10개
Mart() {
for(int i=0;i<10;i++) {
cm[i] = new Computer(); //컴퓨터 40원
ac[i] = new Aircon(); //에어컨 60원
ic[i] = new Icon(); //냉장고 80원
aic[i] = new AirClear(); //공기청정기 30원
}
//4개의 전자제품 각각 10개씩 객체생성
}
}
// 소비자 클래스
class Costumer {
String name;
Scanner sc = new Scanner(System.in);
int money = 200; //200원의 돈을 들고있음
Computer[] cm = new Computer[10]; //컴퓨터 10개
Aircon[] ac = new Aircon[10]; //에어컨 10개
Icon[] ic = new Icon[10]; //냉장고 10개
AirClear[] aic = new AirClear[10]; //공기청정기 10개
Costumer(String name) {
this.name = name;
}
public void Buy(Mart m) {
m = new Mart();
System.out.println(name + "님 무엇을 구매하시겠습니까? ");
System.out.print("1.컴퓨터 2.에어컨 3.냉장고 4.공기청정기 : ");
int choice = sc.nextInt();
if(choice == 1) { //컴퓨터 구매
//살 돈이 있는지 체크
if (money < 40) {
System.out.println("잔액이 부족합니다.");
}else {
// 마트에서 팔 컴퓨터 인덱스 얻어오기
int j = 0;
for(int i=0;i<m.cm.length;i++) {
if(m.cm[i] != null) {
j = i;
break;
}else {
System.out.println("컴퓨터가 다 팔렸습니다.");
break;
}
}
// 소비자가 컴퓨터를 보관할 위치 인덱스 얻어오기
int k = 0;
for(int i=0;i<cm.length;i++) {
if(m.cm[i] == null) {
k = i;
break;
}
}
cm[k] = m.cm[j];
m.cm[j] = null;
money -= 40;
System.out.println(name + "님 컴퓨터를 구매하였습니다. ");
System.out.println(name + " 남은 금액 : " + money);
}
}else if(choice == 2) { //에어컨 구매
//살 돈이 있는지 체크
if (money < 60) {
System.out.println("잔액이 부족합니다.");
}else {
// 마트에서 팔 에어컨 인덱스 얻어오기
int j = 0;
for(int i=0;i<m.ac.length;i++) {
if(m.ac[i] != null) {
j = i;
break;
}else {
System.out.println("에어컨이 다 팔렸습니다.");
break;
}
}
// 소비자가 에어컨을 보관할 위치 인덱스 얻어오기
int k = 0;
for(int i=0;i<ac.length;i++) {
if(m.ac[i] == null) {
k = i;
break;
}
}
ac[k] = m.ac[j];
m.ac[j] = null;
money -= 60;
System.out.println(name + "님 에어컨을 구매하였습니다. ");
System.out.println(name + " 남은 금액 : " + money);
}
}else if(choice == 3) { //냉장고 구매
//살 돈이 있는지 체크
if (money < 80) {
System.out.println("잔액이 부족합니다.");
}else {
// 마트에서 팔 냉장고 인덱스 얻어오기
int j = 0;
for(int i=0;i<m.ic.length;i++) {
if(m.ic[i] != null) {
j = i;
break;
}else {
System.out.println("냉장고가 다 팔렸습니다.");
break;
}
}
// 소비자가 냉장고를 보관할 위치 인덱스 얻어오기
int k = 0;
for(int i=0;i<ic.length;i++) {
if(m.ic[i] == null) {
k = i;
break;
}
}
ic[k] = m.ic[j];
m.ic[j] = null;
money -= 80;
System.out.println(name + "님 냉장고를 구매하였습니다. ");
System.out.println(name + " 남은 금액 : " + money);
}
}else if(choice == 4) { //공기청정기 구매
//살 돈이 있는지 체크
if (money < 30) {
System.out.println("잔액이 부족합니다.");
}else {
// 마트에서 팔 공기청정기 인덱스 얻어오기
int j = 0;
for(int i=0;i<m.aic.length;i++) {
if(m.aic[i] != null) {
j = i;
break;
}else {
System.out.println("컴퓨터가 다 팔렸습니다.");
break;
}
}
// 소비자가 공기청정기를 보관할 위치 인덱스 얻어오기
int k = 0;
for(int i=0;i<aic.length;i++) {
if(m.aic[i] == null) {
k = i;
break;
}
}
aic[k] = m.aic[j];
m.aic[j] = null;
money -= 30;
System.out.println(name + "님 공기청정기를 구매하였습니다. ");
System.out.println(name + " 남은 금액 : " + money);
}
}
}
}
public class MartGame {
public static void main(String[] args) {
Mart m = new Mart();
Costumer one = new Costumer("사재원");
Costumer two = new Costumer("사재투");
while(true) {
int count = 0;
if(one.money < 30) {
System.out.println(one.name + "님은 잔액이 없어 사실수 있는 제품이 없습니다.");
count ++;
}else {
one.Buy(m);
}
if(two.money < 30) {
System.out.println(two.name + "님은 잔액이 없어 사실수 있는 제품이 없습니다.");
count ++;
}else {
two.Buy(m);
}
if(count == 2) {
System.out.println("두 분 금액이 부족하여 자동으로 프로그램을 종료합니다.");
break;
}
}
}
}
감사합니다.