KnowledgeBoat Logo
|

Computer Applications

Define a class named Customer with the following description:

Instance variables /Data members:

String cardName — Name of the card holder.

long cardNo — Card Number.

char cardType — Type of card, 'P' for Platinum, 'G' for Gold, 'S' for Silver.

double purchaseAmount — The amount of purchase done on card.

double cashback — The amount of cashback received.

Member methods:

Customer(String name, long num, char type, double amt) — Parameterised constructor to initialize all data members.

void compute() — To compute the cashback based on purchase amount as follows:

  1. For Silver ('S'), 2% of purchase amount
  2. For Gold ('G'), 5% of purchase amount
  3. For Platinum ('P'), 8% of purchase amount

void display() — To display the details in the below format:

Card Holder Name:
Card Number:
Purchase Amount:
Cashback:

Write a main method to input the card details from the user then create object of the class and call the member methods with the provided details.

Java

Java Constructors

15 Likes

Answer

import java.util.Scanner;

public class Customer
{
    String cardName;
    long cardNo;
    char cardType;
    double purchaseAmount;
    double cashback;

    public Customer(String name, 
                    long num, 
                    char type, 
                    double amt) {
        cardName = name;
        cardNo = num;
        cardType = type;
        purchaseAmount = amt;
        cashback = 0;
    }
    
    void compute() {
        switch (cardType) {
            case 'S':
            cashback = 2.0 * purchaseAmount / 100.0;
            break;
            
            case 'G':
            cashback = 5.0 * purchaseAmount / 100.0;
            break;
            
            case 'P':
            cashback = 8.0 * purchaseAmount / 100.0;
            break;
            
            default:
            System.out.println("INVALID CARD TYPE");
        }
    }
    
    void display() {
        System.out.println("Card Holder Name: " + cardName);
        System.out.println("Card Number: " + cardNo);
        System.out.println("Purchase Amount: " + purchaseAmount);
        System.out.println("Cashback: " + cashback);
    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter Card Holder Name:");
        String n = in.nextLine();
        System.out.println("Enter Card Number:");
        long no = in.nextLong();
        System.out.println("Enter Card Type:");
        char t = in.next().charAt(0);
        System.out.println("Enter Purchase Amount:");
        double a = in.nextDouble();
        
        Customer obj = new Customer(n, no, t, a);
        obj.compute();
        obj.display();
    }
}

Output

BlueJ output of Define a class named Customer with the following description: Instance variables /Data members: String cardName — Name of the card holder. long cardNo — Card Number. char cardType — Type of card, 'P' for Platinum, 'G' for Gold, 'S' for Silver. double purchaseAmount — The amount of purchase done on card. double cashback — The amount of cashback received. Member methods: Customer(String name, long num, char type, double amt) — Parameterised constructor to initialize all data members. void compute() — To compute the cashback based on purchase amount as follows: (a) For Silver ('S'), 2% of purchase amount (b) For Gold ('G'), 5% of purchase amount (c) For Platinum ('P'), 8% of purchase amount void display() — To display the details in the below format: Card Holder Name: Card Number: Purchase Amount: Cashback: Write a main method to input the card details from the user then create object of the class and call the member methods with the provided details.

Answered By

6 Likes