OPA May8 Java

Create a class Player with below attributes:
playerId - int
skill - String
level - String 
points - int

 

Write getters, setters and parameterized constructor in the above mentioned attribute sequence as required. 

 

Create class Solution with the main method. 

Implement two static methods - findPointsForGivenSkill and getPlayerBasedOnLevel in Solution class.

 

findPointsForGivenSkill method:
This method will take two input parameters - array of Player objects and string parameter skill. 
The method will return the sum of the points attribute from player objects for the skill passed as parameter. 
If no player with the given skill is present in the array of player objects, then the method should return 0. 

 

getPlayerBasedOnLevel method:
This method will take two String parameters level and skill, along with the array of Player objects.
The method will return the player object, if the input String parameters matches with the level and skill attribute of the player object and its point attribute is greater than or equal to 20. If any of the conditions are not met, then the method should return null.

 

Note : No two player object would have the same value for skill and level attribute.
       All player object would have the points greater than 0.
       All the searches should be case insensitive.


These above mentioned static methods should be called from the main method. 

For findPointsForGivenSkill method - The main method should print the points as it is if the returned price is greater than 0, or it
should print "The given Skill is not available".

For getPlayerBasedOnLevel method - The main method should print the playerId of the returned player object. If the returned value is null
then it should print "No player is available with specified level, skill and eligibility points".

 

Before calling these static methods in main, use Scanner object to read the values of four Player objects referring attributes in the above mentioned attribute sequence. 
Next, read the value for skill and level.
Please consider the skill value read above as skill parameter for both the static methods.


Consider below sample input and output:
Input:

101
cricket
Basic
20
102
cricket
Intermediate
25
111
football
Intermediate
50
113
baseball
Advanced
100
cricket
Intermediate

 

Output:
45
102

Solutions:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
   public static void main(String[] args)
    {
        //code to read values 
        //code to call required method
        //code to display the result
        Scanner sc = new Scanner(System.in);
        Player players[] = new Player[4];
        for(int i = 0; i<4; i++) {
            int playerId = sc.nextInt();
            sc.nextLine();
            String skill = sc.nextLine();
            String level = sc.nextLine();
            int points = sc.nextInt();
            sc.nextLine();
            players[i] = new Player(playerId, skill, level, points);
        }
        String newskill = sc.nextLine();
        String newlevel = sc.nextLine();
        int totalPoints = findPointsForGivenSkill(players, newskill);
        Player chosenOne = getPlayerBasedOnLevel(players, newskill, newlevel);
        if(totalPoints>0)
            System.out.println(totalPoints);
        else
            System.out.println("The given Skill is not available");
        if(chosenOne == null)
            System.out.println("No player is available with specified level, skill and eligibility points");
        else
            System.out.println(chosenOne.getPlayerId());
    }

    public static int findPointsForGivenSkill(Player[] players, String skill)
      {
          //method logic
          int points = 0;
          for(int i = 0; i<4; i++) {
              if(players[i].getSkill().equalsIgnoreCase(skill))
                points = points + players[i].getPoints();
          }
          return points;
      }

    public static Player getPlayerBasedOnLevel(Player[] players, String skill, String level)
      {
          //method logic
          for(int i=0;i<4; i++) {
              if(players[i].getSkill().equalsIgnoreCase(skill) && players[i].getLevel().equalsIgnoreCase(level)){
                  if(players[i].getPoints() >= 20)
                    return players[i];
              }
          }
          return null;
      }
}

class Player
{
    //code to build Player class`
    private int playerId;
    private String skill;
    private String level;
    private int points;
    public int getPlayerId() {
        return playerId;
    }
    public void setPlayerId(int playerId) {
        this.playerId = playerId;
    }
    public String getSkill() {
        return skill;
    }
    public void setSkill(String skill) {
        this.skill = skill;
    }
    public String getLevel() {
        return level;
    }
    public void setLevel(String level) {
        this.level = level;
    }
    public int getPoints() {
        return points;
    }
    public void setPoints(int points) {
        this.points = points;
    }
    public Player(int playerId, String skill, String level, int points) {
        this.playerId = playerId;
        this.skill = skill;
        this.level = level;
        this.points = points;
    }
}

 

OPA JULY 24 Python

Create a class FoodItem with the below attributes:


item_id of type Number
item_name of type String
item_category of type String
item_price of type Number

Create the init method which takes all parameters in the above sequence. The method should set the value of attributes to parameter values.

Create a method inside the class with the name provideDiscount. This method takes a Number value as argument which is the percentage by which the item price should be discounted and discounts the item price of the food item by the given percentage amount. Returns the updated price of the given item.
e.g. If the food item price is 100 and the given percentage is 10, then the updated price of the food item should be 90.


Create another class Restaurant with the below attributes:
restaurant_name of type String
fooditem_list of type List having Foodltem objects


Create the init method which takes all parameters in the above sequence. The method should set the value of attributes to parameter values inside the method.


Create another method inside the class with the name retrieveUpdatedPrice. The method takes the update percentage as first argument and item id as the second argument . The method should find the respective food item, whose item id is given as in the argument , discount the price as per the given percentage and return the respective Foodltem object.

If the food item with given item id is not found , then the method returns None . Note: In Python None means NULL Object , Accordingly default maiN will display the message ‘No Food item exists which matches the search criteria’ (excluding the quotes)
If the update discount percent (argument to this function) is less than equals to zero , then return the object as it is , means there would not be any changes to the price.
Note: Use the method provideDiscount defined in the Foodltem class to calculate the discount price of the food items .

Instructions to write the main section of the code:
a. You would require to write the main section completely, hence please follow the below instructions for the same.
b. You would require to write the main program which is inline to the “sample input description section” mentioned below and to read the data in the same sequence.
c. Create the respective objects(Foodltem and Restaurant) with the given sequence of arguments to fulfill the Unit_ method requirement defined in the respective classes referring to the below instructions. i. Create a Foodltem object after reading the data related to it and add the object to the list of Foodltem objects which will be provided to the Restaurant class.This point repeats for the number of Foodltem objects(considered in the first line of input) . ii. Create Restaurant object by passing the Restaurant name(you can hard-code any name you want)and List of Foodltem objects ( created and as mentioned in above point# c.i ) as the arguments.
d. Take two number values as input depicting the item_id and the discount percentage to be provided as arguments to the method retrieveUpdatedPrice. e. Call the method retrieveUpdatedPrice mentioned above from the main section.
e. Display the Item Name and the Item Price separated by a ‘-‘ from the returned Foodltem object. Please refer to the sample output for more clarity on the display format.
f. If None is returned by the method retrieveUpdatedPrice ,then display the message No Food item exists which matches the search criterialexcluding the quotes).

• Input Format For Custom Testing
The 1st input taken in the main section is the number of Foodltem objects to be added to the list of FoodItems.
The next set of inputs are the item id, item name, item category and item price for each food item taken one after other and is repeated for number of objects given in the first line of input
The last but one line of input refers the item id , whose price needs to be discounted with the given percentage , mentioned in the last line

Sample Input


4
11
Pulao
Indian
80
12
Bhel
Snacks
50
13
Pasta
Italian
160
14
Burrito
Mexican
140
12
20


Sample Output


Bhel – 40.0

class FoodItem:
    def __init__(self,i,name,category,price):
        self.item_id = i
        self.item_name = name
        self.item_category = category
        self.item_price = price
   
    def provideDiscount(self,p):
        if p>0:
            self.item_price-=(self.item_price*p/100)

class Restaurant:
    def __init__(self, name, list):
        self.restaurant_name = name
        self.fooditem_list = list

    def retrieveUpdatedPrice(self,i,p):
        for food in self.fooditem_list:
            if(food.item_id == i):
                food.provideDiscount(p)
                return food
        return None

if __name__=='__main__':
    number=int(input())
    fooditem_list=[]
    for _ in range(number):
        item_id=int(input())
        item_name=input()
        item_category=input()
        item_price=int(input())
        fooditem_list.append(FoodItem(item_id,item_name,item_category,item_price))
    Rest=Restaurant("name",fooditem_list)
    item_id=int(input())
    percentage=int(input())
    result=Rest.retrieveUpdatedPrice(item_id,percentage)
    if result==None:
        print("No Food item exists which matches the search criteria")
    else:
        print(result.item_name,result.item_price,sep=" - ")

 

©2018 by The real one. Proudly created with Wix.com