面試前我進行了筆試。 我已經完成測驗,然后他們的IT經理回答說我的代碼不好。
只想知道您對我的答案有什么改進建議嗎?

public class Main {
private static Scanner scanner = new Scanner(System.in);
private static HashMap<ProductType, List<Product>> productHashMap = new HashMap<>();
public static void main(String[] args) {
initialProducts(ProductType.A);
initialProducts(ProductType.B);
initialProducts(ProductType.C);
showMenu();
}
private static void showMenu() {
boolean mainLoop = true;
while (mainLoop) {
System.out.println(" Menu: \n "
+ "1 - Change relationship of B \n "
+ "2 - Change relationship of C \n "
+ "3 - Premium of B \n "
+ "4 - Premium of C \n "
+ "Command: ");
try {
int command = scanner.nextInt();
scanner.nextLine();
switch (command) {
case 1:
changeRelationship(ProductType.B);
break;
case 2:
changeRelationship(ProductType.C);
break;
case 3:
calculatePremium(ProductType.B);
break;
case 4:
calculatePremium(ProductType.C);
break;
default:
continue;
}
} catch (InputMismatchException e) {
System.out.println(" Invalid command. Please try again. ");
scanner.nextLine();
showMenu();
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
showMenu();
}
}
}
private static void initialProducts(ProductType type) {
System.out.println(String.format(" Please input values of %s, separated with spaces. ", type));
String productInput = scanner.nextLine();
String[] productArrays = productInput.split(" ");
List<Product> productList = new ArrayList<Product>();
try {
for (int id = 0; id < productArrays.length; id++) {
int value = Integer.parseInt(productArrays[id]);
Product product = new Product(type, id, value);
if (type.hasParentRelationShip()) {
assignParent(product);
}
productList.add(product);
}
productHashMap.put(type, productList);
} catch (NumberFormatException e) {
System.out.println(" Invalid input. Please try again. ");
initialProducts(type);
}
}
private static void assignParent(Product product) {
System.out.println(String.format(" Index for parent for %s[%s] = %d is %s[ ? ]: ", product.getType(), product.getId(), product.getValue(), product.getType().getParentType()));
listAllOptions(product.getType().getParentType());
try {
int selectedProductId = scanner.nextInt();
scanner.nextLine();
Product parent = getProduct(product.getType().getParentType(), selectedProductId);
if (product.getParent() != null) {
product.getParent().removeChildren(product);
}
parent.addChildren(product);
} catch (InputMismatchException e) {
System.out.println(" Invalid input. Please try again. ");
scanner.nextLine();
assignParent(product);
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
assignParent(product);
}
}
private static void listAllOptions(ProductType type) {
List<Product> productList = productHashMap.get(type);
for (int i = 0; i < productList.size(); i++) {
System.out.println(String.format(" %d - %s ", i, productList.get(i).toDetailsString()));
}
}
private static void changeRelationship(ProductType type) {
System.out.println(" Which product you would like to reassign? ");
listAllOptions(type);
try {
int selectedProductId = scanner.nextInt();
scanner.nextLine();
assignParent(getProduct(type, selectedProductId));
} catch (InputMismatchException e) {
System.out.println(" Invalid input. Please try again. ");
scanner.nextLine();
changeRelationship(type);
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
changeRelationship(type);
}
}
private static Product getProduct(ProductType type, int id) {
Optional<Product> product = productHashMap.get(type).stream().filter(p -> p.getId() == id).findFirst();
if (product.isPresent()) {
return product.get();
} else {
throw new NumberFormatException(String.format("Product %s[%d] not found.", type, id));
}
}
private static void calculatePremium(ProductType type) {
System.out.println(" Which product would you like to calculate? ");
listAllOptions(type);
try {
int selectedProductId = scanner.nextInt();
scanner.nextLine();
Product product = getProduct(type, selectedProductId);
System.out.println(String.format("\n Premium of %s[%d]: %f \n", product.getType(), product.getId(), product.getPremium()));
} catch (InputMismatchException e) {
System.out.println(" Invalid input. Please try again. ");
scanner.nextLine();
calculatePremium(type);
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
calculatePremium(type);
}
}
}public class Product {
private ProductType type;
private int id;
private int value;
private List<Product> children;
private Product parent;
public Product(ProductType type, int id, int value) {
this.type = type;
this.id = id;
this.value = value;
this.children = new ArrayList<Product>();
}
public Product getParent() {
return parent;
}
public void setParent(Product parent) {
this.parent = parent;
}
public boolean hasParent() {
return this.type.hasParentRelationShip() && this.parent != null;
}
public List<Product> getChildren() {
return children;
}
public void setChildren(List<Product> children) {
children.forEach(c -> setParent(this));
this.children = children;
}
public void addChildren(Product child) {
child.setParent(this);
this.children.remove(child);
this.children.add(child);
}
public void removeChildren(Product child) {
this.children.remove(child);
}
public ProductType getType() {
return type;
}
public void setType(ProductType type) {
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public String toString() {
return "Product{" +
"type=" + type +
", id=" + id +
", value="https://bbs.csdn.net/topics/ + value +
", children=" + children +
", parent=" + parent +
'}';
}
public String toDetailsString() {
if (this.hasParent()) {
return String.format("%s[%d] value = %d, parent = %s[%d] value = %s", this.type, this.id, this.value, this.getParent().getType(), this.getParent().getId(), this.getParent().getValue());
} else {
return String.format("%s[%d] value = %d, parent = null", this.type, this.id, this.value);
}
}
public Double getPremium() {
switch (this.getType()) {
case B:
return Double.valueOf((this.value - this.getParent().getValue()) / this.getParent().getValue());
case C:
return Double.valueOf((this.value - this.getParent().getParent().getValue()) / this.getParent().getParent().getValue());
default:
return 0.0;
}
}
}public enum ProductType {
A, B, C;
public ProductType getParentType() {
switch (this) {
case A:
return null;
case B:
return A;
case C:
return B;
default:
throw new IllegalArgumentException("Unknown product type.");
}
}
public boolean hasParentRelationShip() {
return this.getParentType() != null;
}
}
uj5u.com熱心網友回復:
還是先把注釋寫上吧,我看都看不下去uj5u.com熱心網友回復:
我隨便說說,你大概看一眼 你這個列舉用的。。。 可以用工廠加策略模式來寫主要 1 沒有拓展性,比如在來個D,E,F 對原來的依賴太強
2. 代碼有冗余,可以提煉一下 可維護性
3. 就是樓上說的注釋問題 可讀性,我都不知道你要干什么。。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/71461.html
標籤:Java相關
