Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package OpinionPoll.Vehicle_Catalogue;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader reader =
- new BufferedReader(
- new InputStreamReader(
- System.in));
- List<Vehicle> vehiclesList = new ArrayList<>();
- System.out.println("Enter vehicle");
- String input = reader.readLine();
- while (!"End".equalsIgnoreCase(input)) {
- String[] data = input.split("\\s+");
- String type = data[0];
- String model = data[1];
- String colour = data[2];
- Integer price = Integer.parseInt(data[3]);
- Vehicle vehicle = new Vehicle(type, model, colour, price);
- vehiclesList.add(vehicle);
- System.out.println("Enter vehicle");
- input = reader.readLine();
- }
- System.out.println("Enter model");
- String model = reader.readLine();
- while (!"Close catalogue".equalsIgnoreCase(model)) {
- String finalModel = model;
- vehiclesList.stream()
- .filter(v -> v.getModel().equalsIgnoreCase(finalModel))
- .forEach(System.out::println);
- System.out.println("Enter model");
- model = reader.readLine();
- }
- System.out.println(
- String.format("The average price of the cars is %.2f",
- averagePrice(vehiclesList.stream()
- .filter(v -> v.getType()
- .equalsIgnoreCase("car"))
- .collect(Collectors.toList()))));
- System.out.println(
- String.format("The average price of our trucks is %.2f.",
- averagePrice(vehiclesList.stream()
- .filter(v -> v.getType()
- .equalsIgnoreCase("truck"))
- .collect(Collectors.toList()))));
- }
- public static double averagePrice(List<Vehicle> vehicles) {
- double sum = 0;
- if (vehicles.size() == 0) {
- return 0;
- }
- for (Vehicle vehicle : vehicles) {
- sum += vehicle.getPrice();
- }
- return sum / vehicles.size();
- }
- }
- package OpinionPoll.Vehicle_Catalogue;
- public class Vehicle {
- private String type;
- private String model;
- private String colour;
- private Integer price;
- public Vehicle(String type, String model, String colour, Integer price) {
- this.type = type;
- this.model = model;
- this.colour = colour;
- this.price = price;
- }
- public String getType() {
- return type;
- }
- public String getModel() {
- return model;
- }
- public String getColour() {
- return colour;
- }
- public Integer getPrice() {
- return price;
- }
- @Override
- public String toString() {
- return String.format("Type %s: \nModel: %s\ncolour: %s\nPrice: %d",
- this.getType(),
- this.getModel().toUpperCase().charAt(0)
- + getModel().substring(1) ,
- this.getColour(),
- this.getPrice());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement