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

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 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 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: "...
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...
If I have 2.5mL of KCl and 3 mL of water. Which one is my solvent?...
If I have 2.5mL of KCl and 3 mL of water. Which one is my solvent? _____________________________ Which one is my solute? ______________________________
Hi, I am doing up my homework on mathematics and here are some questions. I want...
Hi, I am doing up my homework on mathematics and here are some questions. I want to cross-reference my answers thank you. Decide if the statements below are True or False. Please include some rough workings for me to understand: (1) Mr. Tan borrowed $500,000 from Bank XYZ at 5% annual interest to be repaid monthly over 20 years. The amount that he pays back to XYZ each month is between $3000 to $3500. (2) Continuing from (1): after 15...
Here is my fibonacci code using pthreads. When I run the code, I am asked for...
Here is my fibonacci code using pthreads. When I run the code, I am asked for a number; however, when I enter in a number, I get my error message of "invalid character." ALSO, if I enter "55" as a number, my code automatically terminates to 0. I copied my code below. PLEASE HELP WITH THE ERROR!!! #include #include #include #include #include int shared_data[10000]; void *fibonacci_thread(void* params); void parent(int* numbers); int main() {    int numbers = 0; //user input....
I have a high definition antennae mounted on my house in my backyard. As I don’t...
I have a high definition antennae mounted on my house in my backyard. As I don’t exactly have a high ladder, it is mounted quite low, and we don’t receive many channels. The current height is 12 feet. My friend has offered to let me borrow a 15 foot high ladder. It is recommended to mount the lad- der at a 76 degree angle to the ground to prevent it from sliding out. I will be mounting the antennae 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT