I’m trying to apply object orientation to a Java program

Asked

Viewed 165 times

-4

I’m trying to apply object orientation to this program and I wanted to start with the exchange of these ifs, 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

  • 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.

  • 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

  • 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).

1 answer

4

People want to do object-oriented, but have no idea what that is. They actually learn fragments of what OOP is and try to apply it. Usually you get a monster and you get the worst of both worlds.

Object orientation is not panacea and only works if the programmer knows how to organize the code very well and understand deeply why OO is used, what advantages it will bring doing so, which has the ability to see when something is wrong, when OO there is not suitable.

Object-oriented programming is difficult and even experienced programmers often make mistakes. It is even more wrong when there is a lack of understanding of the imperative before trying OO. If it is to do OO wrong, it is better not to do it at all. If not to see a clear benefit is not worth the effort.

My informal estimate is that over 90 percent of programmers think and talk about object-oriented programming, but they don’t even realize they’re not actually doing it. It has a minority who do OO at various levels of quality. So I don’t understand why people insist on trying something they never understand how to do.

I already say that the imperative can be better than what is written there and that the attempt to leave object oriented left the code more confused than if it made more procedural.

The first problem with this code is that it does a lot of different things within a single class. It is the main, it is a menu, it is the CRUD, it is the data repository, it is the business rule, IE, none of this code is object oriented already in the base.

No use eliminating a few ifs that the code will not magically be object oriented. You can delete the if and will not be anything object oriented. You can create a map, but the gain in readability and maintainability is usually zero, it can even be negative.

It is possible to use a rather complex logic to create new options in the menu without tampering with the code, just adding novelty. It’s not worth doing this, it’s not worth it if the project had half a million lines of code and was run by a team of dozens of people and had news almost every day, which is where the object orientation shines.

But for whom? To call private things? It makes no sense.

I do not understand why it has protected members. There may be some reason, but it seems that it was placed so randomly.

I don’t understand this class Pizzas (the name should be singular because it represents only a pizza) makes within this class.

The encapsulation of this last class is bad. And I question if it is necessary. Clearly it was made as cake recipe.

I’m sorry, but the proper refactoring in this code is to start over. Or try to do something less ambitious.

  • You can see that you have some knowledge. I would like to see a simple code of yours, with this concept of OO. It would have some git ?

  • 1

    @Raonibz Maniero is legendary, just open your profile and open the linked github =) Almost all questions he also keeps a copy there in his repository made only for future reference of the answers that Maniero makes available here

  • I did not know! kkkk I took a look and found this: https://answall.com/q/237154/101, but wanted something a little bigger to understand more specifically the concept of OO as quoted. I think it would make a great article.

  • And this code I did not apply OO, I just want to try to apply this code, which was asked by my supervisor this way, with these methods, and to do what he is already doing, so far so good, however, he is very ugly and I want to change it mainly this if, I think you misunderstood me

  • 1

    I want to find time to do a project open with the right concepts. I do OOP since the 80’s, until hj error. And I always evolve something. So no article will solve anything. That’s the problem, today it seems that everyone wants magic formulas. You have to read many books, of various currents of thought, see the general debates, the specifics, see good codes and understand every detail, see what goes wrong, ask a lot, ask for specific help, not try to learn everything at once. Mainly understand the imperative 100% before going to OO.

  • The rest I’ve already said in the answer, re-read it.

  • I get it, it’s a very difficult thing that even if I study 40 years I won’t totally master

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.