FreshMart Java Project Documentation
1. Project Overview
FreshMart একটি কনসোল-ভিত্তিক গ্রোসারি শপ সিমুলেশন, যেখানে একজন ইউজার প্রোডাক্ট
দেখতে পারে, কার্টে যোগ করতে পারে, মুছতে পারে এবং চেকআউট করতে পারে।
2. Classes and Their Responsibilities
2.1 Shop Class
Purpose:
- ইউজারকে স্বাগত জানানো ও নাম নেওয়া।
Methods:
- welcome() – দোকানের ওয়েলকাম মেসেজ।
- CustomerName() – ইউজারের নাম ইনপুট নেয়।
2.2 GroceryShop Class (extends Shop)
Purpose:
- মূল দোকানের প্রোডাক্ট, কার্ট, ও কেনাকাটার সব কার্যক্রম এখানে হয়।
Attributes:
- String[] productNames – প্রোডাক্ট নামের তালিকা।
- double[] productPrices – প্রতিটি প্রোডাক্টের দাম।
- int[] cart – ইউজার কতটা প্রোডাক্ট নিয়েছে তা রাখে।
Methods:
- showItems() – প্রোডাক্ট লিস্ট দেখায়।
- addToCart(int productNumber, int quantity) – প্রোডাক্ট কার্টে যোগ করে।
- viewCart() – কার্টে কী আছে তা দেখায়।
- removeFromCart() – প্রোডাক্ট কার্ট থেকে মুছে ফেলে বা কমায়।
- checkout() – বিল বানায় ও অনলাইন পেমেন্টে ৫% ছাড় দেয়।
2.3 Main Class
Purpose:
- প্রোগ্রামের entry point, ইউজারের ইনপুট ও অপশন নির্বাচন।
Main Steps:
1. welcome() কল হয়।
2. CustomerName() কল হয়।
3. প্রোডাক্ট যোগের জন্য লুপ।
4. পরবর্তী অপশন মেনু (View Cart, Remove, Checkout, Exit)।
3. Flow Diagram (Textual Version)
START
|
|--> Show Welcome Message
|--> Ask for Customer Name
|
|--> LOOP: Show Products
|--> Input Product Number and Quantity
|--> Add to Cart
|--> Repeat until product number is 0
|
|--> LOOP: Menu (View Cart / Remove Item / Checkout / Exit)
|--> If View Cart --> Show items and total
|--> If Remove --> Remove selected product
|--> If Checkout --> Show receipt, apply discount, END
|--> If Exit --> END
4. Special Features
- Online Payment Discount: যদি ইউজার পেমেন্ট করতে চায়, তাহলে বিলের উপর ৫% ছাড়।
- Error Handling: ভুল ইনপুট (যেমন অযৌক্তিক সংখ্যা) ধরার জন্য try-catch ব্যবহার করা হয়েছে।
- Modular Code: প্রতিটি কাজের জন্য আলাদা মেথড আছে – কোড পরিষ্কার ও মেইনটেইনযোগ্য।
Code with comment ::
import java.util.Scanner; // স্ক্যানার লাইব্রেরি ইম্পোর্ট করা হচ্ছে ইনপুট নেয়ার জন্য
// Shop নামক ক্লাস, যেখানে দোকানের সাধারণ কার্যক্রম থাকবে
class Shop {
String customerName; // কাস্টমারের নাম স্টোর করার জন্য ভ্যারিয়েবল
// welcome() মেথড: দোকানে স্বাগতম জানানো
void welcome() {
System.out.println("=======================================");
System.out.println("
Welcome to FreshMart!
"); // ওয়েলকাম মেসেজ
System.out.println(" Your one-stop grocery shop! "); // দোকানের স্লোগান
System.out.println("=======================================");
}
// CustomerName() মেথড: কাস্টমারের নাম ইনপুট নেওয়ার জন্য
void CustomerName() {
Scanner scanner = new Scanner(System.in); // স্ক্যানার অবজেক্ট তৈরি
System.out.print("Enter your name: "); // ইউজারকে নাম ইনপুট দেওয়ার জন্য প্রম্পট
customerName = scanner.nextLine(); // ইউজারের নাম গ্রহণ করা
System.out.println("Welcome to FreshMart, " + customerName + "!"); // গ্রীটিং মেসেজ
}
}
// GroceryShop ক্লাস, যেটি Shop ক্লাসকে extend করে
class GroceryShop extends Shop {
// প্রোডাক্টের নাম ও দাম সংরক্ষণ করার জন্য অ্যারে
String[] productNames = {"Jackfruit", "Papaya", "Orange", "Litchi", "Mango"};
double[] productPrices = {30.0, 20.0, 25.0, 20.0, 30.0};
int[] cart = new int[productNames.length]; // কার্টের জন্য অ্যারে
// showItems() মেথড: সকল প্রোডাক্ট দেখানোর জন্য
void showItems() {
System.out.println("\nAvailable product items:"); // "Available product items" টেক্সট
// লুপের মাধ্যমে সব প্রোডাক্ট এবং তাদের দাম দেখানো
for (int i = 0; i < productNames.length; i++) {
System.out.println((i + 1) + ". " + productNames[i] + " - ৳" + productPrices[i]); //
প্রোডাক্টের নাম ও দাম
}
}
// addToCart() মেথড: ইউজার যেটি প্রোডাক্ট কার্টে যোগ করতে চায়
void addToCart(int productNumber, int quantity) {
if (productNumber >= 1) { // যদি প্রোডাক্ট নম্বর 1 বা তার বেশি হয়
if (productNumber <= productNames.length) { // যদি প্রোডাক্ট নম্বর অ্যারে সাইজের
মধ্যে হয়
if (quantity > 0) { // যদি পরিমাণ 0 এর বেশি হয়
int index = productNumber - 1; // অ্যারে ইনডেক্সের জন্য 1 কমানো হচ্ছে
cart[index] += quantity; // নির্দিষ্ট প্রোডাক্টের কার্টে পরিমাণ যোগ করা হচ্ছে
System.out.println("\n" + quantity + " item(s) of \"" + productNames[index] + "\"
added to the cart."); // সফলতার বার্তা
} else {
System.out.println("Quantity must be greater than 0."); // ভুল পরিমাণ দিলে মেসেজ
}
} else {
System.out.println("Invalid product number. Please choose between 1 and " +
productNames.length + "."); // ভুল প্রোডাক্ট নাম্বার
}
} else {
System.out.println("Product number must be at least 1."); // প্রোডাক্ট নম্বর 1 এর বেশি
হতে হবে
}
}
// viewCart() মেথড: কার্টের সব প্রোডাক্ট দেখানোর জন্য
void viewCart() {
double total = 0; // মোট বিলের জন্য ভ্যারিয়েবল
boolean empty = true; // কার্ট খালি কিনা চেক করার জন্য ফ্ল্যাগ
System.out.println("\nYour Cart is:"); // কার্ট দেখানোর জন্য মেসেজ
// লুপের মাধ্যমে কার্টের সব প্রোডাক্ট দেখানো
for (int i = 0; i < cart.length; i++) {
if (cart[i] > 0) { // যদি কার্টে প্রোডাক্ট থাকে
double cost = cart[i] * productPrices[i]; // মোট মূল্য হিসাব করা
System.out.println("Product list sl ." + (i + 1) + "~ " + productNames[i] + " ৳" +
productPrices[i] + " x " + cart[i] + " = ৳" + cost); // প্রোডাক্টের বিস্তারিত
total += cost; // মোট মূল্য আপডেট করা
empty = false; // কার্ট খালি নয়
}
}
if (empty) {
System.out.println("Your cart is empty."); // যদি কার্ট খালি থাকে
} else {
System.out.println("Total = ৳" + total); // মোট মূল্য দেখানো
}
}
// removeFromCart() মেথড: কার্ট থেকে প্রোডাক্ট রিমুভ করার জন্য
void removeFromCart() {
viewCart(); // প্রথমে কার্ট দেখাও
Scanner scanner = new Scanner(System.in); // ইনপুট নিতে স্ক্যানার
System.out.print("Enter product number to remove: "); // ইউজারকে প্রোডাক্ট নাম্বার
ইনপুট দেওয়ার জন্য প্রম্পট
int productNumber;
try {
productNumber = scanner.nextInt(); // ইউজারের ইনপুট
} catch (Exception e) {
System.out.println("Invalid input. Please enter a number."); // ইনপুট ভুল হলে মেসেজ
scanner.nextLine(); // স্ক্যানার ক্লিয়ার
return;
}
if (productNumber < 1 || productNumber > productNames.length) { // যদি প্রোডাক্ট
নাম্বার ভুল হয়
System.out.println("Invalid product number.");
return;
}
if (cart[productNumber - 1] == 0) { // যদি প্রোডাক্ট কার্টে না থাকে
System.out.println("Item not in cart.");
return;
}
System.out.println(productNames[productNumber - 1] + " currently in cart: " +
cart[productNumber - 1]); // কতটা প্রোডাক্ট কার্টে আছে তা দেখানো
System.out.print("Enter quantity to remove: "); // ইউজারকে কতটা রিমুভ করতে চায় তা জানতে
চাওয়া
int quantityRm;
try {
quantityRm = scanner.nextInt(); // ইউজারের ইনপুট
} catch (Exception e) {
System.out.println("Invalid quantity input."); // ইনপুট ভুল হলে মেসেজ
scanner.nextLine();
return;
}
if (quantityRm <= 0) { // যদি পরিমাণ 0 বা তার কম হয়
System.out.println("Invalid quantity. Must be greater than 0.");
} else if (quantityRm >= cart[productNumber - 1]) { // যদি রিমুভ করার পরিমাণ কার্টে থাকা
পরিমাণের সমান বা বেশি হয়
cart[productNumber - 1] = 0; // প্রোডাক্ট পুরোপুরি রিমুভ করা
System.out.println("All of " + productNames[productNumber - 1] + " removed from
cart."); // রিমুভ করার বার্তা
} else {
cart[productNumber - 1] -= quantityRm; // শুধু নির্দিষ্ট পরিমাণ রিমুভ করা
System.out.println("Removed " + quantityRm + " " + productNames[productNumber - 1]
+ "(s) from cart.");
}
}
// checkout() মেথড: চেকআউট এবং মোট বিল হিসাব করার জন্য
double checkout() {
double total = 0; // মোট বিলের জন্য ভ্যারিয়েবল
System.out.println("\n============= RECEIPT =============");
System.out.println("Customer: " + customerName); // কাস্টমারের নাম
System.out.println("-----------------------------------");
for (int i = 0; i < cart.length; i++) {
if (cart[i] > 0) { // যদি কার্টে প্রোডাক্ট থাকে
double itemTotal = cart[i] * productPrices[i]; // প্রতিটি আইটেমের মোট মূল্য
System.out.println(productNames[i] + " x " + cart[i] + " = ৳" + itemTotal); //
প্রোডাক্টের তথ্য
total += itemTotal; // মোট বিল আপডেট করা
}
}
System.out.println("-----------------------------------");
System.out.println("Subtotal: ৳" + total); // সাবটোটাল
System.out.print("Want to Online payment? (1 = Yes / 0 = No): "); // ইউজারকে অনলাইন
পেমেন্ট করতে চায় কি না তা জানতে চাওয়া
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt(); // পেমেন্টের পছন্দ নেওয়া
if (choice == 1) { // যদি ইউজার অনলাইন পেমেন্ট চায়
double discount = total * 0.05; // ডিসকাউন্ট হিসাব করা (৫%)
total -= discount; // ডিসকাউন্ট কাটা
System.out.println("Online Payment Discount (5%): -৳" + discount);
}
System.out.println("Total Bill: ৳" + total); // চূড়ান্ত বিল
System.out.println("===================================");
System.out.println("Thank you for shopping with FreshMart!"); // ধন্যবাদ বার্তা
System.out.println("Please visit us again!"); // আবার আসার জন্য অনুরোধ
System.out.println("===================================");
return total; // চেকআউটের পর মোট বিল ফেরত দেওয়া
}
}
// Main ক্লাস যেখানে প্রোগ্রাম রান করবে
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // স্ক্যানার অবজেক্ট তৈরি
GroceryShop sp = new GroceryShop(); // GroceryShop অবজেক্ট তৈরি
sp.welcome(); // ওয়েলকাম মেথড কল
sp.CustomerName(); // কাস্টমারের নাম ইনপুট নেয়া
// লুপ যেখানে ইউজার প্রোডাক্ট অ্যাড করতে পারবে
while (true) {
sp.showItems(); // সকল প্রোডাক্ট দেখানো
System.out.println("\nEnter the product number to add to the cart \n(or 0 to finish cart): ");
// ইউজারকে পণ্য নির্বাচন করতে বলা
int productNumber = scanner.nextInt(); // ইউজার থেকে ইনপুট নেওয়া
if (productNumber == 0) // ইউজার 0 দিলে লুপ বন্ধ হবে
break;
// ইউজার যদি সঠিক প্রোডাক্ট নম্বর প্রদান করে
if (productNumber >= 1) {
if (productNumber <= sp.productNames.length) { // যদি প্রোডাক্ট নম্বর বৈধ হয়
String pdName = sp.productNames[productNumber - 1]; // প্রোডাক্টের নাম
System.out.print("How many " + pdName + "(s) do you want to buy: "); // কতটি
কিনতে চায় জানতে চাওয়া
int quantity = scanner.nextInt(); // পরিমাণ ইনপুট নেওয়া
sp.addToCart(productNumber, quantity); // প্রোডাক্ট কার্টে যোগ করা
} else {
System.out.println("Invalid product number."); // ভুল প্রোডাক্ট নম্বর
}
} else {
System.out.println("Product number must be at least 1."); // প্রোডাক্ট নম্বর ভুল হলে
মেসেজ
}
}
// দ্বিতীয় লুপ যেখানে ইউজার কার্ট দেখা, আইটেম রিমুভ করা বা চেকআউট করতে পারবে
boolean runLoop = true;
while (runLoop) {
System.out.println("\nWhat would you like to do next?"); // ইউজারের কাছে অপশন জানতে
চাওয়া
System.out.println("1. View Cart");
System.out.println("2. Remove Item from Cart");
System.out.println("3. Checkout");
System.out.println("4. Exit");
System.out.print("\nEnter your choice: "); // ইউজারের চয়েস ইনপুট নেওয়া
int choice = scanner.nextInt();
switch (choice) {
case 1:
sp.viewCart(); // কার্ট দেখানো
break;
case 2:
sp.removeFromCart(); // কার্ট থেকে আইটেম রিমুভ করা
break;
case 3:
sp.checkout(); // চেকআউট করা
runLoop = false; // লুপ বন্ধ করা
break;
case 4:
System.out.println("Thanks for visiting FreshMart!"); // আউটপুট
runLoop = false;
break;
default:
System.out.println("Invalid choice."); // ভুল চয়েস দিলে বার্তা
}
}
scanner.close(); // স্কয
্ ানার বন্ধ
}
}
Output ::
=======================================
Welcome to FreshMart!
Your one-stop grocery shop!
=======================================
Enter your name: Rakib
Welcome to FreshMart, Rakib!
Available product items:
1. Jackfruit - ৳30.0
2. Papaya - ৳20.0
3. Orange - ৳25.0
4. Litchi - ৳20.0
5. Mango - ৳30.0
Enter the product number to add to the cart
(or 0 to finish cart):
2
How many Papaya(s) do you want to buy: 3
3 item(s) of "Papaya" added to the cart.
...
What would you like to do next?
1. View Cart
2. Remove Item from Cart
3. Checkout
4. Exit
Enter your choice: 3
============= RECEIPT =============
Customer: Rakib
----------------------------------Papaya x 3 = ৳60.0
----------------------------------Subtotal: ৳60.0
Want to Online payment? (1 = Yes / 0 = No): 1
Online Payment Discount (5%): -৳3.0
Total Bill: ৳57.0
===================================
Thank you for shopping with FreshMart!
Please visit us again!
===================================
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )