N
N
ne_nuzhen2020-11-27 21:10:22
Java
ne_nuzhen, 2020-11-27 21:10:22

How to write add and output methods using Vector?

Hello, there was such a problem that I do not know how to write methods for adding elements and their output using the Vector class. I have 4 fields (colour, name, price, year), I need them to be written to the Vector class in the add method, and then in the print method, some element by index from this vector should be output. In main, a menu item is selected, if, for example, the 1st item is selected, then the user enters data, and then using the add method they are added to the Vector, and when the user displays some element of the structure, then all 4 fields are displayed. Below is the main class and the class with fields. Thanks in advance for help!

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

public class vectorExample{

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        var vector = new Vector<Car>();





        String colour;
        String name;
        String price;
        int priceInt = 0;
        String year;
        int yearInt = 0;
        boolean exit = true;
        int menuItem;
        String StringMenu;
        String[] array;
        while(exit){
            System.out.println("Выберите пункт меню: ");
            System.out.println("1. Ввод элемента ");
            System.out.println("2. Вывод элемента");
            System.out.println("3. Вывод структуры");
            System.out.println("4. Выход");
            StringMenu = reader.readLine();
            array = StringMenu.split(" ");
            if (array.length > 1 || array[0].contains("\t")) {
                System.out.println("Ошибка! Введены буквы и/или пробел");
                System.out.println("------------------------------------------- \n");
            } else {
                try {
                    menuItem = Integer.parseInt(array[0]);
                    if(menuItem == 1){
                        System.out.println("\n--------------------------------\n");
                        boolean Enter = true;
                        System.out.println("Введите цвет транспорта:");
                        colour = reader.readLine();
                        //colourMachine.add(colour);
                        System.out.println("Введите марку транспорта:");
                        name = reader.readLine();
                        //nameMachine.add(name);
                        while (Enter) {
                            System.out.println("Введите цену транспорта:");
                            price = reader.readLine();
                            priceInt = Integer.parseInt(price);
                            if (priceInt < 0) {
                                System.out.println("--------------------------------");
                                System.out.println("Цена не может быть отрицательной");
                                System.out.println("--------------------------------\n");
                                System.out.println("Введите цену еще раз");
                                Enter = true;
                            } else {
                                Enter = false;
                            }
                        }
                        Enter = true;
                        while (Enter){
                            System.out.println("Введите год выпуска транспорта:");
                            year = reader.readLine();
                            yearInt = Integer.parseInt(year);
                            if (yearInt < 0) {
                                System.out.println("--------------------------------");
                                System.out.println("Год не может быть отрицательным");
                                System.out.println("--------------------------------\n");
                                System.out.println("Введите год еще раз");
                                Enter = true;
                            } else {
                                Enter = false;
                            }
                        }
                        System.out.println("\n--------------------------------\n");

                        vector.add(new Car(colour, name, priceInt, yearInt));

                        exit = true;
                    } else if(menuItem == 2){
                        System.out.println("вывод одного элемента");
                        String index = reader.readLine();
                        int indexInt = Integer.parseInt(index);
                        var carFromVector = vector.get(indexInt);
                        System.out.println("Элемент "+ indexInt +"\n" + carFromVector);
                        exit = true;
                    } else if(menuItem == 3){
                        System.out.println("вывод структуры");
                        exit = true;
                    } else if(menuItem == 4){
                        exit = false;

                    }
                    if (menuItem < 1 || menuItem > 4) {
                        System.out.println("Ошибка! Вы вышли из диапазона допустимых значений");
                        System.out.println("-------------------------------------------\n");
                        exit = true;
                    }
                } catch (NumberFormatException var14) {
                    System.out.println("Ошибка! Введены буквы и/или пробел1");
                    System.out.println("-------------------------------------------\n");

                    exit = true;
                }
            }
        }
    }

    static class Car{
        private final String colour;
        private final String name;
        private final int price;
        private final int year;

        public  Car(String colour, String name, int price, int year){
            this.colour = colour;
            this.name = name;
            this.price = price;
            this.year = year;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2020-11-27
@ne_nuzhen

It will be strange if the car contains a collection of cars.
Better declare a Vector in the VectorExample class. For example, like this: var vector = new Vector<Car>();
To add a car to vector: vector.add(new Car(..., ..., ..., ...));
To get it by index:var carFromVector = vector.get(0);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question