首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java编程项目

Java编程项目
EN

Stack Overflow用户
提问于 2020-03-05 11:08:29
回答 1查看 179关注 0票数 0

我正在从事一个大学项目,任务是使用扫描仪读取多个数据文件的适当数据。该项目涉及一个超类和几个子类。到目前为止,下面的方法工作得很好,并读取与一个名为Tool的类及其所有字段相对应的数据。但是,我最近添加了一个子类ElectricTool,它扩展了类Tool,还引入了两个新字段,它们需要以与前面相同的方式读取,但使用下面所示的相同方法。我试过很多东西,但我似乎想不出来。有什么建议吗?最好是尽可能的干净/简单的代码,我认为它需要一个读的声明,但我正在挣扎。方法如下:

代码语言:javascript
复制
public void readToolData()

{
    Frame myFrame = null;
    FileDialog fileBox = new FileDialog(myFrame,"Open", FileDialog.LOAD);
    fileBox.setVisible(true);
    String directoryPath = fileBox.getDirectory();
    String fileName = fileBox.getFile();

    File dataFile = new File(fileName);
    System.out.println(fileName +"  "+ directoryPath);
    Scanner scanner = null;
    try
    {
        scanner = new Scanner(dataFile);
    }
    catch (FileNotFoundException e)
    {
        System.out.println(e);
    }

    while( scanner.hasNextLine() )
    {
        String lineOfText = scanner.nextLine().trim().replaceAll("\\s+","");
        if(!lineOfText.isEmpty() && !lineOfText.matches("^//.*") && !lineOfText.substring(0,1).equals("["))
        {
            System.out.println(lineOfText);      
        }
        else{
            continue;
        }

        Scanner scanner2 = new Scanner(lineOfText).useDelimiter("\\s*,\\s*");
        while(scanner2.hasNext())
        {
            Tool tool = new Tool();
            tool.readData(scanner2);


            storeToolList(tool);
        }

    }
    scanner.close();
}

电动工具类

工具类

数据文件

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-05 11:52:59

代码语言:javascript
复制
  public void readToolData() {
    Frame myFrame = null
    FileDialog fileBox = new FileDialog(myFrame, "Open", FileDialog.LOAD);
    fileBox.setVisible(true);
    String directoryPath = fileBox.getDirectory();
    String fileName = fileBox.getFile();

    File dataFile = new File(directoryPath + fileName);
    System.out.println(fileName + "  " + directoryPath);
    Scanner scanner = null;
    try {
      scanner = new Scanner(dataFile);
    } catch (FileNotFoundException e) {
      System.out.println(e);
    }

    // Current tool type
    String toolType = null;
    while( scanner.hasNextLine() ) {
      String lineOfText = scanner.nextLine().trim();

      // Skip empty lines and commentaries
      if(lineOfText.isEmpty() || lineOfText.startsWith("//")) {
        continue;
      }

      if (lineOfText.startsWith("[")) {
        // Extract the tool type name
        String withoutBracket = lineOfText.substring(1);
        // Split by spaces and take the first word
        String[] words = withoutBracket.split(" ");
        toolType = words[0];
        System.out.println("Reading information about " + toolType);
        continue;
      }

      System.out.println(lineOfText);

      Scanner scanner2 = new Scanner(lineOfText).useDelimiter("\\s*,\\s*");
      Tool tool = null;
      if ("ElectricTool".equals(toolType)) {
        tool = new ElectricTool();
      }
      // In the future here will come more cases for different types, e.g.:
      // else if ("HandTool".equals(toolType)) {
      //    tool = new HandTool();
      // }
      if (tool != null) {
        tool.readData(scanner2);
        storeToolList(tool);
      }
    }
    scanner.close();
  }

删除scanner.skip行中的Tool.readData:

代码语言:javascript
复制
public class Tool {

  public void readData(Scanner scanner) {
   toolName = scanner.next(); 
   itemCode = scanner.next(); 
   timesBorrowed = scanner.nextInt(); 
   onLoan = scanner.nextBoolean(); 
   cost = scanner.nextInt(); 
   weight = scanner.nextInt(); 
   scanner.skip(".*");   // Remove this line  
  }

}

并在readTool中实现ElectricTool方法:

代码语言:javascript
复制
 @Override
 public void readData(Scanner scanner) {
   super.readData(scanner);
   rechargeable = scanner.nextBoolean();
   power = scanner.next(); // Or nextInt? what is the type of power field?
 }

若要打印有关应使用多态性的工具的信息,请执行以下操作。在printAllTools中修改Shop.java方法如下:

代码语言:javascript
复制
public void printAllTools() {
  System.out.println("Information");
  System.out.println("---------->");
  for (Tool t : toolList) {
    System.out.println("You have selected:\n");
    t.printDetails();
  }
}

现在,您在Tool.java中的方法Tool.java必须如下所示:

代码语言:javascript
复制
public void printDetails() {
  System.out.println("Tool name: " + toolName + "\n" +
    "Item code: " + itemCode + "\n" +
    "Times borrowed: " + timesBorrowed + "\n" +
    "On load: " + onLoan + "\n" + 
    "Cost: " + cost + "\n" +
    "Weight: " + weight + "g\n"
  );
}

在ElectricTool.java中:

代码语言:javascript
复制
public void printDetails() {
  super.printDetails();
  System.out.println("Rechargeable: " + rechargeable + "\n" +
   "Power: " + power + "\n"
  );
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60544007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档