Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.nio.charset.StandardCharsets;
- public class Main {
- public static void main(String[] args) {
- Room salon = new Room("Salon", 4.00f, 4.00f, 4.00f);
- Room kitchen = new Room("Kitchen", 2.00f, 2.00f, 4.00f);
- Room bedroom = new Room("Bedroom", 3.00f, 3.00f, 4.00f);
- Room[] rooms = new Room[]{salon, kitchen, bedroom};
- for (Room room : rooms) {
- room.displayInformation();
- System.out.println("Painting price: " + room.getPaintingPrice(12.15f) + "PLN");
- System.out.println("Floor price: " + room.getFloorPrice(8.15f) + "PLN");
- System.out.println();
- }
- boolean adding = true;
- while (adding){
- try {
- addRoomFromConsoleToFile();
- adding = false;
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- }
- private static void addRoomFromConsoleToFile() throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- PrintWriter writer = new PrintWriter("rooms.txt", StandardCharsets.UTF_8);
- System.out.println("Give room name:");
- String name = validName(reader.readLine());
- System.out.println("Give room dimensions in comma-separated list(length,width,height):");
- String[] dimensions = validList(reader.readLine().split(","));
- System.out.println("Give price for paint:");
- float priceForPaint = validPrice(reader.readLine());
- System.out.println("Give price for floor:");
- float priceForFloor = validPrice(reader.readLine());
- Room newRoom = new Room(
- name,
- Float.parseFloat(dimensions[0]),
- Float.parseFloat(dimensions[1]),
- Float.parseFloat(dimensions[2])
- );
- newRoom.writeToFile(writer, priceForPaint, priceForFloor);
- }
- private static String[] validList(String[] list) {
- for (String number : list) {
- if (Float.parseFloat(number) <= 0.0f) {
- throw new IllegalArgumentException("Dimensions must be greater then zero");
- }
- }
- if(list.length != 3){
- throw new IllegalArgumentException("Missing dimensions in list");
- }
- return list;
- }
- private static String validName(String name){
- if(name.length() == 0){
- throw new IllegalArgumentException("Name is required");
- }
- return name;
- }
- private static Float validPrice(String price){
- if(Float.parseFloat(price)<= 0.0f){
- throw new IllegalArgumentException("Price must be greater then zero");
- }
- return Float.parseFloat(price);
- }
- }
- package com.company;
- import java.io.PrintWriter;
- class Room {
- String name;
- float height;
- float width;
- float length;
- Room(String name, float length, float width, float height) {
- this.name = name;
- this.length = length;
- this.width = width;
- this.height = height;
- }
- float getVolume() {
- return this.height * this.width * this.length;
- }
- float getWallsSurface() {
- return (this.width * this.height + this.length * this.height) * 2;
- }
- float getFloorSurface() {
- return this.width * this.length;
- }
- float getPaintingPrice(float costPerSquareMeter) {
- return round(this.getWallsSurface() * costPerSquareMeter);
- }
- float getFloorPrice(float costPerSquareMeter) {
- return round(this.getFloorSurface() * costPerSquareMeter);
- }
- private float round(float value) {
- return ((int) ((value + 0.005f) * 100)) / 100f;
- }
- void displayInformation() {
- System.out.println("Name: " + this.name);
- System.out.println("Length: " + this.length + "m");
- System.out.println("Width: " + this.width + "m");
- System.out.println("Height: " + this.height + "m");
- System.out.println("Volumes: " + this.getVolume() + "m3");
- System.out.println("Walls surface: " + this.getWallsSurface() + "m2");
- System.out.println("Floor surface: " + this.getFloorSurface() + "m2");
- }
- void writeToFile(PrintWriter writer, float priceForPaint, float priceForFloor) {
- writer.println("Name: " + this.name);
- writer.println("Length: " + this.length + "m");
- writer.println("Width: " + this.width + "m");
- writer.println("Height: " + this.height + "m");
- writer.println("Volumes: " + this.getVolume() + "m3");
- writer.println("Walls surface: " + this.getWallsSurface() + "m2");
- writer.println("Floor surface: " + this.getFloorSurface() + "m2");
- writer.println("Painting price: " + this.getPaintingPrice(priceForPaint) + "PLN");
- writer.println("Floor price: " + this.getFloorPrice(priceForFloor) + "PLN");
- writer.close();
- System.out.println("Added to file");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement