Hi, i saw an exercise and i made a solution. I’d like your opinion about this :
We have a list of prices of products bought by a customer. We want to make a discount on the highest price. Code a function returning an int, taking as parameters the discount applied to the most expensive article and an array of int representing the prices of the articles.
The account applied to the most expensive article is an int (0<=accountHighestPrice<=100)
This function must return the total price sold to the customer.
Here’s my solution :
Do you have any critics ?
import java.util.*;
public class Exercise {
public static int calculPrice(int[] prices,int discount){
//Determine the highest price
int priceMax = prices[0];
for(int k=1;k<prices.length;k++)
{
if(priceMax < prices[k])
{
priceMax = prices[k];
}
}
//Determine the price sold to the client
int cumul = 0
for(int i = 0;i<prices.length;i++)
{
if(prices[i] == priceMax)
{
prices[i] *= (discount/100);
}
cumul+=prices[i];
}
// Return the price
return cumul;
}
public void main() {
String line;
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()) {
line = sc.nextLine();
/* Lisez les données et effectuez votre traitement */
}
/* Vous pouvez aussi effectuer votre traitement une fois que vous avez lu toutes les données.*/
}
}