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:

AmountDiscount
Upto ₹1000No Discount
₹1001 to ₹50002% if Male & 5% if Female
₹5001 to ₹100005% if Male & 7% if Female
Above ₹100007% if Male & 10% if Female

Java

Java Classes

3 Likes

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

Answered By

2 Likes


Related Questions