Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class MemoList {
- public static void main(String[] args) {
- MemoList memo =
- new MemoList(args.length == 1 ? args[0] : "memo");
- while (true) {
- System.out.print("> コマンドを入力してください: ");
- String command = memo.in.nextLine();
- if (command.equals("list")) memo.list();
- else if (command.equals("add")) memo.add();
- else if (command.equals("del")) memo.delete();
- else if (command.equals("upd")) memo.update();
- else if (command.equals("save")) memo.save();
- else if (command.equals("h")) memo.help();
- else if (command.equals("q")) break;
- else memo.help();
- }
- memo.in.close();
- }
- private final List<String> memolist;
- public Scanner in;
- private final File file;
- public MemoList(String file_name) {
- file = new File(file_name);
- Scanner fin = null;
- try {
- fin = new Scanner(file);
- }
- catch (FileNotFoundException e) {
- System.err.println("File Not Found");
- System.exit(1);
- }
- memolist = new ArrayList<String>();
- while (fin.hasNextLine())
- memolist.add(fin.nextLine());
- fin.close();
- in = new Scanner(System.in);
- }
- private void add() {
- if (memolist.size() >= 10) {
- System.err.println("メモがいっぱいです");
- return;
- }
- System.out.print("> メモ: ");
- String new_memo = in.nextLine();
- memolist.add(new_memo);
- }
- private void delete() {
- System.out.print("> 番号: ");
- int index = Integer.parseInt(in.nextLine());
- if (index < 1 || index > memolist.size() + 1) return;
- memolist.remove(index - 1);
- }
- private void help() {
- System.out.println("list, add, del, upd, save, h, q");
- }
- private void list() {
- int i = 1;
- for (String e: memolist)
- System.out.println(i++ + ": " + e);
- }
- private void save() {
- Writer out = null;
- try {
- out = new FileWriter(file);
- }
- catch (IOException e) {
- System.err.println(file + "can't open");
- System.exit(1);
- }
- for (String e: memolist) {
- try {
- out.write(e + "\n");
- }
- catch (IOException e1) {
- System.err.println(file + "can't write");
- System.exit(1);
- }
- }
- try {
- out.close();
- }
- catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- private void update() {
- System.out.print("> 番号: ");
- int index = Integer.parseInt(in.nextLine());
- if (index < 1 || index > memolist.size() + 1) return;
- memolist.remove(index - 1);
- System.out.print("> メモ: ");
- String new_memo = in.nextLine();
- memolist.add(index - 1, new_memo);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement