java—它是否包含多态引用?如果没有,如何实施?

dtcbnfnu  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(131)

我不太熟悉多态性,想知道我的代码中是否使用了它?
如果这不包含多态引用,你能告诉我需要去哪里吗?程序正在使用的文件不包括在内,因为我主要好奇是否使用了任何多态引用。
java文件1—此文件运行程序

import java.util.Scanner;

public class ADTDemo {

   ADTDictionary dictionary;

   public static void menu() {

       System.out.println("Welcome the Faculty Directory Program");
       System.out.println("  Use commands:");
       System.out.println("  list all");
       System.out.println("  list DEPT_NAME");
       System.out.println("  add DEPT_NAME, FIRST LAST");
       System.out.println("  remove DEPT_NAME, FIRST LAST");
       System.out.println("  exit");
   }

   public static void main(String[] args) {
       menu();
       String command;
       ADTDemo dictObj = new ADTDemo();
       dictObj.dictionary = new ADTDictionary();
       dictObj.dictionary.read();
       Scanner scanner = new Scanner(System.in);

       do {
           System.out.println("");

           System.out.print(">>");
           command = scanner.nextLine().trim();

           if (!command.equals("exit")) {
               dictObj.action(command);
           } else {
               dictObj.dictionary.saveEntries();
               System.out.println("Goodbye! Have a nice day!");
           }

       } while (!command.equalsIgnoreCase("exit"));
   }

   public void action(String command) {
       if (command.equalsIgnoreCase("LIST ALL")) {

           dictionary.listAll();
           return;
       }
       else if (command.toUpperCase().contains("LIST")) {
           if (command.length() == 4){
               System.out.println("Command needed.");
               return;
           }
           command = command.substring(5, command.length());
           dictionary.listDeptName(command);
           return;
       }
       else if (command.toUpperCase().contains("ADD")) {
           command = command.substring(4, command.length());
           dictionary.add(command);
           return;
       }
       else if (command.toUpperCase().contains("REMOVE")) {
           command = command.substring(6, command.length());
           dictionary.remove(command);
       }
   }
}

java文件2

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class ADTDictionary {
   Map<String, List<String>> adtDictionary;

   public void read() {
       try {
           File facultyFile = new File("faculty.txt");
           File departmentFile = new File("departments.txt");

           Scanner departmentScanner = new Scanner(departmentFile);
           Scanner facultyScanner = new Scanner(facultyFile);

           adtDictionary = new HashMap<String, List<String>>();

           while (departmentScanner.hasNextLine()) {
               String department = departmentScanner.nextLine().trim();
               adtDictionary.put(department, new ArrayList<String>());
           }

           while (facultyScanner.hasNextLine()) {
               String faculty = facultyScanner.nextLine();
               String[] values = faculty.split(",");
               adtDictionary.get(values[1].trim()).add(values[0]);
           }

       } catch (FileNotFoundException ex) {
            System.out.println("ERROR: File not found.");
       }
   }

   public void listAll() {
       for (String key : adtDictionary.keySet()) {
           for (String value : adtDictionary.get(key)) {
               System.out.println(value + ", " + key);
           }
       }
   }

   public void listDeptName(String department) {
       if (null != adtDictionary.get(department)) {
           for (String name : adtDictionary.get(department)) {
               System.out.println(name);
           }
       }
       else{
           System.out.println("Unknown entry made.");
       }
   }

   public void add(String value) {
       if(!value.contains(",")){
           System.out.println("Incorrect entry.");
           return;
       }
       String[] values = value.split(",");
       String dept = values[0].trim();
       String faculty = values[1].trim();

       String[] facName = faculty.split(" ");

       if (!(facName.length == 2)){
           System.out.println("Please only enter First and Last name of faculty member.");
           return;
       }

        if (!(null != adtDictionary.get(dept))) {
            if(adtDictionary.containsKey(dept.toUpperCase())){
                System.out.println("Incorrect departtment entry.");
                return;
            }
            else if (dept == dept.toUpperCase()){
                adtDictionary.put(dept, new ArrayList<String>());
            }
            else{
                System.out.println("Incorrect department entry.");
                return;
            }
        }

       for (String name : adtDictionary.get(dept)) {
           if (name.equalsIgnoreCase(faculty)) {
               System.out.println("Cannot add " + name + " to " + dept + " because they already exist there.");
               return;
           }
       }
       adtDictionary.get(dept).add(faculty);
       System.out.println("OK, added " + faculty);
   }

   public void remove(String value) {
       String[] values = value.split(",");
       String dept = values[0].trim();
       String faculty = values[1].trim();

       adtDictionary.get(dept).remove(faculty);
       System.out.println("OK, removed " + faculty + " from " + dept);
   }

   public void saveEntries(){
       try {
            File facultyFile = new File("faculty.txt");
            File departmentFile = new File("departments.txt");

            PrintWriter facWriter = new PrintWriter(facultyFile);
            PrintWriter deptWriter = new PrintWriter(departmentFile);

            for (Object s : adtDictionary.keySet()) {
                deptWriter.println(s);
            }
            deptWriter.close();

            for (String key : adtDictionary.keySet()) {
                for (String value : adtDictionary.get(key)) {
                    facWriter.println(value + ", " + key);
                }
            }
            facWriter.close();
        }
        catch (IOException ex){
                System.out.println("ERROR saving file.");
        }
   }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题