Advertisement
Guest User

lab2

a guest
Oct 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.io.*;
  5. import java.nio.charset.StandardCharsets;
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) {
  10. Room salon = new Room("Salon", 4.00f, 4.00f, 4.00f);
  11. Room kitchen = new Room("Kitchen", 2.00f, 2.00f, 4.00f);
  12. Room bedroom = new Room("Bedroom", 3.00f, 3.00f, 4.00f);
  13. Room[] rooms = new Room[]{salon, kitchen, bedroom};
  14. for (Room room : rooms) {
  15. room.displayInformation();
  16. System.out.println("Painting price: " + room.getPaintingPrice(12.15f) + "PLN");
  17. System.out.println("Floor price: " + room.getFloorPrice(8.15f) + "PLN");
  18. System.out.println();
  19. }
  20. boolean adding = true;
  21. while (adding){
  22. try {
  23. addRoomFromConsoleToFile();
  24. adding = false;
  25. } catch (Exception e) {
  26. System.out.println(e.getMessage());
  27. }
  28. }
  29. }
  30.  
  31. private static void addRoomFromConsoleToFile() throws IOException {
  32. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  33. PrintWriter writer = new PrintWriter("rooms.txt", StandardCharsets.UTF_8);
  34. System.out.println("Give room name:");
  35. String name = validName(reader.readLine());
  36. System.out.println("Give room dimensions in comma-separated list(length,width,height):");
  37. String[] dimensions = validList(reader.readLine().split(","));
  38. System.out.println("Give price for paint:");
  39. float priceForPaint = validPrice(reader.readLine());
  40. System.out.println("Give price for floor:");
  41. float priceForFloor = validPrice(reader.readLine());
  42. Room newRoom = new Room(
  43. name,
  44. Float.parseFloat(dimensions[0]),
  45. Float.parseFloat(dimensions[1]),
  46. Float.parseFloat(dimensions[2])
  47. );
  48. newRoom.writeToFile(writer, priceForPaint, priceForFloor);
  49. }
  50.  
  51. private static String[] validList(String[] list) {
  52. for (String number : list) {
  53. if (Float.parseFloat(number) <= 0.0f) {
  54. throw new IllegalArgumentException("Dimensions must be greater then zero");
  55. }
  56. }
  57. if(list.length != 3){
  58. throw new IllegalArgumentException("Missing dimensions in list");
  59. }
  60. return list;
  61. }
  62.  
  63. private static String validName(String name){
  64. if(name.length() == 0){
  65. throw new IllegalArgumentException("Name is required");
  66. }
  67. return name;
  68. }
  69.  
  70. private static Float validPrice(String price){
  71. if(Float.parseFloat(price)<= 0.0f){
  72. throw new IllegalArgumentException("Price must be greater then zero");
  73. }
  74. return Float.parseFloat(price);
  75. }
  76. }
  77.  
  78. package com.company;
  79.  
  80. import java.io.PrintWriter;
  81.  
  82. class Room {
  83. String name;
  84. float height;
  85. float width;
  86. float length;
  87.  
  88. Room(String name, float length, float width, float height) {
  89. this.name = name;
  90. this.length = length;
  91. this.width = width;
  92. this.height = height;
  93. }
  94.  
  95. float getVolume() {
  96. return this.height * this.width * this.length;
  97. }
  98.  
  99. float getWallsSurface() {
  100. return (this.width * this.height + this.length * this.height) * 2;
  101. }
  102.  
  103. float getFloorSurface() {
  104. return this.width * this.length;
  105. }
  106.  
  107. float getPaintingPrice(float costPerSquareMeter) {
  108. return round(this.getWallsSurface() * costPerSquareMeter);
  109. }
  110.  
  111. float getFloorPrice(float costPerSquareMeter) {
  112. return round(this.getFloorSurface() * costPerSquareMeter);
  113. }
  114.  
  115. private float round(float value) {
  116. return ((int) ((value + 0.005f) * 100)) / 100f;
  117. }
  118.  
  119. void displayInformation() {
  120. System.out.println("Name: " + this.name);
  121. System.out.println("Length: " + this.length + "m");
  122. System.out.println("Width: " + this.width + "m");
  123. System.out.println("Height: " + this.height + "m");
  124. System.out.println("Volumes: " + this.getVolume() + "m3");
  125. System.out.println("Walls surface: " + this.getWallsSurface() + "m2");
  126. System.out.println("Floor surface: " + this.getFloorSurface() + "m2");
  127. }
  128.  
  129. void writeToFile(PrintWriter writer, float priceForPaint, float priceForFloor) {
  130. writer.println("Name: " + this.name);
  131. writer.println("Length: " + this.length + "m");
  132. writer.println("Width: " + this.width + "m");
  133. writer.println("Height: " + this.height + "m");
  134. writer.println("Volumes: " + this.getVolume() + "m3");
  135. writer.println("Walls surface: " + this.getWallsSurface() + "m2");
  136. writer.println("Floor surface: " + this.getFloorSurface() + "m2");
  137. writer.println("Painting price: " + this.getPaintingPrice(priceForPaint) + "PLN");
  138. writer.println("Floor price: " + this.getFloorPrice(priceForFloor) + "PLN");
  139. writer.close();
  140. System.out.println("Added to file");
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement