Question

In: Computer Science

I have 3 classes in the same package called Product, Products and ProductCart. Here are my...

I have 3 classes in the same package called Product, Products and ProductCart. Here are my codes->

Product

public class Product {
        //instance variables
        private int id;
        private String name;
        private double price;
        private int quantity;

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

        //all setters and getters
        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public double getPrice() {
                return price;
        }

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

        public int getQuantity() {
                return quantity;
        }

        public void setQuantity(int quantity) {
                this.quantity = quantity;
        }

        //toString 
        @Override
        public String toString() {
                return id + ", " + name + ", $" + price + ", " + quantity+"pc";
        }

}

Products

import java.util.ArrayList;

public class Products {
        //for holding items
        private ArrayList productList;

        //initializing the products list
        public Products() {
                productList = new ArrayList<>();
        }

        //getter the product list
        public ArrayList getProductList() {
                return productList;
        }
}

ProductCart

public class ProductCart {
        
        //products vairable
        private Products prod;
        
        public ProductCart() {
                prod = new Products();
        }
        
        //adding items
        public void addItems(Product item) {
                prod.getProductList().add(item);
        }
        
        //removing items
        public boolean removeItem(Product item) {
                return prod.getProductList().remove(item);              
        }
        
        //displaying items
        public void displayItem() {
                for(Product item: prod.getProductList())
                        System.out.println(item);
        }
        
        //getting price of the item
        public double getPriceOfAnItem(Product item) {
                int index = prod.getProductList().indexOf(item);
                if(index == -1) {
                        System.out.println("Product not found");
                        return -1;                      
                }
                else
                        return prod.getProductList().get(index).getPrice();
        }

}

Now I want to create class where I ask the user to choose options to perform these tasks->

1. Display Store Products

2. Display Cart

0. Exit

(example if you choose 1 the console will display this-> 1 Chips 40.0 10, 2 Chocolate 60.0 6, 3 Milk 30.0 10)

(example if you choose 2 the console will display this-> 3. Add to Cart 4. Remove From Cart 0. Exit)

This is my code so far but it's not working well because i can not fix "case 4". Please modify it and fix case 4 or write a code that will help me solve the problem (you may write if else statements instead if you want)->

public static void main(String[] args) {
int user_choice = 5;
Scanner sc = new Scanner(System.in);
ProductCart pc = new ProductCart();
  
while (user_choice != 0) {
System.out.println("Please choose an option");
System.out.println("1.Display Store Products 2.Display Cart 3.Add to cart 4.Remove from cart 0.Exit");
user_choice = sc.nextInt();
  
switch(user_choice) {
case 1:
pc.displayItem();
break;
case 2:
System.out.println("3.Add to cart 4.Remove from cart 0.Exit");
break;
case 3:
System.out.println("enter id");
int id = sc.nextInt();
System.out.println("enter name");
String name = sc.next();
System.out.println("Enter price");
double price = sc.nextDouble();
System.out.println("Enter quantity");
int quantity = sc.nextInt();
Product product = new Product(id, name, price, quantity);
pc.addItems(product);
break;
case 4:
System.out.println("enter id");
int id2 = sc.nextInt();
pc.removeItem(product);
break;
case 0:
System.out.println("Finish the operations and exit");
System.out.println("closing now");
System.exit(0);
break;
  
default:
System.out.println("Enter your choice again");
}
}

}

}

Solutions

Expert Solution

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

i have attached my code here and we are supposed to create two classes. one is...
i have attached my code here and we are supposed to create two classes. one is date the other switches accounts for bank and then displays the bank account,type,name,date(pulled from first class and then prints out. i am having issues getting my code to pull from the first class and dont know how to make it read the data from date class if someone can look at my code and tell me how to fix this i would greatly appreciate...
I am trying to create my own doubly linkedlist class and here is what I have...
I am trying to create my own doubly linkedlist class and here is what I have so far. my addfirst is adding elements forever and ever, and I could use some help. This is in C++. Please help me complete my doubly linkedlist class. the addfirst method is what I would like to complete. #ifndef DLLIST_H #define DLLIST_H #include "node.h" #include using namespace std; template class DLList { private:     Node* head = nullptr;     Node* tail = nullptr;    ...
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
I DO NOT have a specific question, my question here is GENERAL HOW TO DETERMINE IF...
I DO NOT have a specific question, my question here is GENERAL HOW TO DETERMINE IF A COMPOUND IS POLAR OR NONPOLAR? I know how to figure about the number of valence electrons and I KNOW how to draw lewis structure, but then I just mess every thing up. I want to know how to determine if it's polar or not. for example: sicl2f2, co2, xef2, xeo4 ... I KNOW how to draw the lewis structure, but this I CANNOT...
I need a paragraph on this: MY TOPIC: Nurses do not have the same right to...
I need a paragraph on this: MY TOPIC: Nurses do not have the same right to free speech as non-nurses.
I need to send my students I am doing distant classes a test How do I...
I need to send my students I am doing distant classes a test How do I do this and have them answer it without the ability for the to keep it or print it out
/* I been trying to solve this problem . this is my own coding /here is...
/* I been trying to solve this problem . this is my own coding /here is the HW INSTRUCTION Let the user to try of maximum 8 times to find the random number For every try display if the user find or not the number and the number of tries At the end of each game display to the user the number of tries to find the number Allow the user to play a maximum of 4 times, by asking...
here i have a dictionary with the words and i have to find the words with...
here i have a dictionary with the words and i have to find the words with the largest size, (i mean word count for eg abdominohysterectomy) which have anagrams , like for example ,loop -> pool. Also , i have to sort them in alphabetical way so what could be the algorithm for that public class Anagrams {    private static final int MAX_WORD_LENGTH = 25;    private static IArray<IVector<String>> theDictionaries =        new Array<>(MAX_WORD_LENGTH);    public static void...
Scenario 3 Media Wise sells a range of media products in package deals. The products sold...
Scenario 3 Media Wise sells a range of media products in package deals. The products sold within bundles are as follows.   TV Stand Speaker Selling Price (£) 400 50 40 Variable Cost (£) 250 30 25 Package A consists of a TV and a stand and is sold for a discounted price of £420. Package B consists of a TV, a stand and two speakers and is sold for a discounted price of £480. Currently packages are sold in a...
Here is my java code. It works and has the correct output, but I need to...
Here is my java code. It works and has the correct output, but I need to add a file and I am not sure how. I cannot use the FileNotFoundException. Please help! import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,0,0}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: "...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT