과거⚰️

파일ATM문제 || 파일 장바구니

아무루 2020. 11. 22. 18:45
package day041;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;

public class Day04101파일ATM문제 {
    public static void main(String[] args) {

        /*
         * atm.txt
         * abcd/1234/300
         * qwer/1111/1800
         * javaking/3333/1000
         */

        Scanner scan = new Scanner(System.in);

        int size = 5;
        int count = 0;
        int log = -1;

        String[] accs = new String[size];
        String[]  pws = new String[size];
        int[]  moneys = new int[size];

        String fileName = "atm.txt";

        while(true) {
            System.out.println("[MEGA ATM]");
            System.out.println("[1]회원가입");
            System.out.println("[2]회원탈퇴");
            System.out.println("[3]로그인");
            System.out.println("[4]로그아웃");
            System.out.println("[5]입금");
            System.out.println("[6]출금");
            System.out.println("[7]이체");
            System.out.println("[8]잔액조회");
            System.out.println("[9]저장");
            System.out.println("[10]로드");
            System.out.println("[0]종료");

            System.out.print("메뉴 선택 : ");
            int sel = scan.nextInt();

            if(sel == 1) {
                if (count == size) {
                    System.out.println("더 이상 가입불가 ");
                } else if (count == 0) {
                    System.out.println("id");
                    String id = scan.next();
                    System.out.println("pw");
                    String pw = scan.next();
                    accs[count] = id;
                    pws[count] = pw;
                    moneys[count] = 1000;
                    count++;
                } else if (count > 0) {
                    int check = -1;
                    System.out.println("id");
                    String id = scan.next();
                    for (int i = 0; i < count; i++) {
                        if (accs[i].equals(id)) {
                            check = i;
                        }
                    }
                    if (check != -1) {
                        System.out.println("이미 존재하는 아이디입니다.");
                    } else {
                        System.out.println("pw");
                        String pw = scan.next();
                        accs[count] = id;
                        pws[count] = pw;
                        moneys[count] = 1000;
                        count++;
                    }
                }
            }
            else if(sel == 2) {
                if(log!=-1){
                    System.out.println("로그인 중인 아이디를 입력하세요.");
                    String id = scan.next();
                    System.out.println("로그인 중인 비밀번호를 입력하세요.");
                    String pw = scan.next();
                    if(accs[log].equals(id)&&pws[log].equals(pw)){
                        for(int i=0; i<size; i++){
                           if(i==log){
                               accs[i]=accs[i+1];
                               pws[i]=pws[i+1];
                               moneys[i]=moneys[i+1];
                           }
                        }
                        count--;
                        System.out.println("회원 탈퇴 되었습니다.");
                    }else {
                        System.out.println("틀렸습니다.");
                    }
                }else{
                    System.out.println("로그인 후 이용가능합니다. ");
                }
            }
            else if(sel == 3) {
                System.out.println("id");
                String id = scan.next();
                System.out.println("pw");
                String pw = scan.next();
                int check = -1;
                for (int i = 0; i < count; i++) {
                    if (accs[i].equals(id)) {
                        check = i;
                    }
                }
                if(check==-1){
                    System.out.println("로그인 실패 아이디와 비밀번호를 확인하세요.");
                }else {
                    if(accs[check].equals(id) && pws[check].equals(pw)){
                        log = check;
                        System.out.println("로그인 성공 "+id+"님 환영합니다.");
                    }
                }
            }
            else if(sel == 4) {
                if(log!=-1){
                    System.out.println("로그아웃 하시겠습니까?");
                    System.out.println("1. yes    2. no");
                    int ch = scan.nextInt();
                    if(ch==1){
                        log=-1;
                        System.out.println("로그아웃 성공");
                    }
                }else{
                    System.out.println("로그인 후 이용가능");
                }
            }
            else if(sel == 5) {
                if(log!=-1){
                    System.out.println("입금할 금액을 넣으세요.");
                    int mon = scan.nextInt();
                    moneys[log]+=mon;
                    System.out.println("입금 성공 ");
                }else{
                    System.out.println("로그인 후 이용가능");
                }
            }
            else if(sel == 6) {
                if(log!=-1){
                    System.out.println("출금 금액 입력하세요.");
                    int mon = scan.nextInt();
                    if(mon<=moneys[log]) {
                        moneys[log] -= mon;
                        System.out.println("출금 성공 ");
                    }else{
                        System.out.println("금액 부족");
                    }
                }else{
                    System.out.println("로그인 후 이용가능");
                }
            }
            else if(sel == 7) {
                if(log!=-1){
                    int check = -1;
                    System.out.println("이체할 상대의 계좌를 입력하세요.");
                    String id = scan.next();
                    for (int i = 0; i < count; i++) {
                        if (accs[i].equals(id)) {
                            check = i;
                        }
                    }
                    if(check!=-1){
                        System.out.println("이체 금액 입력하세요.");
                        int mon = scan.nextInt();
                        if(mon<=moneys[log]) {
                            moneys[log] -= mon;
                            moneys[check]+=mon;
                            System.out.println("출금 성공 ");
                        }else{
                            System.out.println("금액 부족");
                        }
                    }else {
                        System.out.println("이체 금액 입력하세요.(타 은행거래 수수료 1%)");
                        int mon = scan.nextInt();
                        double dex = mon * 0.01;
                        if(mon+dex<=moneys[log]) {
                            moneys[log] -= mon;
                            System.out.println("출금 성공 ");
                        }else{
                            System.out.println("금액 부족");
                        }
                    }
                }else{
                    System.out.println("로그인 후 이용가능");
                }
            }
            else if(sel == 8) {
                if(log!=-1){
                    System.out.println("잔액은 " + moneys[log]+"원 입니다.");
                }else{
                    System.out.println("로그인 후 이용가능");
                }
            }
            else if(sel == 9) {
                String data = "";
                for (int i=0; i<count; i++) {
                    data += accs[i];
                    data += "/";
                    data += pws[i];
                    data += "/";
                    data += moneys[i];
                    data += "\n";
                }
                FileWriter fw = null;
                try {
                    fw = new FileWriter(fileName);
                    fw.write(data);
                    fw.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            else if(sel == 10) {
                FileReader fr = null;
                BufferedReader br = null;
                try {
                    fr = new FileReader(fileName);
                    br = new BufferedReader(fr);
                    String text = "";
                    while (true){
                        String line = br.readLine();
                        if(line == null){
                            break;
                        }
                        text += line; text += "\n";
                    }
                    String[] tmp = text.split("\n");
                    accs = new String[tmp.length];
                    pws = new String[tmp.length];
                    moneys = new int[tmp.length];
                    for (int i=0; i< tmp.length; i++){
                        String[] temp = tmp[i].split("/");
                        accs[i]=temp[0];
                        pws[i]=temp[1];
                        moneys[i]=Integer.parseInt(temp[2]);
                    }
                    count = tmp.length;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            else if(sel == 0) {
                break;
            }

        }

    }
}




package day041;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;

public class Day04102파일장바구니문제 {
    public static void main(String[] args) {

        /*
         * jang.txt
         * 1/1
         * 1/2
         * 1/3
         * 2/2
         * 0/1
         */

        Scanner scan = new Scanner(System.in);

        String[] ids = { "qwer", "javaking", "abcd" };
        String[] pws = { "1111", "2222", "3333" };
        String[] items = { "사과", "바나나", "딸기" };

        final int MAX_SIZE = 100;
        int[][] jang = new int[MAX_SIZE][2];

        String fileName = "jang.txt";

        int count = 0;
        int log = -1;

        while (true) {

            for(int i=0; i<10; i++){
                for (int j=0; j<2; j++){
                    System.out.println(jang[i][0]+"/"+jang[i][1]);
                }
            }
            System.out.println("[MEGA SHOP]");
            System.out.println("[1]로그인");
            System.out.println("[2]로그아웃");
            System.out.println("[3]쇼핑");
            System.out.println("[4]장바구니");
            System.out.println("[5]저장");
            System.out.println("[6]로드");
            System.out.println("[0]종료");

            System.out.print("메뉴 선택 : ");
            int sel = scan.nextInt();

            if (sel == 1) {
                if(log==-1) {
                    System.out.println("아이디 입력 ");
                    String id = scan.next();
                    System.out.println("비밀번호 입력 ");
                    String pw = scan.next();
                    int check = -1;
                    for (int i = 0; i < ids.length; i++) {
                        if (ids[i].equals(id)) {
                            check = i;
                        }
                    }
                    if (check == -1) {
                        System.out.println("없는 아이디입니다.");
                        continue;
                    }
                    if (pws[check].equals(pw) && ids[check].equals(id)) {
                        System.out.println("로그인 성공 ");
                        log = check;
                    } else {
                        System.out.println("로그인 실패 아이디와 비밀 번호를 확인하세요.");
                    }
                }else{
                    System.out.println("로그인 중입니다.");
                }
            } else if (sel == 2) {
                if (log == -1) {
                    System.out.println("로그인 후 이용가능");
                } else {
                    log = -1;
                    System.out.println("로그아웃 성공 ");
                }
            } else if (sel == 3) {
                if (log != -1) {
                    for (int i = 0; i < items.length; i++) {
                        System.out.println((i + 1) + "번 " + items[i]);
                    }
                    System.out.println("뭐 드실라우??");
                    int choice = scan.nextInt();
                    choice -= 1;

                    jang[count][0] = log;
                    jang[count][1] = choice;
                    count++;
                    System.out.println("장바구니 넣기 완료!");

                } else {
                     System.out.println("로그인 후 이용가능");
                }
            }
            else if (sel == 4) {
                if(log != -1) {
                    System.out.println("장바구니 확인!");
                    int apple = 0;
                    int banana = 0;
                    int strawberry = 0;
                    for (int i = 0; i < count; i++) {
                        if (jang[i][0] == log){
                            if(jang[i][1]==0){
                                apple++;
                            }else if(jang[i][1]==1){
                                banana++;
                            }else if(jang[i][1]==2){
                                strawberry++;
                            }
                        }
                    }
                    System.out.println(ids[log]+"님의 장바구니 ");
                    if(apple!=0){
                        System.out.println(items[0]+": "+apple+"개");
                    }if(banana!=0){
                        System.out.println(items[1]+": "+banana+"개");
                    }if(strawberry!=0){
                        System.out.println(items[2]+": "+strawberry+"개");
                            }


                }else {
                    System.out.println("로그인 후 이용 가능");
                }
            }
            else if (sel == 5) {
                String data = "";
                for (int i=0; i<count; i++){
                    data += jang[i][0];
                    data += "/";
                    data += jang[i][1];
                    data += "\n";
                }
                FileWriter fw = null;
                try {
                    fw = new FileWriter(fileName);
                    fw.write(data);
                    fw.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            else if (sel == 6) {
                FileReader fr = null;
                BufferedReader br = null;
                try {
                    fr = new FileReader(fileName);
                    br = new BufferedReader(fr);
                    String text = "";

                    while(true) {
                        String test = br.readLine();
                        if(test == null){
                            break;
                        }
                        text += test;
                        text+= "\n";
                    }
                    String[] tmp = text.split("\n");
                    for (int i=0; i< tmp.length; i++){
                        String[] temp = tmp[i].split("/");
                        jang[i][0]=Integer.parseInt(temp[0]);
                        jang[i][1]=Integer.parseInt(temp[1]);
                    }
                    count = tmp.length;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            else if (sel == 0) {
                System.out.println("프로그램 종료");
                break;
            }
        }

    }
}