Java

2023.01.09 cafe 코드 정리

연을 2023. 1. 9. 22:16
728x90

<Cafe>

import java.util.ArrayList;
import java.util.Scanner;
public class Cafe {

   public static void main(String[] args) {
         Scanner s = new Scanner(System.in);
         Scanner s1 = new Scanner(System.in);

         Menu menu = new Menu();
         Sales sales = new Sales();
         
         String job="";
         String mjob="";
         do {
            System.out.println("실행하실 관리를 선택하세요:[m:메뉴관리] [o:주문관리] [s:매출관리] [Enter:종료] ");
            job = s.nextLine();
            if(job.equals("m")||job.equals("M")) {
               do {
                  System.out.println("메뉴관리를 선택하시오: [C:메뉴추가] [R:메뉴목톡표시] [U:메뉴수정] [D:메뉴삭제]");
                  mjob = s.nextLine();
                  if(mjob.equals("c")||mjob.equals("C")) {
                     menu.build();
                  }else if(mjob.equals("r")||mjob.equals("R")) {
                     menu.display();
                  }else if(mjob.equals("u")||mjob.equals("U")) {
                     menu.modify();
                  }else if(mjob.equals("d")||mjob.equals("D")) {
                     menu.remove();
                  }
               }while(!mjob.equals(""));
               menu.save();//읽어들이고 저장
            }else if(job.equals("o")|| job.equals("O")) {
               Order order = null;
               do{
                  System.out.println("주문작업을 선택하시오:[c:메뉴추가] [r:메뉴목록표시] [d:메뉴삭제]");
                  mjob=s.nextLine();
                  if(mjob.equals("c")) {
                     order = new Order(menu);//class객체를 가져오기 위해서 여기로 와야 한다.
                     order.build();
                  }else if(mjob.equals("r")){
                     order.display();
                  }else if(mjob.equals("d")) {
                     order.remove();
                  }
               }while(!mjob.equals(""));
               System.out.println("마일리지 적립할 모바일 번호를 입력하시오 ['':건너뜀]");
               String mobile = s.nextLine();
               //주문내역->매출내역에 추가
               sales.build(order,mobile);
            }else if(job.equals("s")|| job.equals("S")) {
               sales.getTotal();
            }
         } while(!job.equals(""));
         System.out.println("관리스템 종료");
   }

}

<Menu>

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;//ctrl+shift+O
import java.util.Scanner;

public class Menu {//모양만 만들기때문에 실행문을 쓰지 않는다.
   static /*final*/ float pi;//static float pi=3.14f;이렇게 초기화도 가능
   private ArrayList<String> alMenu; //private으로 선언하면 무조건 return 값 선언해야 다른 class에서도 쓸 수 있다.
   private ArrayList<Integer> alPrice;

   Scanner s,s1;
   static {
      pi=3.14f;
   }
   
   public Menu() {//초기화
      this.alMenu = new ArrayList<String>();
      this.alPrice = new ArrayList<Integer>();
      s = new Scanner(System.in);
      s1 = new Scanner(System.in);
      
      try {
         Scanner fs = new Scanner(new File("d:\\cafe\\menu.txt"));
         while(fs.hasNext()) {
            String str = fs.next();
            str.split(",");
            String[] menuitem = str.split(",");
            this.append(menuitem[0], Integer.parseInt(menuitem[1]));
//            this.alMenu.add(menuitem[0]);
//            this.alPrice.add(Integer.parseInt(menuitem[1]));//int로 바꿔주는 함수
            
         }
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
   }
   int getMenuSize() {
      return this.alMenu.size();
   }
   String getMenuName(int i) {
      return this.alMenu.get(i);
   }
   int getMenuPrice(int i) {
      return this.alPrice.get(i);
   }
   void save() {
      try {
         File f = new File("d:\\cafe\\menu.txt");
         FileWriter fw = new FileWriter(f);
         for(int i=0;i<this.alMenu.size();i++) {
            String str = this.alMenu.get(i)+","+this.alPrice.get(i)+"\n";
            fw.write(str);
         } 
         fw.close();
      } catch (IOException e) {
            e.printStackTrace();
      }
      Menu.pi=3.145f;
   }
   
   void build() {
      
      System.out.println("메뉴명을 입력하세요:, [나가기:Enter]");
      String name = this.s.nextLine();
      while(!name.equals("")) {
         System.out.println(name+" 메뉴가격을 입력하세요: ");
         int price = this.s1.nextInt();
         if(price==-1) break;
         this.append(name,price);
         
         System.out.println("메뉴명을 입력하세요:, [나가기:Enter]");
         name = this.s.nextLine();
      }
   }
   void modify() {
      System.out.println("메뉴수정할 번호을 입력하세요, [나가기:-1]");
      this.display();
      int ndx = s1.nextInt();
      while(ndx!=-1) {
         System.out.println("메뉴명 : ");
         String name = s.nextLine();
         System.out.println("가격 : ");
         int price = s1.nextInt();
         this.update(ndx, name, price);
         
         System.out.println("메뉴수정할 번호을 입력하세요, [나가기:-1]");
         this.display();
         ndx = s1.nextInt();
      }
   }
   void remove() {
      System.out.println("삭제할 메뉴 번호를 입력하세요 : ,[나가기:-1]");
      this.display();
         int ndx = s1.nextInt();
      while(ndx!=-1) {
         this.delete(ndx);
         this.display();
         System.out.println("삭제할 메뉴번호, [나가기 -1]");
         ndx=s1.nextInt();
      }
   }
   void append(String name, int price) {//메뉴, 가격 추가
      this.alMenu.add(name); 
      this.alPrice.add(price);
   }
   void display() {//메뉴,가격 리스트 출력
      for(int i=0;i<alMenu.size();i++) {
         System.out.println("["+i+"] "+ "["+alMenu.get(i)+"] "+"["+alPrice.get(i)+"]");
      }
   }
   void delete(int menu_num) {//메뉴, 가격 삭제
      this.alMenu.remove(menu_num);
      this.alPrice.remove(menu_num);
   }
   void update(int menu_num, String name, int price) { //메뉴번호에 새 메뉴명, 새 가격으로 수정
      this.alMenu.set(menu_num, name);
      this.alPrice.set(menu_num, price);
   }
}

<Order>

import java.util.ArrayList;
import java.util.Scanner;

public class Order {
   private ArrayList<String> alName;
   private ArrayList<Integer> alCount;
   private ArrayList<Integer> alSum;
   Menu m;
   private Scanner s, s1;
   
   public Order(Menu m) {
      this.alName = new ArrayList<String>();
      this.alCount = new ArrayList<Integer>();
      this.alSum = new ArrayList<Integer>();
      this.m = m;
      this.s=new Scanner(System.in);
      this.s1=new Scanner(System.in);
   }
   int getNameSize() {
      return this.alName.size();
   }
   String getName(int i) {
      return this.alName.get(i);
   }
   int getCount(int i) {
      return this.alCount.get(i);
   }
   int getSum(int i) {
      return this.alSum.get(i);
   }
   void build() {
      //this.m.display();
      for(int i=0;i<this.m.getMenuSize();i++) {
         System.out.println(i+"."+this.m.getMenuName(i)+" "+this.m.getMenuPrice(i));
      }
      System.out.println("원하는 메뉴번호, [나가기 -1]");
      int m_num=this.m.s1.nextInt();//메뉴class에서 가져온다.
      while(m_num!=-1) {
         System.out.println("수량?, [나가기 -1]");
         int qty=m.s1.nextInt();
         String name = this.m.getMenuName(m_num);
         int price = this.m.getMenuPrice(m_num);
         
         this.append(name, qty, price);//주문내역 저장
         
         this.m.display();
         System.out.println("원하는 메뉴번호, [나가기 -1]");
         m_num=this.m.s1.nextInt();
      }
   }
   void remove() {
      //주문내역출력
      //삭제할 주문번호 입력
      //while(주문번호!=-1){
      //      삭제(delete());
      //      주문내역출력
      //      삭제할 주문번호 입력
      //}
      this.display();
      System.out.println("원하는 주문번호, 나가기 -1");
      int o_num=this.m.s1.nextInt();
      while(o_num!=-1) {
         this.delete(o_num);
         this.display();
         System.out.println("삭제할 주문번호, 나가기 -1");
         o_num=this.m.s1.nextInt();
      }
   }
   
   void append(String name, int qty, int net) {//메뉴, 수량, 단가 추가
      this.alName.add(name); 
      this.alCount.add(qty);
      this.alSum.add(qty*net);
   }
   
   void display() {
      for(int i=0; i<alName.size();i++) {
         System.out.println("번호 "+i+" 메뉴 : "+alName.get(i)+" 수량 : "+alCount.get(i)+" 총액 : "+alSum.get(i));
      }
   }
   void delete(int order_num) {
      this.alName.remove(order_num);
      this.alCount.remove(order_num);
      this.alSum.remove(order_num);
   }

}

<Sales>

import java.util.ArrayList;

public class Sales {
   ArrayList<String> alMobile;
   ArrayList<String> alName;
   ArrayList<Integer> alCount;
   ArrayList<Integer> alSum;

   
   public Sales(){
      this.alMobile = new ArrayList<String>();
      this.alName = new ArrayList<String>();
      this.alCount = new ArrayList<Integer>();
      this.alSum = new ArrayList<Integer>();
      
   }
   public void build(Order order, String mobile) {
      for(int i=0;i<order.getNameSize();i++) {//주문내역을 매출내역에 넣기
         this.alMobile.add(mobile);
         this.alName.add(order.getName(i));
         this.alCount.add(order.getCount(i));
         this.alSum.add(order.getSum(i));
      }
   }
   public void getTotal() {
      int total = 0;
      for (int sum : this.alSum) {
         total=total+sum;
      }
      System.out.println("매출총액은 : ["+total+"원] 입니다.");
   }


}
728x90