Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm working on a project and im posting what i have so far, however I i don't know how to create a loop to traverse

I'm working on a project and im posting what i have so far, however I i don't know how to create a loop to traverse through the collection structure and modify, return values from the iteration. Can you help me with this?

Requirements for project:

4 Pillars of OOP

Inheritance, Polymorphism (overloading or overriding), Encapsulation, Abstraction (Abstract class or Interface)

Must create and use at least 3 Constructors

Must create and invoke at least four methods.

the main method does not count towards the method count

Two methods must not be getter or setter methods

Use a Scanner to get responses from the user

Must contain a switch statement

Must handle at least one exception

The project must include at least 3 Classes

Use one of the Collections structures.

ArrayList, HashSet, Queue, Stack, LinkedList, HashMap (One of these Data Structures will count)

Use a loop to traverse through the Collection structure you chose and modify, organize or return values from the iteration.

My Code I have so far;

Main.java

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

public class Main {

// main method public static void main(String[] args) {

// Creating arraylist to keep pizza ArrayList pizza = new ArrayList<>();

// Creating pizza reg crust using different constructors PizzaRegCrust1 PizzaRegCrust1 =new PizzaRegCrust1("Cheese Pizza"); PizzaRegCrust2 PizzaRegCrust2 = new PizzaRegCrust2("Pepperoni Pizza");

// Creating pizza thin crust using different constructors PizzaThinCrust1 PizzaThinCrust1 = new PizzaThinCrust1("Cheese Pizza"); PizzaThinCrust2 PizzaThinCrust2 = new PizzaThinCrust2("Pepperoni Pizza");

// Adding pizza regCrust and thinCrust to arraylist pizza.add(PizzaRegCrust1); pizza.add(PizzaRegCrust2); pizza.add(PizzaThinCrust1); pizza.add(PizzaThinCrust2);

// Creating scanner object to read input from user Scanner scanner = new Scanner(System.in);

// Showing option to choose System.out.println("Choose what pizza you want to create: "); System.out.println("1. Regular crust pizza"); System.out.println("2. Thin crust pizza"); System.out.println("Enter your choice (1 or 2): "); System.out.println("Enter your topping (1. cheese or 2. pepperoni): "); try { // Creating pizza after reading choice from user Pizza newPizza = createPizza(scanner.nextLine(), scanner); pizzas.add(newPizza); // adding newly created pizza to arraylist

} catch (Exception ex) { // catching exception if any exception occurred during pizza creation System.out.println("Exception handled: " + ex.getMessage()); }

// Printing details of all pizzas by iterating over the arraylist System.out.println("Printing all pizzas.. "); for (int i = 0; i < pizzas.size(); i++) { Pizza pizza = pizzas.get(i); // getting pizza by index pizza.displayDetails(); // displaying details System.out.println(); // printing a new line }

private static Pizza createPizza(String choice, Scanner scanner) throws Exception { switch (choice) { case "1": { System.out.println("Enter selection: ");

Int selection1 = scanner.nextInt(); PizzaRegCrust1 = new Pizza1 ("Pizza Crust Selected"); Int selection2 = scanner.nextInt(); PizzaRegCrust2 = new Pizza2 ("Pizza Topping Selected"); return PizzaRegCrust; } case "2": { System.out.println("Enter selection: "); Int selection3 = scanner.nextInt(); PizzaThinCrust1 = new Pizza3 ("Pizza Crust Selected"); Int selection4 = scanner.nextInt(); PizzaThinCrust2 = new Pizza4 ("Pizza Topping Selected"); return PizzaThinCrust; } default: throw new Exception("Invalid selection"); }

Pizza.java

// Abstract class Pizza public abstract class Pizza {

// instance variables protected String crusts; protected String toppings;

// Constructor 1 public Pizza() { this.crust = "Regular"; this.topping = "Cheese"; }

// Constructor 2 public Pizza() { this.crust = "Regular"; this.topping = "Pepperoni"; }

// Constructor 3 public Pizza() { this.crust = "Thin"; this.topping = "Cheese"; } // Constructor 4 public Pizza() { this.crust = "Thin"; this.topping = "Pepperoni"; }

// Getters and Setters

public String getCrust() { return crust; }

public void setCrust(String crust) { this.crust = crust; }

public String getTopping() { return topping; }

public void setTopping(String topping) { this.topping = topping; }

// Method to display details public void displayDetails() {

System.out.println("Crust: " + getCrust()); System.out.println("Topping: " + getTopping());

}

} PizzaRegCrust1.java

public class PizzaRegCrust1 extends Pizza { public PizzaRegCrust1() { super("Regular", "Cheese", "Pepperoni"); this.pizza = "Regular"; this.pizza = "Cheese"; this.pizza = "Pepperoni"; }

}

// Implementing abstract method, //@Override

PizzaRegCrust2.java

public class PizzaRegCrust2 extends Pizza { public PizzaRegCrust2() { super("Regular", "Cheese", "Pepperoni"); this.pizza = "Regular"; this.pizza = "Cheese"; this.pizza = "Pepperoni"; }

}

// Implementing abstract method, //@Override

PizzaThinCrust1.java

public class PizzaThinCrust1 extends Pizza { public PizzaThinCrust1() { super("Thin", "Cheese", "Pepperoni"); this.pizza = "Thin"; this.pizza = "Cheese"; this.pizza = "Pepperoni"; }

}

// Implementing abstract method, //@Override

PizzaThinCrust2.java

public class PizzaThinCrust2 extends Pizza { public PizzaThinCrust2() { super("Thin", "Cheese", "Pepperoni"); this.pizza = "Thin"; this.pizza = "Cheese"; this.pizza = "Pepperoni"; }

} // Implementing abstract method, //@Override

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Samsung Galaxy S23 Ultra Comprehensive User Manual

Authors: Leo Scott

1st Edition

B0BVPBJK5Q, 979-8377286455

More Books

Students also viewed these Databases questions

Question

a. Describe the encounter. What made it intercultural?

Answered: 1 week ago