Home > Back-end >  Submit written lost after an interview
Submit written lost after an interview

Time:09-24

Before the interview I had written, I've already finished my test, and then their IT manager answered my bad code,
Just want to know do you have any Suggestions for improvement for my answer?



 public class Main {
Private static Scanner Scanner=new Scanner (System. In);
Private static HashMap ProductHashMap=new HashMap<> (a);

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 the relationship of B + "\ n"
+ 2 to Change the relationship of C \ n "
+ "3 - Premium of B \ n"
+ "4 - Premium of C \ n"
+ "Command:");

Try {
Int the command=scanner. NextInt ();
Scanner. NextLine ();

The 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;
}

} the catch (InputMismatchException e) {
System. The out. Println (" gave the command both Please try again. ");
Scanner. NextLine ();
ShowMenu ();
} the catch (a NumberFormatException e) {
System.out.println(e.getMessage());
ShowMenu ();
}

}
}

Private static void initialProducts (ProductType type) {
System. Out. Println (the String. Format (" both Please input values of % s, separated with Spaces. ", type));
String productInput=scanner. NextLine ();
String [] productArrays=productInput. Split (" ");
List ProductList=new ArrayList (a);
Try {
For (int id=0; Id & lt; ProductArrays. Length; Id++) {
The int value=https://bbs.csdn.net/topics/Integer.parseInt (productArrays [id]);

The Product the Product=new Product (type, id, value);
If (the hasParentRelationShip ()) {
AssignParent (product);
}

ProductList. Add (product);
}
ProductHashMap. Put (type, productList);
} the catch (a NumberFormatException e) {
System. The out. Println (" Invalid input. Both Please try again. ");
InitialProducts (type);
}
}

Private static void assignParent Product (Product) {
System. Out. Println (the String. Format (" Index for the parent [% s]=% d for % s is % s [?] : "of the product. The getType (), the product the getId (), the product the getValue (), the product the getType () getParentType ()));
ListAllOptions (product. GetType (). GetParentType ());

Try {
Int selectedProductId=scanner. NextInt ();
Scanner. NextLine ();

The Product the parent=getProduct (Product. GetType (). GetParentType (), selectedProductId);
If (product. The getParent ()!=null) {
Product. The getParent (). RemoveChildren (product);
}
The parent. AddChildren (product);

} the catch (InputMismatchException e) {
System. The out. Println (" Invalid input. Both Please try again. ");
Scanner. NextLine ();
AssignParent (product);
} the catch (a NumberFormatException e) {
System.out.println(e.getMessage());
AssignParent (product);
}

}

Private static void listAllOptions (ProductType type) {
List ProductList=productHashMap. Get (type);
for (int i=0; i System. Out. Println (the String. Format (" % d - % s ", I, productList. Get (I). ToDetailsString ()));
}
}

Private static void changeRelationship (ProductType type) {
System. Out.println (" Which the product you order like to reassign? ");
ListAllOptions (type);

Try {
Int selectedProductId=scanner. NextInt ();
Scanner. NextLine ();
AssignParent (getProduct (type, selectedProductId));
} the catch (InputMismatchException e) {
System. The out. Println (" Invalid input. Both Please try again. ");
Scanner. NextLine ();
ChangeRelationship (type);
} the catch (a NumberFormatException e) {
System.out.println(e.getMessage());
ChangeRelationship (type);
}

}

Private static Product getProduct (ProductType type, int id) {
Optional The product=productHashMap. Get (type). The stream () filter (p - & gt; P.g etId ()==id). FindFirst ();
If (product. The isPresent ()) {
Return the product. The get ();
} else {
Throw new a NumberFormatException (String format (" the Product % s (% d) not found. ", the type, id));
}
}

Private static void calculatePremium (ProductType type) {
System. Out.println (" Which product order you like to calculate? ");
ListAllOptions (type);

Try {
Int selectedProductId=scanner. NextInt ();
Scanner. NextLine ();
The Product the Product=getProduct (type, selectedProductId);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related