从命令行运行控制台Java应用程序

时间:2018-10-29 09:25:40

标签: java model-view-controller command-line

我正在尝试从命令行启动一个应用程序。我想实现OOP的所有功能,特别是MVC设计模式。老实说,我从来没有做过这样的事情(我是交互式创建应用程序,还是使用JavaFX GUI创建应用程序)。我使用流API创建了Person类,分析了输入字符串。我创建了一个包含应该具有不同实现的方法的接口..但是,在我看来,我的方法不正确。请协助进一步发展。这是我今天要吃的东西:

模型类:

public class Person implements Serializable {
private static final long serialVersionUID = 9005793181629487010L;

private static AtomicInteger nextID = new AtomicInteger(0);

private int id;
private String name;
private String surname;
private String age;
private String address;

public Person(int id, String name, String surname, String age, String address) {
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.age = age;
    this.address = address;
}

public Person(String name, String surname, String age, String address) {
    this(nextID.getAndIncrement(), name, surname, age, address);
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public String getAge() {
    return age;
}

public void setAge(String age) {
    this.age = age;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

@Override
public String toString() {
    return "\n\nID: " + getId() + "\nName: " + getName() + "\nSurname: " + getSurname() + "\nAge: " + getAge() + "\nAddress: " + getAddress();
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Person person = (Person) o;
    return Objects.equals(name, person.name) &&
            Objects.equals(surname, person.surname) &&
            Objects.equals(age, person.age) &&
            Objects.equals(address, person.address);
}

@Override
public int hashCode() {
    return Objects.hash(name, surname, age, address);
}}

处理用于保存完整信息的文件的类:

public class DataFile {
private File file;
private List<Person> people = new ArrayList<>();

public DataFile(String fileName) {
    this.file = new File(fileName);
}

public void save(Person person) {
    try(BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) {
        writer.write(person.getName()+ "\r\n" + person.getSurname() + "\r\n" + person.getAge() + "\r\n" + person.getAddress() + "\r\n\r\n");
    } catch(IOException e) {
        throw new RuntimeException(e);
    }
    loadAll();
    writeToJson();
}

private void loadAll() {
    try(BufferedReader reader = new BufferedReader(new FileReader(file))) {
        String name;
        while((name = reader.readLine()) != null) {
            Person person = new Person(name, reader.readLine(), reader.readLine(), reader.readLine());
            people.add(person);
            reader.readLine();
        }
    }
    catch (IOException e) {
        System.out.println(e + "\n\nA new file 'db.txt' will be created.\n");
    }
}

private void writeToJson(){
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("data_file.json"))) {
        Gson gson = new Gson();
        bw.write(gson.toJson(people));
        bw.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}}

包含用于处理每个命令的方法的接口:

public interface Command {
void execute(Map<String, String> options);}

“添加到文件”命令的实现:

public class AddCommand implements Command {

@Override
public void execute(Map<String, String> params) {
    Person person = new Person(params.get("name"), params.get("surname"), params.get("age"), params.get("address"));
    System.out.println(person.toString() + "\n");
    params.forEach((k, v) -> System.out.printf("flag: %s - value: %s\n", k, v));
    DataFile file = new DataFile("db.txt");
    file.save(person);
}}

该程序的主要类别:

public class Main {

public static void main(String[] args) {
    //Arrays.stream(args).forEach(System.out::println);
    Map<String, String> params = new Main().parse(args);
    AddCommand add = new AddCommand();
    add.execute(params);
}

private Map<String, String> parse(String[] args) {
    Map<String, String> parameters = new LinkedHashMap<>();
    for (int i = 0; i < args.length; i += 2){
        if (i + 1 < args.length){
            parameters.put(args[i], args[i + 1]);
        }
    }
    return parameters;
}}

一切正常,但是,我不知道如何实现搜索替换,删除等操作。也许我是错误地对输入字符串进行了排序..而且我不明白我需要如何处理诸如添加,更新,查找,查看等命令。可能有必要使用“全局”和“本地”来解析字符串参数,即在界面中,设置要执行的方法参数(Map globalOptions,Map localOptions)……其中第一个Map是文件及其名称,第二个Map是名称,姓氏,年龄等。但是也许我错了。需要帮助!

0 个答案:

没有答案