Answer :
Sure! Let's break down the solution step-by-step:
1. Read the Input Values
- Number of days within which the bill is paid.
- Type of customer ('D' for Dealer and 'R' for Retailer).
- Amount of purchase.
2. Determine the Discount Rate
- Identify the discount rate based on the number of days and the type of customer:
- If payment is within 30 days:
- Dealer: 15%
- Retailer: 10%
- If payment is between 31 to 45 days:
- Dealer: 10%
- Retailer: 5%
- If payment is more than 45 days:
- No discount for either Dealer or Retailer.
3. Calculate the Discount Amount
- Multiply the purchase amount by the discount rate.
4. Calculate the Net Amount
- Subtract the discount amount from the purchase amount.
5. Display the Result
Here’s how you can implement this in Java:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read the input values
System.out.println("Enter the number of days within which the bill is paid:");
int days = scanner.nextInt();
System.out.println("Enter the type of customer (D for Dealer, R for Retailer):");
char customerType = scanner.next().charAt(0);
System.out.println("Enter the amount of purchase:");
double purchaseAmount = scanner.nextDouble();
// Initialize the discount rate
double discountRate = 0.0;
// Determine the applicable discount rate
if (days <= 30) {
if (customerType == 'D') {
discountRate = 0.15; // 15% discount for Dealers
} else if (customerType == 'R') {
discountRate = 0.10; // 10% discount for Retailers
}
} else if (days <= 45) {
if (customerType == 'D') {
discountRate = 0.10; // 10% discount for Dealers
} else if (customerType == 'R') {
discountRate = 0.05; // 5% discount for Retailers
}
}
// Calculate the discount amount
double discountAmount = purchaseAmount * discountRate;
// Calculate the net amount to be paid
double netAmount = purchaseAmount - discountAmount;
// Display the net amount to be paid
System.out.printf("Net amount to be paid: %.2f%n", netAmount);
// Close the scanner
scanner.close();
}
}
```
### Explanation:
- First, we import the `Scanner` class for reading input.
- We then prompt the user to enter the days, customer type, and purchase amount and read these values.
- Depending on the input values, we identify the discount rate:
- For payments within 30 days, Dealers get a 15% discount and Retailers get a 10% discount.
- For payments between 31 and 45 days, Dealers get a 10% discount and Retailers get a 5% discount.
- For payments after 45 days, no discount is applied.
- We calculate the discount amount by multiplying the purchase amount by the discount rate.
- The net amount to be paid is calculated by subtracting the discount amount from the purchase amount.
- Finally, we print the net amount to be paid.
This solution systematically addresses the problem and covers all the tariff conditions provided.
1. Read the Input Values
- Number of days within which the bill is paid.
- Type of customer ('D' for Dealer and 'R' for Retailer).
- Amount of purchase.
2. Determine the Discount Rate
- Identify the discount rate based on the number of days and the type of customer:
- If payment is within 30 days:
- Dealer: 15%
- Retailer: 10%
- If payment is between 31 to 45 days:
- Dealer: 10%
- Retailer: 5%
- If payment is more than 45 days:
- No discount for either Dealer or Retailer.
3. Calculate the Discount Amount
- Multiply the purchase amount by the discount rate.
4. Calculate the Net Amount
- Subtract the discount amount from the purchase amount.
5. Display the Result
Here’s how you can implement this in Java:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read the input values
System.out.println("Enter the number of days within which the bill is paid:");
int days = scanner.nextInt();
System.out.println("Enter the type of customer (D for Dealer, R for Retailer):");
char customerType = scanner.next().charAt(0);
System.out.println("Enter the amount of purchase:");
double purchaseAmount = scanner.nextDouble();
// Initialize the discount rate
double discountRate = 0.0;
// Determine the applicable discount rate
if (days <= 30) {
if (customerType == 'D') {
discountRate = 0.15; // 15% discount for Dealers
} else if (customerType == 'R') {
discountRate = 0.10; // 10% discount for Retailers
}
} else if (days <= 45) {
if (customerType == 'D') {
discountRate = 0.10; // 10% discount for Dealers
} else if (customerType == 'R') {
discountRate = 0.05; // 5% discount for Retailers
}
}
// Calculate the discount amount
double discountAmount = purchaseAmount * discountRate;
// Calculate the net amount to be paid
double netAmount = purchaseAmount - discountAmount;
// Display the net amount to be paid
System.out.printf("Net amount to be paid: %.2f%n", netAmount);
// Close the scanner
scanner.close();
}
}
```
### Explanation:
- First, we import the `Scanner` class for reading input.
- We then prompt the user to enter the days, customer type, and purchase amount and read these values.
- Depending on the input values, we identify the discount rate:
- For payments within 30 days, Dealers get a 15% discount and Retailers get a 10% discount.
- For payments between 31 and 45 days, Dealers get a 10% discount and Retailers get a 5% discount.
- For payments after 45 days, no discount is applied.
- We calculate the discount amount by multiplying the purchase amount by the discount rate.
- The net amount to be paid is calculated by subtracting the discount amount from the purchase amount.
- Finally, we print the net amount to be paid.
This solution systematically addresses the problem and covers all the tariff conditions provided.