커피메뉴를 파일에 쓰기
package 직렬화메뉴쓰기;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class SerialMenuWrite {
static Map<String, CoffeeMenuInfo> map = new HashMap<>();
public static void main(String[] args) throws IOException {
menuWrite();
selectMenu();
}
static void menuWrite() {
map.put("Americano",
new CoffeeMenuInfo("Americano", 2500, "Coffee", "그냥 커피"));
map.put("Espresso",
new CoffeeMenuInfo("Espresso", 2500, "Coffee", "진한 커피"));
map.put("Latte",
new CoffeeMenuInfo("Latte", 2500, "Coffee", "진한 커피"));
}
static void selectMenu() throws IOException {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("메뉴를 선택 하세요 : ");
System.out.print("[1]메뉴 보기, [2]메뉴 추가, [3]종료 : ");
int inputMenu = sc.nextInt();
switch(inputMenu) {
case 1 :
System.out.println("========= 메뉴 보기 =========");
for(String e : map.keySet()) {
System.out.println("메뉴 : " + map.get(e).getName());
System.out.println("메뉴 : " + map.get(e).getPrice());
System.out.println("분류 : " + map.get(e).getGroup());
System.out.println("설명 : " + map.get(e).getDesc());
System.out.println("---------------------------------");
}
break;
case 2 :
System.out.print("추가 할 메뉴를 입력 하세요 : ");
String key = sc.next();
if(map.containsKey(key)) {
System.out.println("해당 메뉴가 이미 존재 합니다.");
} else {
System.out.print("가격 입력 : ");
int price = sc.nextInt();
System.out.print("분류 입력 : ");
String grp = sc.next();
System.out.print("설명 입력 : ");
String desc = sc.next();
map.put(key, new CoffeeMenuInfo(key, price, grp, desc));
}
break;
case 3 :
System.out.println("메뉴를 종료 합니다. 내용을 파일에 저장 합니다.");
FileOutputStream fos = new FileOutputStream("C:/파일입출력tmp/커피메뉴만들기.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(map);
oos.flush();
oos.close();
return;
}
}
}
}
package 직렬화메뉴쓰기;
import java.io.Serializable;
public class CoffeeMenuInfo implements Serializable {
private String name;
private int price;
private String group;
private String desc;
public CoffeeMenuInfo(String name, int price, String group, String desc) {
this.name = name;
this.price = price;
this.group = group;
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
커피 메뉴를 파일에서 읽기
package 직렬화메뉴읽기;
import 직렬화메뉴쓰기.CoffeeMenuInfo;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class SerialMenuRead {
public static void main(String[] args) throws IOException, ClassNotFoundException {
getMenuList();
}
static void getMenuList() throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("C:/파일입출력tmp/커피메뉴만들기.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Map<String, CoffeeMenuInfo> map = new HashMap<>();
map = (Map<String, CoffeeMenuInfo>) ois.readObject();
System.out.println("<< 역직렬화해서 메뉴를 가져 옴 >>");
for(String e : map.keySet()) {
System.out.println("메뉴 : " + map.get(e).getName());
System.out.println("가격 : " + map.get(e).getPrice());
System.out.println("분류 : " + map.get(e).getGroup());
System.out.println("설명 : " + map.get(e).getDesc());
System.out.println("-------------------------------------");
}
fis.close();
ois.close();
}
}