2017-09-20 49 views
0

我已经创建了三个方法readLong,readInt和readDouble,它们基本上做了同样的事情。唯一不同的是扫描仪调用的方法。如何通过将所有代码全部转换为一种方法来减少重复代码?当从控制台读取不同类型的数字时,减少重复代码

public long readLong(String description) 
    { 
     System.out.println(description); 
     long nrToReturn = 0; 
     boolean acceptedValue = false; 

     do { 
      System.out.println(); 
      System.out.print("Choose one: "); 
      try 
      { 
       nrToReturn = consoleScanner.nextLong(); //Only line thats different except return value 
       acceptedValue = true; 
      }catch(Exception e) 
      { 
       acceptedValue = false; 
       consoleScanner.nextLine(); 
      } 
     }while (!acceptedValue); 

     consoleScanner.nextLine(); 
     return nrToReturn; 
    } 
+0

发表于[代码审查]这个问题( https://codereview.stackexchange.com/)。 –

+0

看看Java中的泛型类型 – Michu93

+0

这些方法的调用者如何知道要调用哪一个? –

回答

2

在这里,我们一起去一个想法:

import java.util.Scanner; 

public class ScannerTest { 

    private Scanner consoleScanner; 

    public ScannerTest() { 
     consoleScanner = new Scanner(System.in); 
    } 

    @SuppressWarnings("unchecked")  
    private <T extends Number> T readType(String description, Class<T> desiredType) { 
     System.out.println(description); 
     Number result = null; 

     while (result == null) { 
      System.out.println(); 
      System.out.print("Choose one: "); 
      try { 
       if (Integer.class.equals(desiredType)) { 
        result = new Integer(consoleScanner.nextInt()); 
       } else if (Long.class.equals(desiredType)) { 
        result = new Long(consoleScanner.nextLong()); 
       } 
      } catch(Exception e) { 
       consoleScanner.nextLine(); 
      } 
     } 

     consoleScanner.nextLine(); 
     return (T) result; 
    } 

    public long readLong(String description) { 
     return this.readType(description, Long.class); 
    } 

    public int readInt(String description) { 
     return this.readType(description, Integer.class); 
    } 

    public static void main(String[] args) { 
     ScannerTest t = new ScannerTest(); 
     t.readLong("Reading a long value..."); 
     t.readInt("Reading an integer value..."); 
    } 
} 

更新,以下@ Michu93想法单一透明的方法:

import java.util.Scanner; 

public class ScannerTest { 

    private Scanner consoleScanner; 

    public ScannerTest() { 
     consoleScanner = new Scanner(System.in); 
    } 

    @SuppressWarnings("unchecked") 
    public <T extends Number> T readNumber(String description) { 
     System.out.println(description); 
     Number result = null; 

     while (result == null) { 
      System.out.print("\nChoose one: "); 
      String textRead = consoleScanner.next(); 

      try { 
       result = new Integer(textRead); 
      } catch(Exception e1) { 
       try { 
        result = new Long(textRead); 
       } catch (Exception e2) { 
        try { 
         result = new Double(textRead); 
        } catch (Exception e3) { 
        } 
       } 
      } 
      consoleScanner.nextLine(); 
     } 

     return (T) result; 
    } 

    public static void main(String[] args) { 
     ScannerTest t = new ScannerTest(); 
     for (int i = 0; i < 3; i++) { 
      Number input = t.readNumber(i + ": Reading int, long or double..."); 
      System.out.println("Input class: " + input.getClass().getCanonicalName()); 
      System.out.println("Input value: " + input); 
     } 
    } 
} 
+0

编辑:我正在考虑使用泛型而不是Object,因为@ Michu93指出。但我不认为有可能在不通知需要哪种类型的输入的情况下创建这样的公共方法[public static T readNumber(String description)],除非您定义类型之间的层次结构并尝试读取每个每次调用方法时都键入直到成功。更新我的答案以适应的想法。 [不能评论原来的线程,因为我还不是50分,对不起]。 –