Answer to Question #243885 in Java | JSP | JSF for ssmi

Question #243885

* Make an application with the above classes(invoice,item,itemQuantity,storage) for a client that comes with a list of items * that needs to purchase with the following ruleset: - At the beginning at least 5 different types of items must be added to the storage (use the method addItemQuantity() from Storage.class) - Then create a list of items of the client. - For every item first should be checked if the items are available (use getItemQuantity of Storage.class) - If an item is unavailable do not include it in the invoice - If the item is available include it in the invoice - After all the items are added to the invoice, show all the items added with the description, quantity and cost of the item. This final list should be shown ordered in alphabetical order. - output the invoice into a text file (invoice.text) and save it on the local PC.


1
Expert's answer
2021-09-30T00:57:59-0400
import java.util.ArrayList;
import java.util.Scanner;
// class product having attributes of any product in Extreme IT products
class Storage {

    private String quantity;
    private String description;
    private double price;

    // the constructor
    public Storage() {
        this.quantity = "";
        this.description = "";
        this.price = 0;
    }

    public Storage(String quantity, String description, double price) {
        this.quantity = quantity;
        this.description = description;
        this.price = price;
    }

    public String getItemQuantity() {
        return quantity;
    }

    public void addItemQuantity(String quantity) {
        this.quantity = quantity;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Item quantity: " + this.quantity + "\n" + "Item description: " + this.description + "\n"
                + "Item price: " + this.price + "\n";
    }
}

public class Main {
    private static int getProductByCode(ArrayList<Storage> products, String code) {

        for (int i = 0; i < products.size(); i++) {
            if (products.get(i).getItemQuantity().compareTo(code) == 0) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Storage> products = new ArrayList<Storage>();
        ArrayList<Storage> invoice = new ArrayList<Storage>();
        String [][] my_items = {
                {}
        };
        String quantity;
        String description;
        double price;
        int terminate_condition = 1;
        System.out.println("Welcome to DKay shopping center application");
        while (terminate_condition == 1) {
            System.out.println("1. purchase products");
            System.out.println("2. purchase products");
            System.out.println("3.print all items in storage");

            System.out.println("Enter the option for operation to perform:");
            int option = sc.nextInt();
            int another_item = 1;
            if (option == 1) {
                System.out.println("Adding products to storage, you must add more than 5 products");
                int number = 1;
                while (another_item==1) {
                    sc.nextLine();
                    System.out.print("Enter item Quantity: ");
                    quantity = sc.nextLine();
                    System.out.print("Enter item description: ");
                    description = sc.nextLine();
                    System.out.print("Enter item price: ");
                    price = sc.nextDouble();
                    Storage newProduct = new Storage(quantity, description, price);
                    products.add(newProduct);
                    System.out.println("\nitem added to cart.\n");
                    number = number + 1;
                    if(number>5)
                    {
                        System.out.println("Add another item.\n1.Yes\n2.N0");
                        System.out.println("Enter option: ");
                        int myoption = sc.nextInt();
                        another_item = myoption;
                    }
                }

            }
            else if (option == 2)
            {
                int another = 1;
                while (another==1) {
                    sc.nextLine();
                    System.out.print("Enter item Quantity: ");
                    quantity = sc.nextLine();
                    System.out.print("Enter item description: ");
                    description = sc.nextLine();
                    System.out.print("Enter item price: ");
                    price = sc.nextDouble();
                    Storage myproduct = new Storage(quantity, description, price);
                    invoice.add(myproduct);
                    System.out.println("\nitem added to cart.\n");
                        System.out.println("Add another item.\n1.Yes\n2.N0");
                        System.out.println("Enter option: ");
                        int myoption = sc.nextInt();
                    another = myoption;

                }

                System.out.println("Dear customer this is your invoice");
                int count = 1;
                for (int i = 0; i < invoice.size(); i++) {
                    System.out.print(count);
                    System.out.println(".\t"+invoice.get(i).toString() + "\n");
                    count = count+1;
                }
            }

            else if (option == 3)
            {
                System.out.println("All the items in storage");
                int count = 1;
                for (int i = 0; i < products.size(); i++) {
                    System.out.print(count);
                    System.out.println(".\t"+products.get(i).toString() + "\n");
                    count = count+1;
                }
            }

            else {
                System.out.println("Invalid option please try again");
            }
            System.out.println("Do you want to perform any other operation\n\t1.Yes\n\t2.No");
            int operation = sc.nextInt();
            terminate_condition = operation;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS