متجر إلكتروني جافا عرض تقديمي
CS1301 package shoponline;
Online Shop System ShopOnline
A Java application to manage purchases, calculate discounts and VAT, and generate the final invoice accurately.
Project Idea
Program Flowchart
Method-by-Method Guide
Project Idea & Problem Statement
2 / 19
Project Idea
Build a console-based online store in Java that lets users browse products, add quantities to their cart, compute totals with discount and VAT, and print a formatted invoice.
- Menu-driven loop until the user chooses Exit
- Modular design with dedicated methods
The Problem It Solves
Manual order calculation often causes arithmetic mistakes, missed conditional discounts, or incorrect VAT computations.
Software Solution:
Automates the cart: applies a 10% discount at 200 S.R.+, adds 15% VAT, and validates invalid inputs.
.
Program Flowchart
3 / 19
True (Loop) Start Init Vars = 0 Display Menu Read customerChoice switch (customerChoice) 1 2 3 4 default 0 displayProducts() buyProducts() Order Total printInvoice() Invalid Msg Goodbye Msg customerChoice != 0 ? False End
Start / End Input / Output Process / Call Decision
Class Setup & Static Variables
4 / 19
package shoponline;
import java.util.Scanner;
public class ShopOnline {
static Scanner input = new Scanner(System.in);
static int headph = 0;
static int keybo = 0;
static int mouse = 0;
static int usb = 0;
Package & Import
Imports Scanner to read user input from the keyboard.
Shared Scanner Object
Declares static Scanner input so both main and buyProducts can read input.
Cart Counters (static int)
Store quantities for headph, keybo, mouse, usb, starting at 0 and shared across all methods.
main() — Part 1: Menu & do-while Loop
5 / 19
public static void main(String[] args) {
int customerChoice;
System.out.println("\n========== ONLINE SHOP ==========");
System.out.println("1. View Products");
System.out.println("2. Buy Products");
System.out.println("3. Calculate Total");
System.out.println("4. Print Invoice");
System.out.println("0. Exit");
do { System.out.print("\nEnter your choice: ");
customerChoice = input.nextInt();
switch (customerChoice) {
case 1: displayProducts(); break;
case 2: buyProducts(); break;
Main Menu Display
Prints the shop options once before the loop and stores the user's selection in customerChoice.
do-while & Cases 1–2
Runs at least once, calling displayProducts() for 1 or buyProducts() for 2.
main() — Part 2: case 3
6 / 19
case 3: {
double subtotal = calculateSubtotal();
double discount = calculateDiscount(subtotal);
double afterDiscount = subtotal - discount;
double vat = afterDiscount * 0.15;
double finalTotal = calculateTotal();
System.out.println("\n------ ORDER TOTAL ------");
System.out.printf("Subtotal: %7.2f S.R.%n", subtotal);
System.out.printf("Discount: %7.2f S.R.%n", discount);
System.out.printf("After Discount: %7.2f S.R.%n", afterDiscount);
System.out.printf("VAT: %7.2f S.R.%n", vat);
System.out.println("-------------------------");
System.out.printf("Total: %7.2f S.R.%n", finalTotal);
break;
}
Total Breakdown
Computes subtotal, discount, amount after discount, 15% VAT, and final total.
printf Formatting
%7.2f right-aligns numbers to 2 decimal places with S.R..
main() — Part 3: Invoice & Exit
7 / 19
case 4:
printInvoice();
break;
case 0:
System.out.println("Thank you for shopping with us!");
System.out.println("Goodbye!");
break;
default:
System.out.println("Please enter a valid option!");
}
} while (customerChoice != 0);
}
Case 4: Invoice
Calls printInvoice() to display the full itemized receipt.
Case 0: Exit Loop
Prints a goodbye message and ends the do-while loop when customerChoice == 0.
default Case
Catches any invalid menu number and prompts the user again.
View Products: displayProducts()
8 / 19
public static void displayProducts() {
System.out.println("\n------- PRODUCTS -------");
System.out.println("1. Headphones - 150 S.R.");
System.out.println("2. Keyboard - 120 S.R.");
System.out.println("3. Mouse - 60 S.R.");
System.out.println("4. USB Cable - 25 S.R.\n");
}
Purpose
A void method with no parameters that prints the store's product catalog clearly.
Product Numbers
Shows each item's ID (1 to 4) and price in S.R. so the customer knows what to enter when buying.
buyProducts() — Part 1: Input Validation
9 / 19
public static void buyProducts() {
int productNumber;
int quantity;
boolean shop = true;
while (shop) {
System.out.print("Enter product number: ");
productNumber = input.nextInt();
if (productNumber == 0) {
shop = false;
} else if (productNumber < 1 || productNumber > 4) {
System.out.println("Invalid product number.");
} else {
System.out.print("Enter quantity: ");
quantity = input.nextInt();
while (shop) Loop
Lets the user buy multiple items in one session, stopping when 0 is entered.
Range Check
Rejects numbers outside 1–4, otherwise asks for the quantity.
buyProducts() — Part 2: Updating Cart
10 / 19
if (quantity <= 0) {
System.out.println("Invalid quantity.");
} else {
switch (productNumber) {
case 1: headph += quantity; break;
case 2: keybo += quantity; break;
case 3: mouse += quantity; break;
case 4: usb += quantity; break;
}
System.out.println(quantity + " " +
getProductName(productNumber) + " added.");
} } } }
Quantity Guard
quantity <= 0 prevents zero or negative quantities from entering the cart.
Accumulation (+=)
Adds the new quantity to the matching static counter and confirms with getProductName().
Product Name: getProductName(int product)
11 / 19
public static String getProductName(int product) {
switch (product) {
case 1:
return "Headphones";
case 2:
return "Keyboard";
case 3:
return "Mouse";
case 4:
return "USB Cable";
default:
return "Unknown";
}
}
Return Type String
Takes an integer product ID and returns its name using switch, or "Unknown" by default.
Reusability
Called in buyProducts() for confirmation messages and in printInvoice() for item rows.
Product Price: getPrice(int product)
12 / 19
public static double getPrice(int product) {
switch (product) {
case 1:
return 150.0;
case 2:
return 120.0;
case 3:
return 60.0;
case 4:
return 25.0;
default:
return 0.0;
}
}
Return Type double
Maps a product ID to its unit price (150.0, 120.0, 60.0, 25.0), returning 0.0 for default.
Where It Is Used
Used inside printInvoice() to calculate each line's cost (getPrice(i) * qty).
Subtotal: calculateSubtotal()
13 / 19
public static double calculateSubtotal(){
double totalHeadphones = headph * 150.0;
double totalKeyboard = keybo * 120.0;
double totalMouse = mouse * 60.0;
double totalUSBcable = usb * 25.0;
return totalHeadphones + totalKeyboard
+ totalMouse + totalUSBcable;
}
Calculation Logic
Multiplies each static quantity counter by its unit price and sums all four line totals.
Return Value
Returns the pre-discount, pre-tax subtotal used by calculateDiscount() and calculateTotal().
Discount: calculateDiscount(double subtotal)
14 / 19
public static double calculateDiscount(double subtotal) {
if (subtotal >= 200.0) {
return subtotal * 0.10;
} else {
return 0.0;
}
}
Discount Condition
Checks if (subtotal >= 200.0): orders of 200 S.R. or more earn a 10% discount.
Below Threshold
If the subtotal is under 200 S.R., the method returns 0.0 (no discount).
Final Total: calculateTotal()
15 / 19
public static double calculateTotal() {
double subtotal = calculateSubtotal();
double discount = calculateDiscount(subtotal);
double afterDiscount = subtotal - discount;
double vat = afterDiscount * 0.15;
return afterDiscount + vat;
}
1. Method Chaining
Calls calculateSubtotal() and calculateDiscount(), then subtracts the discount.
2. Adding 15% VAT
Computes 15% VAT on afterDiscount and returns the final payable amount.
printInvoice() — Part 1: printInvoice()
16 / 19
public static void printInvoice() {
double subtotal = calculateSubtotal();
double discount = calculateDiscount(subtotal);
double afterDiscount = subtotal - discount;
double vat = afterDiscount * 0.15;
double finalTotal = calculateTotal();
System.out.println("\\n========== INVOICE ==========");
if (headph > 0) System.out.printf("%-15s %3d %10.2f%n", getProductName(1), headph, getPrice(1) \* headph);
if (keybo > 0) System.out.printf("%-15s %3d %10.2f%n", getProductName(2), keybo, getPrice(2) \* keybo);
if (mouse > 0) System.out.printf("%-15s %3d %10.2f%n", getProductName(3), mouse, getPrice(3) \* mouse);
if (usb > 0) System.out.printf("%-15s %3d %10.2f%n", getProductName(4), usb, getPrice(4) \* usb);
Compute Totals
Calls helper methods to prepare subtotal, discount, VAT, and final total.
(> 0) Filter
Prints only items with quantity greater than 0, showing name, qty, and line total.
printInvoice() — Part 2: Summary
17 / 19
System.out.printf("%nSubtotal: %20.2f%n", subtotal);
System.out.printf("Discount: %20.2f%n", discount);
System.out.printf("VAT: %20.2f%n", vat);
System.out.println("-----------------------------");
System.out.printf("TOTAL: %20.2f%n", finalTotal);
System.out.println("\\nThank you for shopping!");
}
Aligned Columns (printf)
%20.2f aligns all currency values cleanly to 2 decimal places.
Closing Message
Displays a divider line, the final TOTAL, and a thank-you note to the customer.
Try Our Program Live!
18 / 19
Be Our First Customer!
Scan the QR Code to Experience ShopOnline
Test the complete shopping flow yourself: view products, add items to your cart, check your 10% discount & 15% VAT, and generate your invoice.
Browse
Shop
Invoice

Skip Main go to ShopOnline
Summary & Future Improvements
19 / 19
What We Built
- A menu-driven console shop system in Java
- Cart management with 10% discount (200 S.R.+) and 15% VAT
- Input validation and a formatted, itemized invoice
- Modular design using dedicated methods
Future Improvements
- Support more products using arrays or a file
- Handle non-numeric input with exception handling
- Add a graphical user interface (GUI)
- Save orders to a file
Thank you!