Advertisement
cd62131

MemoList

Mar 12th, 2014
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.90 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.Writer;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Scanner;
  9.  
  10. public class MemoList {
  11.   public static void main(String[] args) {
  12.     MemoList memo =
  13.       new MemoList(args.length == 1 ? args[0] : "memo");
  14.     while (true) {
  15.       System.out.print("> コマンドを入力してください: ");
  16.       String command = memo.in.nextLine();
  17.       if (command.equals("list")) memo.list();
  18.       else if (command.equals("add")) memo.add();
  19.       else if (command.equals("del")) memo.delete();
  20.       else if (command.equals("upd")) memo.update();
  21.       else if (command.equals("save")) memo.save();
  22.       else if (command.equals("h")) memo.help();
  23.       else if (command.equals("q")) break;
  24.       else memo.help();
  25.     }
  26.     memo.in.close();
  27.   }
  28.   private final List<String> memolist;
  29.   public Scanner in;
  30.   private final File file;
  31.  
  32.   public MemoList(String file_name) {
  33.     file = new File(file_name);
  34.     Scanner fin = null;
  35.     try {
  36.       fin = new Scanner(file);
  37.     }
  38.     catch (FileNotFoundException e) {
  39.       System.err.println("File Not Found");
  40.       System.exit(1);
  41.     }
  42.     memolist = new ArrayList<String>();
  43.     while (fin.hasNextLine())
  44.       memolist.add(fin.nextLine());
  45.     fin.close();
  46.     in = new Scanner(System.in);
  47.   }
  48.  
  49.   private void add() {
  50.     if (memolist.size() >= 10) {
  51.       System.err.println("メモがいっぱいです");
  52.       return;
  53.     }
  54.     System.out.print("> メモ: ");
  55.     String new_memo = in.nextLine();
  56.     memolist.add(new_memo);
  57.   }
  58.  
  59.   private void delete() {
  60.     System.out.print("> 番号: ");
  61.     int index = Integer.parseInt(in.nextLine());
  62.     if (index < 1 || index > memolist.size() + 1) return;
  63.     memolist.remove(index - 1);
  64.   }
  65.  
  66.   private void help() {
  67.     System.out.println("list, add, del, upd, save, h, q");
  68.   }
  69.  
  70.   private void list() {
  71.     int i = 1;
  72.     for (String e: memolist)
  73.       System.out.println(i++ + ": " + e);
  74.   }
  75.  
  76.   private void save() {
  77.     Writer out = null;
  78.     try {
  79.       out = new FileWriter(file);
  80.     }
  81.     catch (IOException e) {
  82.       System.err.println(file + "can't open");
  83.       System.exit(1);
  84.     }
  85.     for (String e: memolist) {
  86.       try {
  87.         out.write(e + "\n");
  88.       }
  89.       catch (IOException e1) {
  90.         System.err.println(file + "can't write");
  91.         System.exit(1);
  92.       }
  93.     }
  94.     try {
  95.       out.close();
  96.     }
  97.     catch (IOException e1) {
  98.       e1.printStackTrace();
  99.     }
  100.   }
  101.  
  102.   private void update() {
  103.     System.out.print("> 番号: ");
  104.     int index = Integer.parseInt(in.nextLine());
  105.     if (index < 1 || index > memolist.size() + 1) return;
  106.     memolist.remove(index - 1);
  107.     System.out.print("> メモ: ");
  108.     String new_memo = in.nextLine();
  109.     memolist.add(index - 1, new_memo);
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement