Try to refactor below codes for computing score (underlined part) with extract methods
public class Customer
{
void foo()
{
int a, b, c, xfactor;
int score; Scanner myInput = new Scanner(System.in);
a = myInput.nextInt();
b = myInput.nextInt();
c = myInput.nextInt();
xfactor = myInput.nextInt();
System.out.println(“Computer score:”);
// Compute score score = a*b+c;
score *= xfactor;
}
}
import java.util.Scanner;
class Customer {
private int a, b, c, xfactor;
public void inputData() {
Scanner myInput = new Scanner(System.in);
System.out.print("Enter a: ");
a = myInput.nextInt();
System.out.print("Enter b: ");
b = myInput.nextInt();
System.out.print("Enter c: ");
c = myInput.nextInt();
System.out.print("Enter xfactor: ");
xfactor = myInput.nextInt();
}
public void displayScore() {
System.out.println("Computer score: " + computeScore());
}
private int computeScore() {
// Compute score = a*b+c;
int score = a * b + c;
score *= xfactor;
return score;
}
}
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Customer customer = new Customer();
customer.inputData();
customer.displayScore();
}
}
Comments
Leave a comment