0% found this document useful (0 votes)
7 views14 pages

Name: Ridhima Srivastava Uid: 2024200126 Experiment No. 7b Aim: Program 1 Problem Statement

The document outlines a Java program that simulates a shopping experience at a Westside store, where customers can purchase clothes and accessories using either cash or credit card payments. It defines several classes including Payment, CashPayment, CreditCardPayment, and Person, and implements a main method to handle user input for purchases. The program demonstrates object-oriented programming concepts such as abstraction and inheritance while managing inventory and processing transactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views14 pages

Name: Ridhima Srivastava Uid: 2024200126 Experiment No. 7b Aim: Program 1 Problem Statement

The document outlines a Java program that simulates a shopping experience at a Westside store, where customers can purchase clothes and accessories using either cash or credit card payments. It defines several classes including Payment, CashPayment, CreditCardPayment, and Person, and implements a main method to handle user input for purchases. The program demonstrates object-oriented programming concepts such as abstraction and inheritance while managing inventory and processing transactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Name: Ridhima Srivastava

UID: 2024200126

Experiment No. 7b

AIM:

Program 1

PROBLEM Define a Westside class that has sales in clothes and


STATEMENT : accessories. Let us say 10 clothes and 10 accessories each cost
5000. Clothes and accessories are limited and updated as soon
as purchase is done. Define a class named Payment (abstract
class) that contains an instance variable of type double that
stores the amount of the payment. Amount is initialized 25,000
and updated with each purchase. Also create a method named
(abstract) paymentDetails that updates the amount of the
payment. Next, define a class named CashPayment that is
derived from Payment. This class should redefine the
paymentDetails method to indicate that the payment is in cash.
Include appropriate constructor(s)/methods. Define a class
named CreditCardPayment that is derived from Payment. This
class should contain instance variables for the name on the
card, expiration date, and credit card number. Include
appropriate constructor(s)/methods. Finally, redefine the
paymentDetails method to include all credit card information in
the printout. Define a class Person that contains person_name
and P_phone_no. Create a main method that creates at least
five persons who will be given random chances for buying using
any payment method CashPayment /CreditCardPayment. Once
a person buys clothes/ accessories, the amount gets
debited.Note that both CashPayment and CreditCardPayment
will be derived from the Payment class.

Sample Output:

Enter number of persons: 5

Enter details for person 1:


Name: John

Phone Number: 9876543210

Enter details for person 2:

Name: Sarah

Phone Number: 9123456789

Enter details for person 3:

Name: Emily

Phone Number: 9234567890

Enter details for person 4:

Name: David

Phone Number: 9345678901

Enter details for person 5:

Name: Bob

Phone Number: 9456789012

Total number of persons: 5

===== Simulating Purchases for Random Persons =====

Emily (9234567890) is making a purchase...

Choose item to buy:

1. Clothes

2. Accessories

2
Choose payment method:

1. Cash

2. Credit Card

Enter Card Holder Name: Emily

Enter Card Expiry Date (MM/YY): 12/27

Enter Card Number: 4321-8765-2109-6543

Payment of ₹5000.0 done using Credit Card.

Card Name: Emily

Card Expiry: 12/27

Card Number: 4321-8765-2109-6543

Remaining balance: ₹20000.0

Accessories purchased. Remaining stock: 9

---------------------------------------

Bob (9456789012) is making a purchase...

Choose item to buy:

1. Clothes

2. Accessories

Choose payment method:

1. Cash

2. Credit Card

Payment of ₹5000.0 done using Cash. Remaining balance:


₹20000.0

Clothes purchased. Remaining stock: 9


---------------------------------------

John (9876543210) is making a purchase...

Choose item to buy:

1. Clothes

2. Accessories

Choose payment method:

1. Cash

2. Credit Card

Enter Card Holder Name: John

Enter Card Expiry Date (MM/YY): 09/26

Enter Card Number: 8765-4321-1098-7654

Payment of ₹5000.0 done using Credit Card.

Card Name: John

Card Expiry: 09/26

Card Number: 8765-4321-1098-7654

Remaining balance: ₹15000.0

Accessories purchased. Remaining stock: 8

---------------------------------------

Sarah (9123456789) is making a purchase...

Choose item to buy:

1. Clothes

2. Accessories
1

Choose payment method:

1. Cash

2. Credit Card

Payment of ₹5000.0 done using Cash. Remaining balance:


₹15000.0

Clothes purchased. Remaining stock: 8

---------------------------------------

David (9345678901) is making a purchase...

Choose item to buy:

1. Clothes

2. Accessories

Choose payment method:

1. Cash

2. Credit Card

Enter Card Holder Name: David

Enter Card Expiry Date (MM/YY): 06/28

Enter Card Number: 5678-1234-9012-3456

Payment of ₹5000.0 done using Credit Card.

Card Name: David

Card Expiry: 06/28

Card Number: 5678-1234-9012-3456

Remaining balance: ₹10000.0


Clothes purchased. Remaining stock: 7

PROGRAM: import java.util.*;


abstract class Payment {
double amount;

public Payment() {
this.amount = 25000.0;
}

public abstract void paymentDetails();

public double getAmount() {


return amount;
}

public void updateAmount(double cost) {


amount -= cost;
}
}

class CashPayment extends Payment {


public CashPayment() {
super();
}

@Override
public void paymentDetails() {
System.out.println("Payment of ₹5000.0 done using Cash. Remaining
balance: ₹" + amount);
}
}

class CreditCardPayment extends Payment {


private String cardHolderName;
private String expiryDate;
private String cardNumber;

public CreditCardPayment(String cardHolderName, String expiryDate,


String cardNumber) {
super();
this.cardHolderName = cardHolderName;
this.expiryDate = expiryDate;
this.cardNumber = cardNumber;
}

@Override
public void paymentDetails() {
System.out.println("Payment of ₹5000.0 done using Credit Card.");
System.out.println("Card Name: " + cardHolderName);
System.out.println("Card Expiry: " + expiryDate);
System.out.println("Card Number: " + cardNumber);
System.out.println("Remaining balance: ₹" + amount);
}
}
class Westside {
private static int clothesStock = 10;
private static int accessoriesStock = 10;
private static final double ITEM_COST = 5000.0;

public static boolean purchaseItem(int itemChoice) {


if (itemChoice == 1 && clothesStock > 0) {
clothesStock--;
return true;
} else if (itemChoice == 2 && accessoriesStock > 0) {
accessoriesStock--;
return true;
}
return false;
}

public static int getClothesStock() {


return clothesStock;
}

public static int getAccessoriesStock() {


return accessoriesStock;
}
}
class Person {
String personName;
String phoneNumber;

public Person(String personName, String phoneNumber) {


this.personName = personName;
this.phoneNumber = phoneNumber;
}

public String getName() {


return personName;
}

public String getPhoneNumber() {


return phoneNumber;
}
}

public class main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();

System.out.print("Enter number of persons: ");


int numPersons = scanner.nextInt();
scanner.nextLine();

Person[] persons = new Person[numPersons];


for (int i = 0; i < numPersons; i++) {
System.out.println("Enter details for person " + (i + 1) + ":");
System.out.print("Name: ");
String name = scanner.nextLine(); // Read the full name
System.out.print("Phone Number: ");
String phoneNumber = scanner.nextLine(); // Read the phone
number
persons[i] = new Person(name, phoneNumber);
}

System.out.println("\nTotal number of persons: " + numPersons);


System.out.println("\n---------------------------------------");

for (int i = 0; i < numPersons; i++) {


Person person = persons[random.nextInt(numPersons)];
System.out.println("\n" + person.getName() + " (" +
person.getPhoneNumber() + ") is making a purchase...");

// Item selection
System.out.println("Choose item to buy:");
System.out.println("1. Clothes");
System.out.println("2. Accessories");
int itemChoice = scanner.nextInt();
scanner.nextLine();

// Payment method selection


System.out.println("Choose payment method:");
System.out.println("1. Cash");
System.out.println("2. Credit Card");
int paymentMethodChoice = scanner.nextInt();
scanner.nextLine();

if (Westside.purchaseItem(itemChoice)) {
double cost = 5000.0;

if (paymentMethodChoice == 1) { // Cash
CashPayment cashPayment = new CashPayment();
cashPayment.updateAmount(cost);
cashPayment.paymentDetails();
} else if (paymentMethodChoice == 2) { // Credit Card
System.out.print("Enter Card Holder Name: ");
String cardHolderName = scanner.nextLine(); // Read the
cardholder name
System.out.print("Enter Card Expiry Date (MM/YY): ");
String expiryDate = scanner.nextLine(); // Read the expiry date
System.out.print("Enter Card Number: ");
String cardNumber = scanner.nextLine(); // Read the card
number

CreditCardPayment creditCardPayment = new


CreditCardPayment(cardHolderName, expiryDate, cardNumber);
creditCardPayment.updateAmount(cost);
creditCardPayment.paymentDetails();
}

// Output item purchase result


if (itemChoice == 1)
{
System.out.println("Clothes purchased. Remaining stock: " +
Westside.getClothesStock());
} else
{
System.out.println("Accessories purchased. Remaining stock: "
+ Westside.getAccessoriesStock());
}
} else
{
System.out.println(itemChoice == 1 ? "Clothes are out of stock." :
"Accessories are out of stock.");
}

System.out.println("\n---------------------------------------");
}

}
}

RESULT:
CONCLUSION: From the above programs i revised the concepts of abstarction,multiple
classes and revising previous concepts of java.

You might also like