Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I want to add and delete products from ArrayList java package march11; public in

ID: 3714376 • Letter: I

Question

I want to add and delete products from ArrayList

java


package march11;

public interface productlnterface {

public double computeSalePrice();

public double getRegularPrice();

public void setRegularPrice(double regularPrice);
  
public String toString();
}

////////////////////////////////////

package march11;

public abstract class product implements productlnterface {

private double regularPrice;

public product() {
}

public product(double regularPrice) {
this.regularPrice = regularPrice;
}

@Override
public double computeSalePrice() {
double saleprice;

saleprice = regularPrice + (regularPrice * (10 / 100));

return saleprice;
}

@Override
public void setRegularPrice(double regularPrice) {
this.regularPrice = regularPrice;
}

@Override
public double getRegularPrice() {
return this.regularPrice;
}
  
public abstract String toString();  

}

////////////////////////////////

package march11;

public class dd extends Book {

private int age;

public dd(double regularPrice, String publisher, int yearPublished) {
super(regularPrice, publisher, yearPublished);
this.age = age;
}

public double computeSalePrice() {

double saleprice;

saleprice = this.getRegularPrice() + (this.getRegularPrice() * (15 / 100));

return saleprice;
}

@Override
public String toString() {
return ".Publisher: " + getPublisher() + " Price: " + computeSalePrice()+" YearPublished: "+getYearPublished();
}
}

////////////////////////////////

package march11;

public class TV extends Electronics {

private int size;

public TV(double regularPrice, String manufacturer, int size) {
super(regularPrice, manufacturer);
this.size = size;
}

public double computeSalePrice() {

double saleprice;

saleprice = this.getRegularPrice() + (this.getRegularPrice() * (50 / 100));

return saleprice;
}

@Override
public String toString() {
return ".Name: "+getManufacturer()+" Price: "+computeSalePrice()+" Size: "+size;
}
}

//////////////////////////////////////

package march11;

import java.util.ArrayList;
import java.util.Scanner;

import march12.ProductInterface.*;

public class Main {

ArrayList<product> market = new ArrayList<product>();

private int ch = 0;
private int p = 0;

public Main() {

menu();

}

public void startScreen() {

System.out.println("Welcome to the market. ................................");

System.out.println("1. Display Store Products");

System.out.println("2. Display Cart");

System.out.println("0. Exit");
}

public void storeProductsMenu() {

System.out.println(" 1. Add to Cart");

System.out.println("2. Remove From Cart");

System.out.println("0. Exit");

}

private int getUserInput() {

Scanner in = new Scanner(System.in);

System.out.print("Your choice:");

ch = Integer.parseInt(in.nextLine());
System.out.println(" ");
return ch;

}

public void TrLoop() {
if (p == 2) {
System.out.println("-----------------------BYE!----------------------- ");
System.out.println("$$$$$$$$$$$$Newton passed from here$$$$$$$$$$$$");
System.exit(0);
} else {
p++;
System.out.println("************************ " + "Invalid c1`hoica");
System.out.println("Try again :( " + (p) + " ) " + "************************ ");
}
}

private void displayStoreProducts() {//????? ???????

product TV1 = new TV(1000, "Samsung", 30);
product TV2 = new TV(2000, "Sony", 50);

product MP3Player = new MP3Player(250, "Apple", "blue");

product ChildrenBook = new ChildrenBook(15, "Pee Wee press", 1987, 8);

product Cartoon = new Cartoon(14, "Pee Wee press", 1924, "Batman");

product[] pa = {TV1, TV2, MP3Player, ChildrenBook, Cartoon};

int i = 1;
for (product pr : pa) {
System.out.println(i + pr.toString());
i++;
}
}

public void menu() {

do {

startScreen();

getUserInput();

switch (ch) {

case 1:

displayStoreProducts();
storeProductsMenu();

getUserInput();

// innerChoice1();
break;

case 2:

// showCart();
break;

case 0:

System.exit(0);

break;

default:
TrLoop();
break;

}

} while (ch != 0);
} // ???? ????? ???????? ??????

private void innerChoice1() {

switch (ch) {

case 1:

// addProductToCart();
// showCart();
break;

case 2:

// removeProductFromCart();
break;

default:

break;

}

}

private void addProductToCart() {
int pid = getUserInput() - 1;
// market.add(1);
}

}

////////////////////////////////////

package march11;

public class MP3Player extends Electronics {

String color;

public MP3Player(double regularPrice, String manufacturer, String color) {
super(regularPrice, manufacturer);
this.color = color;
}

public double computeSalePrice() {

double saleprice;

saleprice = this.getRegularPrice() + (this.getRegularPrice() * (40 / 100));

return saleprice;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

@Override
public String toString() {
return ".Name: "+getManufacturer()+" Price: "+computeSalePrice()+" Color: "+color;
}
}

////////////////////////////////

package march11;

public interface ElectronicsInterface extends productlnterface {

public String getManufacturer();
}

///////////////

package march11;

public abstract class Electronics extends product implements ElectronicsInterface {

private String manufacturer;

public Electronics(double regularPrice, String manufacturer) {// ??? ?????? ? ?????
super(regularPrice);
this.manufacturer = manufacturer;
}

@Override
public String getManufacturer() {//??????
return manufacturer;
}

public void setManufacturer(String manufacturer) {//
this.manufacturer = manufacturer;
}

@Override
public abstract String toString();

}

/////////////////////////////////

package march11;

public class ChildrenBook extends Book {

private int age;

public ChildrenBook(double regularPrice, String publisher, int yearPublished, int age) {
super(regularPrice, publisher, yearPublished);
this.age = age;
}

public double computeSalePrice() {

double saleprice;

saleprice = this.getRegularPrice() + (this.getRegularPrice() * (15 / 100));

return saleprice;
}

@Override
public String toString() {
return ".Publisher: " + getPublisher() + " Price: " + computeSalePrice()+" YearPublished: "+getYearPublished();
}
}

//////////////////////////////

package march11;

public class Cartoon extends Book {

private String characterName;

public Cartoon(double regularPrice, String publisher, int yearPublished, String characterName) {
super(regularPrice, publisher, yearPublished);
this.characterName = characterName;
}

public double computeSalePrice() {
double saleprice;

saleprice = this.getRegularPrice() + (this.getRegularPrice() * (5 / 100));

return saleprice;
}

@Override
public String toString() {
return ".Publisher: " + getPublisher() + " Price: " + computeSalePrice()+" YearPublished: "+getYearPublished()+" CharacterName: "+characterName;
}
}

//////////////////////////////

package march11;

public interface BookInterfacee extends productlnterface {//????? ??????

public String getPublisher();

public void setPublisher(String publisher);

public int getYearPublished();

public void setYearPublished(int yearPublished);

}

//////////////////////////////

package march11;

public abstract class Book extends product implements BookInterfacee {

private String publisher;
private int yearPublished;

public Book(double regularPrice, String publisher, int yearPublished) {
super(regularPrice);
this.publisher = publisher;
this.yearPublished = yearPublished;
}

@Override
public String getPublisher() {
return publisher;
}

@Override
public void setPublisher(String publisher) {
this.publisher = publisher;
}

public int getYearPublished() {
return yearPublished;
}

public void setYearPublished(int yearPublished) {
this.yearPublished = yearPublished;
}

public double computeSalePrice() {

double saleprice;

saleprice = this.getRegularPrice() + (this.getRegularPrice() * (20 / 100));

return saleprice;
}

@Override
public abstract String toString() ;

}

///////////////////////////////////

package march11;

public class Application {

public static void main(String[] args) {
new Main();
}

}

Explanation / Answer

There is so much of the code in your question, so that it is actually little difficult to put all pieces together and run the,. However, i think that you have you code in place, so i will suggest you how to make the code changes so that you achieve the functionality you intend for...

So You have a scanner object, through which you ask user for input. you have used it in getUserInput() function.

in market class:

ArrayList<product> market = new ArrayList<product>();

Above is the arraylist, which store the products. The Product is basically a interface which is implemented through other interfaces(ElectronicsInterface, BookInterfacee), which are implemented by different concrete classes. They can be defined like below:

Product -> Electronics -> TV & MP3Player

Product -> Book -> Cartoon, dd and ChildrenBook

So in short, there are 5 concrete classes which extend Product abstract class. Now you can add any of them into the arrayList.

So, you need to give the User a choice Menu on what do they want to add:

1. TV

2. MP3Player

3. Cartoon

4. ChildrenBook

5. DD

Once user select a choice, then based on appropriate constructor of that class, ask them for other values... like if user chooses 3. Cartoon, then that constructor is like below:

public Cartoon(double regularPrice, String publisher, int yearPublished, String characterName) {

So it means, we need price, published, year and characterName. Take these values from user and create a Cartoon like below:

Product cartoonProduct = new Cartoon(regularPrice, publisher, yearPublished, characterName);

then add this to the Product arrayList like below:

market.add(cartoonProduct);

//////////////////////////////////////////////

To Delete the product, you can iterate on The product list like below, and display Users all the products:

for(int i=0; i<market.size(); i++) {

System.out.println(i + ": " + market.get(i));

}

This will print list of products with numbers 1, 2, 3.. Then ask user which product number they want to delete..

Based on the user input, You can use below code to remove product..

market.remove(index);

I have tried to explain you how you approach a problem, as this is very long problem.. so whenever you face a issue, please ask in comments and i will help. Thanks!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote