q1 and q2 half - #4
Conversation
| // static TreeMap<String,User> tm = new TreeMap<String,User>(); | ||
| // static TreeMap<String, ArrayList<User>> tm = new TreeMap<String, ArrayList<User>>(); | ||
| // static ArrayList<Integer> roll=new ArrayList<>(); | ||
| public static User getUserInput() { |
There was a problem hiding this comment.
Suggest breaking a large function that does too many things into many smaller functions, each doing one thing.
Example: Instead of using getUserInput() where all parameters are input, the function can be divided into inputName() inputAge() inputAddress() inputCourses()
Have a standard way of structuring the verification - example: ideally for Every parameter, there is verification.
So restructuring this function
getUserInput()
-> getName() which internally calls validateNameInput()
-> getCourses() which internally calls validateCoursesInput()
etc.
And a good naming convention will improve Quality of Code and Maintainability.
So each of the above functions should have their own function for Validation. Abstract the utility of functions from each other.
There was a problem hiding this comment.
Here's an example of a more structured approach implementing the above idea.
// Global Variable Messages are a good idea.
final private static String GET_NAME_MESSAGE = "Enter The Username"
User getName(){
System.out.println(GET_NAME_MESSAGE);
input = scan.nextLine();
if (validateNameInput(input)){
user.setName(name) ;
}
else {
// ERROR LOGIC
}
}
validateNameInput(){
// VALIDATION CODE
}
| System.out.println("Select an option - \n1.Add user\n2.Display user\n3.Delete user\n4.Save user\n5.Exit"); | ||
| int t=sc.nextInt(); | ||
| switch(t) { | ||
| case 1: |
There was a problem hiding this comment.
Using Braces for blocks of code that belong together and Correct Indentation are nice for maintainability even if not required by the language.
| } | ||
|
|
||
| public static void displayUser() { | ||
| List<User> l = dao.findAllUsers(); |
There was a problem hiding this comment.
always use fully self-explanatory variable names
| public class Main extends InventoryUtil{ | ||
| public static void main(String args[]) { | ||
| //calls utility function to start program | ||
| start(); |
There was a problem hiding this comment.
Use OOPs more effectively here -> Create Objects of Classes and call functions as functions of these Objects, instead of using Functions Directly. This allows you to fully utilise the advantages of OOPs. Understand the value of using Singleton pattern cases as well.
| //after taking input if item type is wrong then we need to again take input | ||
| //that's why above implementation is correct | ||
| //public Item calculateTax(Item item) throws InvalidItemException{ | ||
| public Item calculateTax(Item item){ |
There was a problem hiding this comment.
Instead of sending the Entire Object as parameter, attempt to use the most primitive possible data types that makes sense to have to reduce the complexity of your functions.
Example: Here we use calculateTax(Item item) { String type=item.getType(); }
Instead, we can use calculateTax(String type) { }
The first method passes an entire object. Ex calling statement: calculateTax(item)
The second only passes the String. Ex calling statement:calculateTax(item.getType())
Many such similar Optimizations can be found.
This also implies that the function is at the most fundamental level in terms of solving a problem.
| Item item=null; | ||
| String ch="y"; | ||
| //variable to get out of the loop | ||
| int flag2=0; |
There was a problem hiding this comment.
Use better variable names that are self documenting so it's clear why they are being used
| @@ -0,0 +1,47 @@ | |||
| package inventory.model; | |||
|
|
|||
| public class Item { | |||
There was a problem hiding this comment.
Use of explicit Getters, Setters, Default Values are a good idea.
| public static Item getItems() { | ||
| Item item=new Item(); | ||
|
|
||
| System.out.println("Enter item details : "); |
There was a problem hiding this comment.
Learn to use Command Line Arguments for Console App -> Learn about Shell Input to Java Application
No description provided.