Does this contain Polymorphic references? If not, how could it be implemented?

◇◆丶佛笑我妖孽 提交于 2021-02-11 14:19:03

问题


I am not too familiar with polymorphism, and was wondering if I have it used in my code?

If this doesn't contain a polymorphic reference, could you lead me in a direction of where I would need to go? The files that the program is using are not included, as I am mainly curious about whether or not any polymorphic references are used.

java file 1 - this file runs the program

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 file 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.");
        }
   }
}

来源:https://stackoverflow.com/questions/65227693/does-this-contain-polymorphic-references-if-not-how-could-it-be-implemented

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!