Second attempt. First question was incorrectly answered: There are multiple answ
ID: 3913371 • Letter: S
Question
Second attempt. First question was incorrectly answered:
There are multiple answers on chegg for this question but they all fal short - either missing sentinel, or calculate discount but do not calculate actual total or apply discount... A pet shop wants to give a discount to its clients if they buy one or more pets and at least five other items. The discount is equal to 20 percent of the cost of the other items, but not the pets. To solve the problem, implement a method: public static void discount(double[] prices, boolean[] isPet, int nItems) The method receives information about a particular sale. For the ith item, prices[i] is the price before any discount, and isPet[i] is true if the item is a pet. Write a program that prompts a cashier via a method to enter each price and then a Y for a pet or N for another item. Use a price of -1 as a sentinel. Save the inputs in an array. Call the discount()method, and display the discount. Must have: Good java style; JavaDoc comments and comments in the code.
Here is my code but this does not apply the sentinel -1 which I want fixed. Also I want the last line to read "Discount" and display discount only if discount was applied or shows total amount:
--------------------------
import java.util.Scanner;
public class petstore {
public static void discount(double[] prices, boolean[] isPet, int nItems){
if(nItems >= 5){
boolean pet = false;
for(int i = 0;i<isPet.length;i++){
if(isPet[i] == true){
pet = true;
break;
}
}
for(int i = 0;i<isPet.length;i++){
if(isPet[i] == false){
prices[i] = prices[i] * 0.8;
}
}
}
}
public static void main(String args[]) {
int n;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of items: ");
n = scanner.nextInt();
double price[] = new double[n];
boolean isPet[] = new boolean[n];
String temp;
for(int i = 0;i<n;i++){
System.out.println("Enter price of item"+(i+1)+": ");
price[i] = scanner.nextDouble();
System.out.print("Is this item is a pet? (y/n): ");
temp = scanner.next().toLowerCase();
if(temp.charAt(0) == 'y'){
isPet[i] = true;
}
else{
isPet[i] = false;
}
}
discount(price,isPet,n);
for(int i = 0;i<n;i++){
System.out.println(price[i]);
}
}
}
Explanation / Answer
import java.util.Scanner;
public class pet {
public static double discount(double[] prices, boolean[] isPet, int nItems){
double discount = 0.0; //this variable is taken to store discount;
if(nItems >= 5){
boolean pet = false;
for(int i = 0;i<nItems;i++){
if(isPet[i] == true){
pet = true;
break;
}
}
if(pet){ //This condition is imposed to ensure that atleast one pet is bought
for(int i = 0;i<nItems;i++){
if(isPet[i] == false){
discount += (prices[i]*0.2); //Calculation of discounts
prices[i] = prices[i] * 0.8;
}
}
}
}
return discount;
}
public static void main(String args[]) {
int n=0;
Scanner scanner = new Scanner(System.in);
//Considering that maximum number of items can be 100
double price[] = new double[100];
boolean isPet[] = new boolean[100];
String temp;
//This is how we apply the sentinel for price=-1 i.e if -1 is entered as a price no more items will be entered
while(true){
System.out.println("Enter price of item"+(n+1)+": ");
price[n] = scanner.nextDouble();
if(price[n]==-1.0)
break;
System.out.print("Is this item is a pet? (y/n): ");
temp = scanner.next().toLowerCase();
if(temp.charAt(0) == 'y'){
isPet[n] = true;
}
else{
isPet[n] = false;
}
n++;
}
double Discount = discount(price,isPet,n); //We call the function for knowing the discount
for(int i = 0;i<n;i++){
System.out.println(price[i]);
}
/*To fulfill the last requirement i.e. discount should be displayed only if it is calculated
* we check whether the computed discount is 0 if true, that means no discount is computed else
* we display the discount.
*/
System.out.println("Discount: "+Discount);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.