I'm trying to apply object orientation to a program in Java

-4

I'm trying to apply object orientation to this program and I wanted to start by swapping these if s, does anyone have any tips on 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();
    }

}
    
asked by anonymous 03.10.2017 / 21:07

1 answer

4

People want to do object-oriented, but have no idea what that is. In fact they learn fragments of what OOP is and try to apply. In general there is a monstrengo and if you swear 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 by doing so, that is able to see when something is wrong, when OO is not there appropriate.

Object-oriented programming is difficult and even experienced programmers often make a lot of mistakes. Even more so when there is no understanding of the imperative before attempting the OO. If it is to do wrong OO it is better not to do it. If it is not to see a clear benefit is not worth the effort.

My informal estimate is that more than 90 percent of programmers find and say that they program object-oriented, but they can not even imagine that they are not actually doing it. It has a minority that does OO in several levels of quality. So I do not understand why the staff insists on trying something they never understand how to do.

I already added that the imperative may be better than it is written there and that the attempt to leave object-oriented left the code more confusing than if it were more procedural.

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

It's no use eliminating some% s with% s that the code will not be magically object-oriented. You can delete if and there will be nothing object oriented. You can create a map, but the gain in readability and maintainability is usually zero, may be even negative.

It is possible to create a very complex logic to allow you to create new menu options without touching the code, just adding newness. This is not worth doing, it does not even matter 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 does not make sense.

I do not understand why you have protected members. There may be some reason, but it looks like it was randomly placed.

I do not understand this class if (the name should be unique because it represents only a pizza) does within this class.

The encapsulation of this last class is poor. 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.

    
03.10.2017 / 21:53