-4
I’m trying to apply object orientation to this program and I wanted to start with the exchange of these if
s, does anyone have any hint of what to put in place?
import java.util.*;
public class MenuPizza {
protected static Scanner l;
private boolean execute;
protected List<Pizzas> pizzas;
public static void main(String[] args) {
new MenuPizza();
}
public MenuPizza() {
l = new Scanner(System.in);
execute = true;
pizzas = new ArrayList<Pizzas>();
System.out.println("Olá\n" + "Seja bem-vindo ao nosso menu de pizzas");
while (execute) {
String opcao = menu();
if (opcao.equalsIgnoreCase("1")) {
inserirPizza();
} else if (opcao.equals("2")) {
listarPizza();
} else if (opcao.equals("3")) {
removerPizza();
} else if (opcao.equals("4")) {
alterarPizza();
} else if (opcao.equals("5")) {
exibirMenu();
} else if (opcao.equals("6")) {
execute = false;
} else {
System.out.println("\nOpção Inválida\n");
}
}
}
private static String menu() {
System.out.println("");
System.out.println("1. Inserir Pizzas");
System.out.println("2. Listar Pizzas");
System.out.println("3. Remover Pizzas");
System.out.println("4. Alterar Preço da Pizza");
System.out.println("5. Exibir Menu de Opções");
System.out.println("6. Sair");
System.out.println("\r\nDigite uma das opções :");
return l.nextLine();
}
public class Pizzas {
public String sabor;
public String preço;
public Pizzas() {
}
public String getSabor() {
return sabor;
}
public void setSabor(String sabor) {
this.sabor = sabor;
}
public String getPreço() {
return preço;
}
public void setPreço(String preço) {
this.preço = preço;
}
}
private void inserirPizza() {
boolean inserindo = true;
while (inserindo) {
System.out.println("\r\nInserir Pizzas");
Pizzas d = new Pizzas();
d.setSabor(textInput("\r\nDigite o sabor da pizza: "));
d.setPreço(textInput("\nDigite o preço da pizza: "));
String inserir = textInput("\nAdicionar pizza ? (S/N)");
if (inserir.equalsIgnoreCase("s")) {
System.out.println("\r\nPizza adicionada ao menu");
pizzas.add(d);
}
else if (inserir.equalsIgnoreCase("n")) {
System.out.println("\r\nPizza não adicionada");
}
else {
System.out.println("\nOpção inválida\n");
}
inserindo = false;
}
}
private void listarPizza() {
if (pizzas.size() == 0) {
System.out.println("\r\nAinda não há pizzas adicionadas\n");
} else {
System.out.println("\r\nListar Pizzas\n");
for (Pizzas pizzas2 : pizzas) {
System.out.println("Sabor: " + pizzas2.getSabor());
System.out.println("Preço: " + pizzas2.getPreço() + "\n");
}
}
}
private void removerPizza() {
listarPizza();
String remover = textInput("Digite o sabor da pizza que deseja remover: ");
for (Pizzas pizzas2 : pizzas) {
if (pizzas2.sabor.equalsIgnoreCase(remover)) {
remover += pizzas.remove(pizzas2);
System.out.println("\nPizza removida\n");
listarPizza();
}
}
}
private void alterarPizza() {
listarPizza();
String sabor = textInput("Digite o sabor da pizza que deseja alterar o preço: ");
String novoPreço = textInput("\nDigite o novo preço: ");
for (Pizzas pizzas2 : pizzas) {
if (pizzas2.getSabor().equalsIgnoreCase(sabor)) {
pizzas2.setPreço(novoPreço);
System.out.println("\nPreço alterado\n");
listarPizza();
}
}
}
private void exibirMenu() {
menu();
}
private String textInput(String fim) {
System.out.println(fim);
return l.nextLine();
}
}
I know almost nothing of java, but in C, it is wrong to use
Ç
for variable name– isaque
Java "Ç" is not a problem. Your code is all disfigured, this obvious that you took a code made in a structured language, and played inside a JAVA solution and broke everything.
– Gabriel Coletta
I read all the answers here. Maniero’s response was so exceptional that it made me want to read hundreds of books about OOP. But as it seems you want at all costs just replace the if/Else try to read about the switch, I’m just a student too and do not know if it is possible to use the switch with a String. I did a quick read on Google and it seems possible yes using the hashcode() method; I found something on oracle documentation and that [doubt in stackoverflow](https://stackoverf
– Lucas
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).
– Maniero