What's new

Java Tapos na

Sure, I can help you with that. Do you have any specific requirements or instructions on how to implement the Java Files and I/O or the OOP concepts?
 
no just dont add hashmap
Alright, here's an outline for a simple ordering system in Java without using HashMap:

1. Create a Product class to represent each item in the menu. Each Product object should have a name, price, and quantity.

2. Create a Menu class to hold a list of available Product objects. This class should have methods to add products to the menu and retrieve the list of products.

3. Create an Order class to represent an individual order. An Order object should have a reference to the Product being ordered and the quantity of that product.

4. Create a Cart class to hold a list of Order objects. This class should have methods to add orders to the cart, remove orders from the cart, and retrieve the list of orders.

5. Create a Payment class to represent a payment for a cart of orders. A Payment object should have an amount due and a method to make the payment.

6. Create a Receipt class to represent a receipt for a completed transaction. A Receipt object should have a reference to the Cart object and the total amount ρáíd.

7. Create a Main class to run the program. This class should have methods to display the menu, take user input for orders, and display the contents of the cart. When the user is ready to checkout, the program should create a Cart object and a Payment object, calculate the total cost of the cart, and create a Receipt object. Finally, the program should write the transaction details to a file.

Let me know if you want me to provide sample code for any of these classes.
 
Alright, here's an outline for a simple ordering system in Java without using HashMap:

1. Create a Product class to represent each item in the menu. Each Product object should have a name, price, and quantity.

2. Create a Menu class to hold a list of available Product objects. This class should have methods to add products to the menu and retrieve the list of products.

3. Create an Order class to represent an individual order. An Order object should have a reference to the Product being ordered and the quantity of that product.

4. Create a Cart class to hold a list of Order objects. This class should have methods to add orders to the cart, remove orders from the cart, and retrieve the list of orders.

5. Create a Payment class to represent a payment for a cart of orders. A Payment object should have an amount due and a method to make the payment.

6. Create a Receipt class to represent a receipt for a completed transaction. A Receipt object should have a reference to the Cart object and the total amount ρáíd.

7. Create a Main class to run the program. This class should have methods to display the menu, take user input for orders, and display the contents of the cart. When the user is ready to checkout, the program should create a Cart object and a Payment object, calculate the total cost of the cart, and create a Receipt object. Finally, the program should write the transaction details to a file.

Let me know if you want me to provide sample code for any of these classes.
can u code for me?
 
can u code for me?
Sure, here's a sample code for the classes mentioned in the outline:

Product.java

Code:
public class Product {
    private String name;
    private double price;
    private int quantity;

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

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public int getQuantity() {
        return quantity;
    }
}

Menu.java

Code:
import java.util.ArrayList;
import java.util.List;

public class Menu {
    private List<Product> products;

    public Menu() {
        products = new ArrayList<>();
    }

    public void addProduct(Product product) {
        products.add(product);
    }

    public List<Product> getProducts() {
        return products;
    }
}

Order.java

Code:
public class Order {
    private Product product;
    private int quantity;

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

    public Product getProduct() {
        return product;
    }

    public int getQuantity() {
        return quantity;
    }
}

Cart.java

Code:
import java.util.ArrayList;
import java.util.List;

public class Cart {
    private List<Order> orders;

    public Cart() {
        orders = new ArrayList<>();
    }

    public void addOrder(Order order) {
        orders.add(order);
    }

    public void removeOrder(Order order) {
        orders.remove(order);
    }

    public List<Order> getOrders() {
        return orders;
    }

    public double getTotalCost() {
        double totalCost = 0;
        for (Order order : orders) {
            totalCost += order.getProduct().getPrice() * order.getQuantity();
        }
        return totalCost;
    }
}

Payment.java

Code:
public class Payment {
    private double amountDue;

    public Payment(double amountDue) {
        this.amountDue = amountDue;
    }

    public boolean makePayment(double amountPaid) {
        if (amountPaid >= amountDue) {
            return true;
        } else {
            return false;
        }
    }
}

Receipt.java

Code:
import java.io.FileWriter;
import java.io.IOException;

public class Receipt {
    private Cart cart;
    private double amountPaid;

    public Receipt(Cart cart, double amountPaid) {
        this.cart = cart;
        this.amountPaid = amountPaid;
    }

    public void printReceipt(String fileName) throws IOException {
        FileWriter writer = new FileWriter(fileName);
        writer.write("Order details:\n");
        writer.write("--------------\n");
        for (Order order : cart.getOrders()) {
            writer.write(order.getQuantity() + " x " + order.getProduct().getName() + " - $" + order.getProduct().getPrice() + "\n");
        }
        writer.write("\n");
        writer.write("Total cost: $" + cart.getTotalCost() + "\n");
        writer.write("Amount ρáíd: $" + amountPaid + "\n");
        writer.write("Change: $" + (amountPaid - cart.getTotalCost()) + "\n");
        writer.close();
    }
}

Main.java

Code:
import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // create menu
        Menu menu = new Menu();
        menu.addProduct(new Product("Product 1", 10.99, 5));
        menu.addProduct(new Product("Product 2", 5.99, 10));
        menu.addProduct(new Product("Product 3", 8.49, 3));

        // display menu
        System.out.println("Menu:");
        System.out.println("-----");
        for (Product product : menu.getProducts()) {
            System.out.println(product.getName() + " - $" + product.getPrice() + " (" + product.getQuantity() + " available)");
        }

        // create cart
        Cart cart = new Cart();

        // take user input for orders
        Scanner scanner = new Scanner(System.in);
        boolean isOrdering = true;
        while (isOrdering) {
            System.out.println("Enter product name (or 'done' to finish ordering):");
            String productName = scanner.nextLine();
            if (productName.equalsIgnoreCase("done")) {
                isOrdering = false;
            } else {
                Product selectedProduct = null;
                for (Product product : menu.getProducts()) {
                    if (product.getName().equalsIgnoreCase(productName)) {
                        selectedProduct = product;
                        break;
                    }
                }
                if (selectedProduct != null) {
                    System.out.println("Enter quantity:");
                    int quantity = scanner.nextInt();
                    scanner.nextLine(); // consume newline
                    if (quantity > selectedProduct.getQuantity()) {
                        System.out.println("Error: not enough quantity available.");
                    } else {
                        cart.addOrder(new Order(selectedProduct, quantity));
                        System.out.println("Order added to cart.");
                    }
                } else {
                    System.out.println("Error: product not found.");
                }
            }
        }

        // display cart
        System.out.println("Cart:");
        System.out.println("-----");
        for (Order order : cart.getOrders()) {
            System.out.println(order.getQuantity() + " x " + order.getProduct().getName() + " - $" + order.getProduct().getPrice() + " each");
        }
        System.out.println("Total cost: $" + cart.getTotalCost());

        // make payment
        System.out.println("Enter amount ρáíd:");
        double amountPaid = scanner.nextDouble();
        Payment payment = new Payment(cart.getTotalCost());
        if (payment.makePayment(amountPaid)) {
            System.out.println("Payment successful.");
        } else {
            System.out.println("Error: amount ρáíd is insufficient.");
        }

        // create receipt
        Receipt receipt = new Receipt(cart, amountPaid);
        try {
            receipt.printReceipt("Receipt.txt");
            System.out.println("Transaction details written to file.");
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

This is just a sample code, so feel free to modify it to suit your needs. Let me know if you have any questions!
 

Similar threads

Back
Top