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:
- For Silver ('S'), 2% of purchase amount
- For Gold ('G'), 5% of purchase amount
- 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.
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
Related Questions
Name the following:
(a) Method with the same name as of the class and is invoked every time an object is created.
(b) Keyword to access the classes of a package.Consider the given program and answer the questions given below:
class temp { int a; temp() { a=10; } temp(int z) { a=z; } void print() { System.out.println(a); } void main() { temp t = new temp(); temp x = new temp(30); t.print(); x.print(); } }(a) What concept of OOPs is depicted in the above program with two constructors?
(b) What is the output of the method main()?