Least offer Maya buys “N” no of products from a shop. The shop offers a different percentage of discount on each item. She wants to know the item that has the minimum discount offer, so that she can avoid buying that and save money.
[Input Format: The first input refers to the no of items; the second input is the item name, price and discount percentage separated by comma(,)]
Assume the minimum discount offer is in the form of Integer.
Note: There can be more than one product with a minimum discount.
Sample Input 1:
4
mobile,10000,20
shoe,5000,10
watch,6000,15
laptop,35000,5
Sample Output 1:
shoe
Explanation: the discount on the mobile is 2000, the discount on the shoe is 500, the discount on the watch is 900 and the discount on the laptop is 1750. So the discount on the shoe is the minimum.
Sample Input 2:
4
Mobile,5000,10
shoe,5000,10
WATCH,5000,10
Laptop,5000,10
Sample Output 2:
Mobile
shoe
WATCH
Laptop
Use a scanner and take the input as a string using a loop(as you want several lines consisting of strings and integers), further within the loop split the string to array and parse the string value containing numbers to integers. You could do something like this:
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int noOfItems = sc.nextInt();
String input="";
int price[] = new int[noOfItems];
int percentage[] = new int[noOfItems];
String itemName[] = new String[noOfItems];
String[] values;
for(int i=0;i<noOfItems;++i){
input = sc.nextLine();
values = input.split(",");
itemName[i] = values[0];
price[i] = Integer.parseInt(values[1]);
percentage[i] = Integer.parseInt(values[2]);
}
}
Assuming each input consists of an array of strings like the following:
String[] input = {"mobile,10000,20", "shoe,5000,10", "watch,6000,15", "laptop,35000,5"};
What you could do is loop through all strings and decide for each item if the discount is worse than the previous worst discount.
String worstItem = null;
double worstDiscount = 0;
for(String item : input) {
// Split the string in multiple strings
String[] splittedString = item.split(",");
String itemName = splittedString[0]; // First item is the items name
int price = Integer.parseInt(splittedString[1]); // The items price, parsed as an integer
int discount = Integer.parseInt(splittedString[2]);
// Calculate discount price
double discountPrice = price * (discount / 100);
// Check if the discount price is worse than the discount of a previous item
if(discountPrice < worstDiscount) {
worstDiscount = discountPrice;
worstItem = itemName;
}
}
System.out.println(worstItem + " has the worst discount");
Based on the needs, to get the multiple inputs in a single line. You can get them in this way. First, you need to get the number of inputs.
numberOfInputs = scanner.nextInt()
Once you get the number of inputs, you can then use the numberOfInputs and put them in a for loop and then loop through to get all the Strings. You can then utilize the strings and convert them into values using split.
for(int i=0; i< numberOfInputs; i++){
String inputLine = scanner.next();
String[] inputArray = inputLine.split(",");
}
I have also provided the answer based on the question. However, rather than using a map or array to deal with the storage of the data. I went ahead and created a product class to deal with the storage.
import java.util.Scanner;
class Product {
String name;
int price, discountPercentage, discountPrice;
public Product(String name, int price, int discountPercentage, int discountPrice) {
this.name = name;
this.price = price;
this.discountPercentage = discountPercentage;
this.discountPrice = discountPrice;
}
public String getName() {
return name;
}
public int getDiscountPrice() {
return discountPrice;
}
}
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 0, worstPrice = 0;
n = sc.nextInt();
Product[] productItems = new Product[n]; //Product Array with the length of the input.
for (int i = 0; i < n; i++) {
String input = sc.next(); //Take the input from the user.
String[] inputArray = input.split(","); //Split the input based on comma.
String name = inputArray[0]; //The array item contains the name.
int price = Integer.parseInt(inputArray[1]); //The Second array item contains the price.
int discountPercent = Integer.parseInt(inputArray[2]); //The Third array contains the discount percentage.
int discountPrice = (price * discountPercent) / 100; //Based on the price & discount percentage, get the discount price.
// Create a product object using the values and assign it to the product items array.
productItems[i] = new Product(name, price, discountPercent, discountPrice);
// The below statement, will get us the lowest discount price.
if (i == 0 || discountPrice < worstPrice) worstPrice = discountPrice;
}
// Print all the product items, which have the worst price in the provided input.
for (Product productItem : productItems) {
if (productItem.getDiscountPrice() == worstPrice) {
System.out.println(productItem.getName());
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.