Home > front end >  Springboot- How to pass the score to an entity class
Springboot- How to pass the score to an entity class

Time:03-15

I am trying to create a program that collects the score at the end and assigns it a grade but it is assigning the grade for every score printed. In layman's terms the system should go through each method and deduct points based on whether the condition is met. Its for a website so I would need to display the scores so I need to use an entity class with getters and setters from what I have read. I am quite lost about how to go about this

public class Review {



    public static void main(String[] args) { //getWordLength() { // Checking word length. Less than 6 means reviewer can't weigh out positives and negatives
        // TODO Auto-generated method stub
         int ReviewScore = 30;
        
         String Review = "me I me, pizza Montreal";
         String[] words = Review.split("\\s ");
         
          System.out.println("Word Count is: " words.length);
           int wordlength = Integer.valueOf(words.length);
          
           
            if (wordlength< 6) { 
                 ReviewScore -=4; // deducts 4pts if review less than 6 words
                System.out.println("Score is "  ReviewScore);
                
            }
            verbCount( ReviewScore,Review );
            
    }
    
        public static  void verbCount (int ReviewScore, String Review) { //Count verbs 'I' or 'me'
    
        for (String s : Review.split ("\\s ") )  { // splits review into separate words
            
        
            if (s.contains("me" )){ // Checks for 'me' or 'I'
            
                
                    ReviewScore -= 1;
                    System.out.println("Score is "  ReviewScore);
                    // deducts by 2 pts  for each time 'I' is mentioned
                    }
                
                    if ( s.contains ("I")) {
                        ReviewScore -= 1;
                        System.out.println("Score is "  ReviewScore); //deducts by 2 pts  for each time 'me' is mentioned
            
                }
                    WordBucket (ReviewScore, s);
        }
                    
            
}
    
        public static void WordBucket ( int ReviewScore, String s) {
        for (String word : Analyser.FREQWORDS) {
            if(s.contains(word)) {
                System.out.println("Score is "  ReviewScore);
                break;
                }
            else { 
                
                ReviewScore -=5;
                System.out.println("Score is "  ReviewScore);
                break;
            }
            
            
}
        

                Grade (ReviewScore) ;                               
}
        

        public static void Grade (int ReviewScore) {
             int Finalscore= ReviewScore;
                
                
             
         if (Finalscore >=20 && Finalscore <=25) { 
                
                 System.out.println ("a");
                     
             } else if (Finalscore >=14 && Finalscore <=19) {
             
                 System.out.println ("b");
             } else if (Finalscore >=7 && Finalscore <=13 ) {
                
                 System.out.println ("c"); 
            }  else if ( Finalscore <6)
                 System.out.println ("d");
            else {
                 System.out.println ("an error has occured")  ;
            }
            } 
            
            
        
  }

CodePudding user response:

I Believe this is the solution for your query. I understand that you want to print/pass the final Score as well as grade to entity class so in this way we can do where

a) instead of passing each and every word to subsequent function, you are passing the String array itself. b) returning the score value from each functional call so that the corresponding scores can be saved and passed to other function call.

Also try to avoid using system Print out since it is just for printing to console purpose. You need to return the value from each and every call. If you run this program , the output you are going to get is below

Word Count is: 5 Final Score is 18 Final Grade is b

the lines below

int finalScore = verbCount(ReviewScore, words);

String finalGrade = Grade(finalScore);

Can help you pass the finalScore and finalGrade to entity class which needs to consume these values.

Please note I am using the String array ArrayAnlyser here which I believe you are using as an enum as a part of your program.

public class Review {

public static void main(String[] args) {
    int ReviewScore = 30;
    String Review = "me I me, pizza Montreal";
    String[] words = Review.split("\\s ");
    System.out.println("Word Count is: "   words.length);
    int wordlength = Integer.valueOf(words.length);
    if (wordlength < 6) {
        ReviewScore -= 4;
    }
    int finalScore = verbCount(ReviewScore, words);
    System.out.println("Final Score is "   finalScore);
    String finalGrade = Grade(finalScore);
    System.out.println("Final Grade is "   finalGrade);
}
public static int verbCount(int ReviewScore, String[] words) { //Count verbs 'I' or 'me'
    for (String s : words) { // splits review into separate words
        if (s.contains("me")) { // Checks for 'me' or 'I'
            ReviewScore -= 1;
            //  System.out.println("Score is "  ReviewScore);
            // deducts by 2 pts  for each time 'I' is mentioned
        }
        if (s.contains("I")) {
            ReviewScore -= 1;
            // System.out.println("Score is "  ReviewScore); //deducts by 2 pts  for each time 'me' is mentioned
        }
    }
    int RevisedScore = WordBucket(ReviewScore, words);
    return RevisedScore;
}

public static int WordBucket(int ReviewScore, String[] words) {
    String[] ArrayAnlyser = {"me", "I", "pizza", "Montreal"};
    boolean flag = false;
    for (String word : words) {
        flag = false;
        for (String analyWord : ArrayAnlyser) {
            if (analyWord.contains(word)) {
                //  System.out.println("Score is "  ReviewScore);
                flag = true;
                break;
            }
        }
        if (!flag) {
            ReviewScore -= 5;
            // System.out.println("Score is "  ReviewScore);
        }
    }
    return ReviewScore;
    // Grade (ReviewScore) ;
}

public static String Grade(int ReviewScore) {
    int Finalscore = ReviewScore;
    String grade = "";
    if (Finalscore >= 20 && Finalscore <= 25) {
        //   System.out.println ("a");
        grade = "a";
    } else if (Finalscore >= 14 && Finalscore <= 19) {
        grade = "b";
        //     System.out.println ("b");
    } else if (Finalscore >= 7 && Finalscore <= 13) {
        grade = "c";
        //     System.out.println ("c");
    } else if (Finalscore < 6) {
        grade = "d";
        //    System.out.println ("d");
    } else {
        grade = "error occured";
        //    System.out.println ("an error has occured")  ;
    }
    return grade;
}

}

  • Related