In: Computer Science
A certain store gives its customers a discount on the goods they buy based on a peculiar manner.
The discount percentage is equal to each customer's remainder of weight to age, weight % age.
The maximum discount percentage allowed is 10.
If the remainder is greater than 10, the discount percentage is recomputed as the remainder of 10.
For example, a customer whose weight is 175 lbs and age 61, buys an item costing $60.97. The discount percentage = 175 % 61 = 53. As 53 is greater than 10, the discount percentage is recomputed as 53 % 10 = 3 percent.
Therefore, the discount amount is 0.03(60.97) = $1.83.
Write a class file called lastname_discount.java. Write methods to compute and return the discount percentage, the discount amount for each customer and the sum of all the discount amounts.
Write a demo file called lastname_discount_demo.java. Add two integer arrays containing the weight and age of customers and a double array containing the cost of goods (any number of customers greater than 10). Write an object to pass the three arrays into the class file and print the information returned by the three methods specified in the previous paragraph.
Extend the story line as you like (any reasonable) by clearly defining what it is. Add a fourth method (type your creation as a comment above the fourth method). Print the results of this fourth method below the first three outputs.
Short Summary:
Source Code:
lastname_discount.java File:
public class lastname_discount {
private int[] weights;
private int[] ages;
private double[] costs;
private double[] discounts;
/**
* @param weights
* @param ages
* @param costs
*/
public lastname_discount(int[] weights, int[] ages,
double[] costs) {
this.weights = weights;
this.ages = ages;
this.costs = costs;
this.discounts = new
double[costs.length];
calculateDiscounts();
}
/**
* Calculate discounts for each customer and store in a
double array
*/
private void calculateDiscounts() {
for(int index = 0; index <
weights.length; index++) {
int remainder =
(weights[index] % ages[index]) % 10;
discounts[index]
= remainder;
}
}
/**
* @param index - customer index
* @return discount percentage for the corresponding
customer
*/
public double getCustomerDiscountPercentage(int index)
{
return discounts[index];
}
/**
* @param index - customer index
* @return discount amount for the corresponding
customer
*/
public double getCustomerDiscountAmount(int index)
{
return costs[index] *
discounts[index] / 100;
}
/**
* @return sum of discounts of all customers
*/
public double sumOfAllDiscountsAmount() {
double sum = 0;
for(int index = 0; index <
discounts.length; index++) {
sum +=
discounts[index] * costs[index];
}
return sum;
}
/**
* Prints the customer details who gets highest
discount amount
*/
public void printHighDiscountCustomer() {
int highIndex = 0;
for(int index = 0; index <
discounts.length; index++) {
// if customer
gets high amount than previous highest
if
(getCustomerDiscountAmount(index) >
getCustomerDiscountAmount(highIndex)) {
highIndex = index;
}
}
// Print the customer details for
highIndex
System.out.println("Customer
Weight: " + weights[highIndex]
+ ", Age: " + ages[highIndex] + ", Cost of
goods: $" + costs[highIndex]
+ ", Discount Percentage: " +
getCustomerDiscountPercentage(highIndex) + "%"
+ ", Discount Amount: $ " +
getCustomerDiscountAmount(highIndex));
}
}
lastname_discount_demo.java
File:
public class lastname_discount_demo {
public static void main(String[] args) {
// Weights array
int[] weights = {175, 160, 190,
154, 100, 139, 123, 165, 138, 125};
// Ages array
int[] ages = {61, 45, 56, 30, 23,
45, 65, 25, 30, 34};
//Costs array
double[] costs = {60.97, 50.00, 56,
78.90, 56.78, 34.67, 67, 87.56, 56.89, 45.78 };
lastname_discount discounts = new
lastname_discount(weights, ages, costs);
//Print the details of
customer
System.out.printf("%10s %10s %10s
%20s %20s\n", "Weight", "Age", "Cost($)", "Discount(%)", "Discount
Amount($)");
for(int index = 0; index <
weights.length; index++) {
System.out.printf("%10d %10d %10.2f %20.2f %20.2f\n",
weights[index], ages[index],
costs[index],
discounts.getCustomerDiscountPercentage(index),
discounts.getCustomerDiscountAmount(index));
}
System.out.println("\n\nCustomer
details who gets more discount amount:");
discounts.printHighDiscountCustomer();
}
}
Sample Run:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************