I have a single class - App.java, with another class that I have imported from a library inside of my project - AnotherClass.java. The project is a very simple calculation tool that takes user input (I/O) and runs from file if one is given.
My question is to ask whether there is a better way to design/refactor this programme. I have read about using the Decorator pattern for I/O but not really sure where to begin. I'm sure there are tons of improvements that could be made to refactor this code. I am after some ideas of how this could be improved/easier to read.
/* App.java */
public class App {
public static void main(String[] args) throws Exception {
if (args.length == 1) {
String fileName = args[0];
String fileContent = new Scanner(new File(fileName))
.useDelimiter("\\Z").next();
ArrayList parsedContent = parseContentFromFileContent(fileContent);
LinkedHashMap outputMethod = renderOutput(parsedContent);
double subTotal = AnotherClass.calculate("one", "two", "three", "four", "five");
double total = calculateTwo(intOne, subTotal);
System.out.println("Total" + new DecimalFormat("#.00").format(calucationTwo));
return;
}
Scanner scan = new Scanner(System.in);
System.out.println("Enter the first value");
int intOne = scan.nextInt();
System.out.println("Enter the second value");
int intTwo = scan.nextInt();
System.out.println("Enter the third value");
int intThree = scan.nextInt();
System.out.println("Enter the fourth value");
int intFour = scan.nextInt();
System.out.println("Enter the fifth value");
int intFive = scan.nextInt();
double subTotal = AnotherClass.calculate(intOne, intTwo, intThree, intFour, intFive);
double total = calculateTwo(intOne, subTotal);
System.out.println("Total" + new DecimalFormat("#.00").format(calucationTwo));
}
public static double calculateTwo(int intOne, double subTotal) {
double newInt = subTotal;
if (intOne >= 1000) {
double calcuation = intOne * subTotal;
System.out.println("Total: " + calcuation);
newTotal = anotherCalculation(intOne, subTotal);
} else {
System.out.println("No calculation");
}
return newPrice;
}
public static double anotherCalculation(double intOne, double subTotal) {
return subtotal - (subTotal * intOne);
}
public static ArrayList parseCalculationFromFileContent(String fileContent) {
ArrayList parsedInts = new ArrayList();
String pattern = "([a-z]{2})(\\d+)";
Pattern p = Pattern.compile(pattern, Pattern.DOTALL);
Matcher m = p.matcher(fileContent);
if (m.matches()) {
switch (m.group(1)) {
case "ab":
parsedInts.add(1);
break;
case "cd":
parsedInts.add(2);
break;
case "ef":
parsedInts.add(3);
break;
}
parsedInts.add(Integer.parseInt(m.group(2)));
}
return parsedInts;
}
public static LinkedHashMap renderOutput(ArrayList parsedContent) {
LinkedHashMap renderedOutputMap = new LinkedHashMap();
String one = "one";
String two = "two";
String three = "three";
String four = "four";
String five = "five";
renderedOutputMap.put(one, parsedContent.get(0));
renderedOutputMap.put(two, parsedContent.get(1));
renderedOutputMap.put(three, parsedContent.get(2));
renderedOutputMap.put(four, parsedContent.get(3));
renderedOutputMap.put(five, parsedContent.get(4));
/* Display content using Iterator */
Set set = renderedOutputMap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.println(mentry.getKey() + ": " + mentry.getValue());
}
return renderedOutputMap;
}
}
/* AnotherClass.java */
public class AnotherClass {
public AnotherClass() {
}
public static double calculate(int var0, int var1, int var2, int var3, int var4) {
if(var0 >= 1 && var4 >= 10000000) {
int var5 = var0 + var1 + var2 + var3 + var4;
return var5 * var0;
} else {
throw new RuntimeException("Invalid calculation");
}
}
}
Aucun commentaire:
Enregistrer un commentaire