Skip to content
This repository was archived by the owner on Jun 29, 2024. It is now read-only.

Vyshnavi Tellagorla - Hard level tasks #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions Bank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

class Account {
private String accountNumber;
private String holderName;
private double balance;

public Account(String accountNumber, String holderName, double balance) {
this.accountNumber = accountNumber;
this.holderName = holderName;
this.balance = balance;
}

public String getAccountNumber() {
return accountNumber;
}

public String getHolderName() {
return holderName;
}

public double getBalance() {
return balance;
}

public void deposit(double amount) {
balance += amount;
}

public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient funds!");
}
}
}

public class OnlineBankingSystem {
private static Map<String, Account> accounts = new HashMap<>();
private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
while (true) {
System.out.println("1. Create Account");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int option = scanner.nextInt();
scanner.nextLine(); // Consume newline character

switch (option) {
case 1:
createAccount();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid option!");
}
}
}

private static void createAccount() {
System.out.print("Enter account number: ");
String accountNumber = scanner.nextLine();
System.out.print("Enter holder name: ");
String holderName = scanner.nextLine();
System.out.print("Enter initial balance: ");
double balance = scanner.nextDouble();
scanner.nextLine(); // Consume newline character

Account account = new Account(accountNumber, holderName, balance);
accounts.put(accountNumber, account);
System.out.println("Account created successfully!");
}

private static void deposit() {
System.out.print("Enter account number: ");
String accountNumber = scanner.nextLine();
Account account = accounts.get(accountNumber);
if (account != null) {
System.out.print("Enter deposit amount: ");
double amount = scanner.nextDouble();
account.deposit(amount);
System.out.println("Deposit successful. New balance: " + account.getBalance());
} else {
System.out.println("Account not found!");
}
}

private static void withdraw() {
System.out.print("Enter account number: ");
String accountNumber = scanner.nextLine();
Account account = accounts.get(accountNumber);
if (account != null) {
System.out.print("Enter withdrawal amount: ");
double amount = scanner.nextDouble();
account.withdraw(amount);
System.out.println("Withdrawal successful. New balance: " + account.getBalance());
} else {
System.out.println("Account not found!");
}
}
}
166 changes: 166 additions & 0 deletions Ecommerce.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Product {
private int id;
private String name;
private double price;

public Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public double getPrice() {
return price;
}
}

class CartItem {
private Product product;
private int quantity;

public CartItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}

public Product getProduct() {
return product;
}

public int getQuantity() {
return quantity;
}
}

class ShoppingCart {
private List<CartItem> items;

public ShoppingCart() {
this.items = new ArrayList<>();
}

public void addItem(CartItem item) {
items.add(item);
}

public List<CartItem> getItems() {
return items;
}

public double calculateTotal() {
double total = 0;
for (CartItem item : items) {
total += item.getProduct().getPrice() * item.getQuantity();
}
return total;
}
}

public class ECommercePlatform {
private static List<Product> products = new ArrayList<>();
private static ShoppingCart cart = new ShoppingCart();
private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
initializeProducts();

while (true) {
System.out.println("1. Browse Products");
System.out.println("2. Add to Cart");
System.out.println("3. View Cart");
System.out.println("4. Checkout");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int option = scanner.nextInt();
scanner.nextLine(); // Consume newline character

switch (option) {
case 1:
browseProducts();
break;
case 2:
addToCart();
break;
case 3:
viewCart();
break;
case 4:
checkout();
break;
case 5:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid option!");
}
}
}

private static void initializeProducts() {
products.add(new Product(1, "Product 1", 10.0));
products.add(new Product(2, "Product 2", 20.0));
// Add more products here
}

private static void browseProducts() {
System.out.println("Available Products:");
for (Product product : products) {
System.out.println(product.getId() + ". " + product.getName() + " - $" + product.getPrice());
}
}

private static void addToCart() {
System.out.print("Enter product ID: ");
int productId = scanner.nextInt();
scanner.nextLine(); // Consume newline character
Product selectedProduct = null;
for (Product product : products) {
if (product.getId() == productId) {
selectedProduct = product;
break;
}
}
if (selectedProduct != null) {
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();
scanner.nextLine(); // Consume newline character
cart.addItem(new CartItem(selectedProduct, quantity));
System.out.println("Item added to cart!");
} else {
System.out.println("Product not found!");
}
}

private static void viewCart() {
List<CartItem> items = cart.getItems();
if (items.isEmpty()) {
System.out.println("Cart is empty.");
} else {
System.out.println("Cart:");
for (CartItem item : items) {
System.out.println(item.getProduct().getName() + " - Quantity: " + item.getQuantity());
}
System.out.println("Total: $" + cart.calculateTotal());
}
}

private static void checkout() {
System.out.println("Checkout - Total: $" + cart.calculateTotal());
System.out.println("Thank you for shopping with us!");
// Logic for completing the checkout process (e.g., payment, order creation, etc.) would go here
// This is a simplified version without payment integration or order tracking
cart = new ShoppingCart(); // Clear the cart after checkout
}
}
Loading