I am having trouble with this code in Eclipse Java Oxygen. Product Class: package org; // Create abstract class Product public abstract class Product {
I am having trouble with this code in Eclipse Java Oxygen.
Product Class:
package org;
// Create abstract class Product
public abstract class Product {
public String name;
public Product() {
}
public Product(String name) {
this.name = name;
}
public Product(Product p) {
this.name=p.name;
}
@Override
public String toString() {
return "Product [name=" + name + "]";
}
}
Snack Class:
package org;
// Create abstract class Snack which extends Product
public abstract class Snack extends Product {
public double weight;
public double price;
public Snack() {
super();
}
public Snack(double weight, double price) {
super();
this.weight = weight;
this.price = price;
}
public Snack(Snack s) {
super();
this.weight = s.weight;
this.price = s.price;
}
@Override
public String toString() {
return "Snack [weight=" + weight + ", price=" + price + "]";
}
}
Drink Class:
package org;
//Create class Drink which extends Product Class
public class Drink extends Product{
public String type;
public double price;
public Drink() {
super();
}
public Drink(double price) {
super();
this.price = price;
}
public Drink(Drink d) {
super();
this.price = d.price;
}
@Override
public String toString() {
return "Drink [type=" + type + ", price=" + price + "]";
}
}
Candy Class:
package org;
// Create class Candy which extends Snack Class
public class Candy extends Snack{
public int calories;
public Candy() {
super();
}
public Candy(int calories) {
super();
this.calories = calories;
}
public Candy(Candy c) {
super();
this.calories = c.calories;
}
@Override
public String toString() {
return "Candy [calories=" + calories + "]";
}
}
Chips Class:
package org;
//Create class Chips which extends Snack Class
public class Chips extends Snack {
public String contents;
public Chips() {
super();
}
public Chips(String contents) {
super();
this.contents = contents;
}
public Chips(Chips c) {
super();
this.contents = c.contents;
}
@Override
public String toString() {
return "Chips [contents=" + contents + "]";
}
}
Gum Class:
package org;
// Create class Gum which extends Snack Class
public class Gum extends Snack {
public String flavour;
public Gum() {
super();
}
public Gum(String flavour) {
super();
this.flavour = flavour;
}
public Gum(Gum g) {
super();
this.flavour = g.flavour;
}
@Override
public String toString() {
return "Gum [flavour=" + flavour + "]";
}
}
Dispenser Class:
package org;
import java.util.Scanner;
public class Dispenser {
public static int numberOfProducts=0;
public static Product[] p=new Product[100]; // Initializing an array of Products
Dispenser(Product pr)
{
p[numberOfProducts]=pr; // adding products to the product array
numberOfProducts++;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int num;
boolean valid=true;
Product p = null;
Scanner sc=new Scanner(System.in);
while(valid) // Read the values from user until user enters 5
{
System.out.println("Enter which product you need to create 1.Drink 2.Candy 3.Chips 4.Gum Enter 5 to exit");
num=sc.nextInt(); // Read the values from the user
if(num==1)
{
p=new Drink(); // Creating a Drink object when user value is 1
Dispenser d=new Dispenser(p);
System.out.println("Product Added");
}
else if(num==2)
{
p=new Candy(); // Creating a Candy object when user value is 2
Dispenser d=new Dispenser(p);
System.out.println("Product Added");
}
else if(num==3)
{
p=new Chips(); // Creating a Chips object when user value is 3
Dispenser d=new Dispenser(p);
System.out.println("Product Added");
}
else if(num==4)
{
p=new Gum();
Dispenser d=new Dispenser(p); // Creating a Gum object when user value is 1
System.out.println("Product Added");
}
else if(num==5)
{
valid=false; // Exit the console from reading user input
}
else
{
System.out.println("Enter valid number"); // Entering wrong input
}
}
displayProducts(); // calling methods which will display the Products added
}
private static void displayProducts() {
// TODO Auto-generated method stub
int i=0;
System.out.println("The products added are");
while(i { System.out.println(p[i].toString()); // displaying the product names i++; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started