如何增加id并在java中进行冗长的描述

时间:2017-08-27 15:56:28

标签: java arrays arraylist collections

我已经制作了一个Note类,我希望增加id。它没有增加。而且我正在从控制台获取输入描述。如何在用户的java中接受像("这是Hello World")这样冗长的描述。请帮助。

  public class Note   {

  private String title;
  private static int id;
  private static int count = 0;
  private String description;



 public static int getId() {
    return id = ++count;
 }

 public String getTitle() {
       id++;
    return title;
 }

 public String getDescription(){
    return description;
 }

 public void setDescription(String description){
    this.description=description;
 }
 public String toString() {

    return ("Id : " + id + "\n Title :" + title + "\n Description :"+       description);
}

  }

注意接受用户输入的控制台类。它接受1.添加注释我希望从用户接受适当的描述。第二个视图注意在toString方法的帮助下我打印输出。第三是退出

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



      public class NoteConsole {

    public static void NoteConsole() {
      final int ADD_NOTE = 1;
    final int VIEW_NOTE = 2;
        final int EXIT = 3;
    boolean loop = false;

    NoteConsole nc = new NoteConsole();
    Note note = new Note();
    ArrayList<Note> notes = new ArrayList<Note>();
    NoteServiceSerialize service1 = new NoteServiceSerialize();

    System.out.println("Write to the Console 1.AddNote, 2. ToView        3. Exit");

    Scanner in = new Scanner(System.in);
    int choice = in.nextInt();

    while (!loop) {
        switch (choice) {
        case ADD_NOTE: {

            System.out.println("Enter the title");
            String title = in.next();
            note.setTitle(title);
            System.out.println("Enter the description");
            String description = in.next();
            note.setDescription(description);
            notes.add(note);
            service1.noteSerialize(notes);
            break;

            }

        case VIEW_NOTE: {
            for (Note note1 : notes) {
                System.out.println(note1);
            }
            break;
            }

        case EXIT: {
            //code
            }
        }

        }

    }


public static void main(String[] args) {
    NoteConsole();
}

}

NoteServiceSerialize class -

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;



  public class NoteServiceSerialize {


 public void noteSerialize (ArrayList<Note> list){

try{
FileOutputStream file =              new                                                                                  FileOutputStream("D:\\serializable_notes.txt");
 ObjectOutputStream obj = new ObjectOutputStream(file);
 obj.writeObject(list);
 file.close();
  obj.close();
  }
   catch(Exception e){
    e.printStackTrace();
  }


 }

}

3 个答案:

答案 0 :(得分:1)

在类的构造函数中创建id实例变量并增加count,并将当前值count分配给id

public class Note   {

     private String title;
     private int id;
     private static int count = 0;
     private String description;

     public Note(){
          count++;
          this.id = count;
     }

     public int getId() {
        return id;
     }

     public String getTitle() {
        return title;
     }
     public void setTitle(String title) {
        this.title = title;
     }
     public String getDescription(){
        return description;
     }

     public void setDescription(String description){
        this.description=description;
     }
     public String toString() {
        return ("Id : " + id + "\n Title :" + title + "\n Description :"+ description);
    }
}

现在每个Note对象都有单独的id

public class NoteConsole {
//NoteConsole() is not constructor, should avoid same method name and class name 
public static void NoteConsole() {
    final int ADD_NOTE = 1;
    final int VIEW_NOTE = 2;
    final int EXIT = 3;
    boolean loop = true;

    //NoteConsole nc = new NoteConsole();
    Note note = new Note();
    ArrayList<Note> notes = new ArrayList<Note>();
    //NoteServiceSerialize service1 = new NoteServiceSerialize();



    Scanner in = new Scanner(System.in);

    while (loop) {
        System.out.println("Write to the Console 1.AddNote, 2. ToView        3. Exit");
        int choice = in.nextInt();
        in.nextLine();
        switch (choice) {
            case ADD_NOTE: {

                 System.out.println("Enter the title");
                 //in.nextLine() will read complete line.
                 String title = in.nextLine();
                 note.setTitle(title);
                 System.out.println("Enter the description");
                 String description = in.nextLine();
                 note.setDescription(description);
                 notes.add(note);
                 //service1.noteSerialize(notes);
                 break;

            }

            case VIEW_NOTE: {
                for (Note note1 : notes) {
                    System.out.println(note1);
                }
                break;
            }

            case EXIT: {
                loop = false;
            }
        }

    }

    in.close();
}


public static void main(String[] args) {
    NoteConsole();  
}

答案 1 :(得分:1)

首先你应该提供NoteServiceSerialize课程。 对于您的描述问题,您应该使用in.nextLine()。 关于增量id,在getTitle()中执行此操作是一种不好的做法。 这种方式更好的选择:

private static int lastId;
private int id = nextId();

private static int nextId() {
    lastId = ++lastId;
    return lastId;
}

,您的课程也有其他一些问题,请将其替换为:

public class Note {

private String title;
private String description;
private static int lastId;
private int id = nextId();

private static int nextId() {
    lastId = ++lastId;
    return lastId;
}

public static int getLastId() {
    return lastId;
}

public String getTitle() {
    return title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}
public void setTitle(String description) {
    this.description = description;
}


@Override
public String toString() {

    return ("Id : " + id + "\nTitle :" + title + "\nDescription :" + description);
}

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

public class NoteConsole {

final static ArrayList<Note> notes = new ArrayList<Note>();

public static void NoteConsole() {
    NoteServiceSerialize service1 = new NoteServiceSerialize();
    NoteConsole nc = new NoteConsole();
    Note note = new Note();
    final int ADD_NOTE = 1;
    final int VIEW_NOTE = 2;
    final int EXIT = 3;
    boolean loop = false;

    Scanner in = new Scanner(System.in);

    while (!loop) {
        System.out.println("Write to the Console 1.AddNote  2. ToView  3. Exit");
        int choice = in.nextInt();
        switch (choice) {
            case ADD_NOTE:
                System.out.println("Enter the title :");
                String title = in.next();
                System.out.println("Enter the description :");
                //if you had a problem with description text remove in.next()
                in.next();
                String description = in.nextLine();
                note.setTitle(title);
                note.setDescription(description);
                notes.add(note);
                service1.noteSerialize(notes);
                System.out.println("----------------------");
                break;
            case VIEW_NOTE:
                for (Note note1 : notes) {
                    System.out.println(note1);
                    System.out.println("----------------------");
                }
                break;

            case EXIT:
                loop = !loop;
                break;
            default:
                System.out.println("Not a Valid Option !!!");
        }
        System.out.println();
    }

}

     public static void main(String[] args) {
         NoteConsole();
     }

}

答案 2 :(得分:0)

id将保持为0(基元的默认值),除非您调用其中一个方法:

public static int getId() {
   return id = ++count;
}

public String getTitle() {
   id++;
   return title;
}

您实际增加id的值。也许是这样的:

public String toString() {
    return ("Id : " + getId() + "\n Title :" + title + "\n Description :"+ description);
}

还要确保将count初始化为:

private static final int count = 0;