Computer Applications
A shopping center offers discounts to its customers based on the table given below. Write a Java class to compute the bill for the shopping center.
Data Members / Instance Variables:
- String n — To store the name of the customer
- char g — To store the gender of the customer. 'M' for male and 'F' for female.
- double amt — To store the amount of purchase
- double dis — To store the discount amount
- double bill — To store the bill payable by the customer
Member Functions:
- Default constructor to initialise data members
- input() — Function that accepts name, gender and amount purchased
- calc() — Function that computes the discount (as per table given below) and bill amount
- display() — Function that displays the data members
- main() — Function that creates an object of the class and calls the member functions.
Calculation of discount is as per the below table:
| Amount | Discount |
|---|---|
| Upto ₹1000 | No Discount |
| ₹1001 to ₹5000 | 2% if Male & 5% if Female |
| ₹5001 to ₹10000 | 5% if Male & 7% if Female |
| Above ₹10000 | 7% if Male & 10% if Female |
Answer
import java.util.Scanner;
public class KboatShoppingCenter
{
String n;
char g;
double amt;
double dis;
double bill;
public KboatShoppingCenter() {
n = "";
g = 0;
amt = 0;
dis = 0;
bill = 0;
}
public void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
n = in.nextLine();
System.out.print("Enter Gender(M/F): ");
g = in.next().charAt(0);
g = Character.toUpperCase(g);
System.out.print("Enter Purchase Amount: ");
amt = in.nextDouble();
}
public void calc() {
double dp = 0;
if (amt <= 1000) {
dp = 0;
}
else if (amt <= 5000) {
if (g == 'M') {
dp = 2;
}
else if (g == 'F') {
dp = 5;
}
}
else if (amt <= 10000) {
if (g == 'M') {
dp = 5;
}
else if (g == 'F') {
dp = 7;
}
}
else {
if (g == 'M') {
dp = 7;
}
else if (g == 'F') {
dp = 10;
}
}
dis = dp / 100 * amt;
bill = amt - dis;
}
public void display() {
System.out.println("Name: " + n);
System.out.println("Gender: " + g);
System.out.println("Purchase Amount: " + amt);
System.out.println("Discount: " + dis);
System.out.println("Bill: " + bill);
}
public static void main(String args[]) {
KboatShoppingCenter obj = new KboatShoppingCenter();
obj.input();
obj.calc();
obj.display();
}
}Output
Related Questions
DTDC a courier company charges for the courier based on the weight of the parcel. Define a class with the following specifications:
Class name: courier
Member variables:
name – name of the customer
weight – weight of the parcel in kilograms
address – address of the recipient
bill – amount to be paid
type – 'D'- domestic, 'I'- international
Member methods:
void accept ( ) — to accept the details using the methods of the Scanner class only.
void calculate ( ) — to calculate the bill as per the following criteria:
Weight in Kgs Rate per Kg First 5 Kgs Rs.800 Next 5 Kgs Rs.700 Above 10 Kgs Rs.500 An additional amount of Rs.1500 is charged if the type of the courier is I (International)
void print ( ) — To print the details
void main ( ) — to create an object of the class and invoke the methods
Design a class name ShowRoom with the following description:
Instance variables / Data members:
String name — To store the name of the customer
long mobno — To store the mobile number of the customer
double cost — To store the cost of the items purchased
double dis — To store the discount amount
double amount — To store the amount to be paid after discountMember methods:
ShowRoom() — default constructor to initialize data members
void input() — To input customer name, mobile number, cost
void calculate() — To calculate discount on the cost of purchased items, based on following criteriaCost Discount (in percentage) Less than or equal to ₹10000 5% More than ₹10000 and less than or equal to ₹20000 10% More than ₹20000 and less than or equal to ₹35000 15% More than ₹35000 20% void display() — To display customer name, mobile number, amount to be paid after discount.
Write a main method to create an object of the class and call the above member methods.