Advertisement
existentiamm

Programowanie obiektowe Main, Cars, Person, Garage

Mar 23rd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.69 KB | None | 0 0
  1. ************************************MAIN**********************************************
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. package javaapplication1;
  8. import Paczka.Car;
  9. import Paczka.Garage;
  10. import Paczka.Person;
  11. import java.io.IOException;
  12.  
  13. /**
  14. *
  15. * @author Karolina
  16. */
  17. public class JavaApplication1 {
  18.  
  19.  
  20. /**
  21. * @param args the command line arguments
  22. */
  23. public static void main(String[] args) throws IOException, CloneNotSupportedException {
  24. // TODO code application logic here
  25.  
  26. Car car1 = new Car();
  27.  
  28. car1.details();
  29. car1.setBrand("Fiat");
  30. car1.setModel("126p");
  31. car1.setDoorCount(2);
  32. car1.setEngineVolume(650);
  33. car1.setAvgConsump(6.0);
  34. car1.setRegistrationNumber("KR12345");
  35. car1.details();
  36. Car car2 = new Car("Syrena", "105", 2, 0.8f, 7.6d, "WE1234");
  37. System.out.println(car2);
  38. double routeConsumption = car2.calculateConsump(500);
  39. System.out.println(String.format("Route consumption: %f L", routeConsumption));
  40. double routeCost = car2.calculateCost(500, 5);
  41. System.out.println(String.format("Route cost: %f", routeCost));
  42. Car.displayCarCount();
  43. System.out.println("==================Copy References Example ==========================");
  44. Car car3 = car2;
  45. car3.details();
  46. car3.setModel("OPEL");
  47. car3.details();
  48. car2.details();
  49. System.out.println("==================Copy objects Example ==========================");
  50. Car car4 = car2.clone();
  51. car4.setModel("AUDI");
  52. car2.details();
  53. car4.details();
  54. System.out.println("================== GARAGE ==========================");
  55. Garage garage1 = new Garage();
  56. garage1.setAddress("ul. Garażowa 1");
  57. garage1.setCapacity(1);
  58. //garage1.carEnter(car4);
  59. garage1.details();
  60.  
  61. System.out.println("================== Class with list field ==========================");
  62. Person.setMaxCarCount(3);
  63. Person person1 = new Person();
  64. person1.setFirstName("Jan");
  65. person1.setLastName("Kowalski");
  66. person1.addCarRegistrationNumber("SCZ 12345");
  67. person1.addCarRegistrationNumber("SC 9863");
  68. person1.addCarRegistrationNumber("SCZ 37893");
  69. person1.addCarRegistrationNumber("SCZ 4912");
  70. person1.details();
  71. Person person2 = new Person("Piotr", "Nowak");
  72. person2.addCarRegistrationNumber("DW 9866");
  73. person2.details();
  74. person2.removeCarRegistrationNumber("DW 9866");
  75. person2.details();
  76. person2.removeCarRegistrationNumber("DW 9866");
  77. }
  78. }
  79.  
  80.  
  81. *********************PERSON***********************************************
  82. /*
  83. * To change this license header, choose License Headers in Project Properties.
  84. * To change this template file, choose Tools | Templates
  85. * and open the template in the editor.
  86. */
  87. package Paczka;
  88.  
  89. import java.util.ArrayList;
  90. import java.util.List;
  91.  
  92. /**
  93. *
  94. * @author Karolina
  95. */
  96. public class Person {
  97. private static int maxCarCount;
  98. private String firstName;
  99. private String lastName;
  100. private List<String> registrationNumbers = new ArrayList<String>();
  101. /////!!!!!!!!! List<String> myList = new ArrayList<String>();
  102.  
  103. public Person(){
  104. firstName = "brak";
  105. lastName= "brak";
  106. }
  107. public Person(String firstName, String lastName){
  108. this.firstName = firstName;
  109. this.lastName = lastName;
  110. }
  111.  
  112. public static int getMaxCarCount() {
  113. return maxCarCount;
  114. }
  115.  
  116. public static void setMaxCarCount(int maxCarCount) {
  117. Person.maxCarCount = maxCarCount;
  118. }
  119.  
  120. public String getFirstName() {
  121. return firstName;
  122. }
  123.  
  124. public void setFirstName(String firstName) {
  125. this.firstName = firstName;
  126. }
  127.  
  128. public String getLastName() {
  129. return lastName;
  130. }
  131.  
  132. public void setLastName(String lastName) {
  133. this.lastName = lastName;
  134. }
  135.  
  136. public List<String> getRegistrationNumbers() {
  137. return registrationNumbers;
  138. }
  139.  
  140. public void setRegistrationNumbers(List<String> registrationNumbers) {
  141. this.registrationNumbers = registrationNumbers;
  142. }
  143.  
  144. @Override
  145. public String toString() {
  146. return "Person{" + "firstName=" + firstName + ", lastName=" + lastName + ", registrationNumbers=" + registrationNumbers + '}';
  147. }
  148.  
  149. public void details(){
  150. System.out.println(toString());
  151. }
  152.  
  153. public void addCarRegistrationNumber(String registrationNumer){
  154. registrationNumbers.add(registrationNumer);
  155. System.out.println(registrationNumer);
  156.  
  157. }
  158.  
  159. public void removeCarRegistrationNumber(String registrationNumer) {
  160. registrationNumbers.remove(registrationNumer);
  161. }
  162.  
  163. }//koniec klasy Person
  164. ****************************CARS*********************************
  165. package Paczka;
  166.  
  167.  
  168. /*
  169. * To change this license header, choose License Headers in Project Properties.
  170. * To change this template file, choose Tools | Templates
  171. * and open the template in the editor.
  172. */
  173.  
  174. /**
  175. *
  176. * @author Karolina
  177. */
  178. public class Car implements Cloneable {
  179. private String brand;
  180. private String model;
  181. private int doorCount;
  182. private float engineVolume;
  183. private double avgConsump;
  184. private String registrationNumber;
  185. private static int carCount;
  186. private int liczba;
  187.  
  188. public Car(){
  189. brand = "brak";
  190. model = "brak";
  191. doorCount = 0;
  192. engineVolume = 0;
  193. avgConsump = 0;
  194. registrationNumber = "brak";
  195. carCount ++;
  196. }
  197.  
  198. public Car(String brand, String model, int doorCount, float engineVolume, double avgConsump, String registrationNumber) {
  199. this.brand = brand;
  200. this.model = model;
  201. this.doorCount = doorCount;
  202. this.engineVolume = engineVolume;
  203. this.avgConsump = avgConsump;
  204. this.registrationNumber = registrationNumber;
  205. carCount ++;
  206.  
  207. }
  208.  
  209. @Override
  210. public Car clone() throws CloneNotSupportedException {
  211. return (Car)super.clone(); //To change body of generated methods, choose Tools | Templates.
  212. }
  213.  
  214. //gettery i settery
  215. public String getBrand() {
  216. return brand;
  217.  
  218. }
  219.  
  220. public void setBrand(String brand) {
  221. this.brand = brand;
  222. }
  223.  
  224. public String getModel() {
  225. return model;
  226. }
  227.  
  228. public void setModel(String model) {
  229. this.model = model;
  230. }
  231.  
  232. public int getDoorCount() {
  233. return doorCount;
  234. }
  235.  
  236. public void setDoorCount(int doorCount) {
  237. this.doorCount = doorCount;
  238. }
  239.  
  240. public float getEngineVolume() {
  241. return engineVolume;
  242. }
  243.  
  244. public void setEngineVolume(float engineVolume) {
  245. this.engineVolume = engineVolume;
  246. }
  247.  
  248. public double getAvgConsump() {
  249. return avgConsump;
  250. }
  251.  
  252. public void setAvgConsump(double avgConsump) {
  253. this.avgConsump = avgConsump;
  254. }
  255.  
  256. public String getRegistrationNumber() {
  257. return registrationNumber;
  258. }
  259.  
  260. public void setRegistrationNumber(String registrationNumber) {
  261. this.registrationNumber = registrationNumber;
  262. }
  263.  
  264. public static int getCarCount() {
  265. return carCount;
  266. }
  267.  
  268. public static void setCarCount(int carCount) {
  269. Car.carCount = carCount;
  270. }
  271.  
  272. @Override
  273. public String toString() {
  274. return "Car{" + "brand=" + brand + ", model=" + model + ", doorCount=" + doorCount + ", engineVolume=" + engineVolume + ", avgConsump=" + avgConsump + ", registrationNumber=" + registrationNumber + '}';
  275. }
  276.  
  277. public double calculateConsump(double roadLength){
  278. return (roadLength/100)*avgConsump;}
  279.  
  280. public double calculateCost(double roadLength,double petrolCost){
  281. return calculateConsump(roadLength)* petrolCost; }
  282.  
  283.  
  284. public void details(){
  285. System.out.println (toString());
  286. }
  287.  
  288. public static void displayCarCount(){
  289. System.out.println(carCount);
  290. }
  291.  
  292.  
  293. }//koniec klasy Car
  294. *******************************************GARAGE***********************
  295. package Paczka;
  296. import Paczka.Car;
  297. /**
  298. *
  299. * @author Karolina
  300. */
  301. public class Garage implements Cloneable {
  302. private Car[] cars;
  303. private String address;
  304. private int capacity;
  305.  
  306. public Garage(){ }
  307.  
  308. public Garage( String address, int capacity) {
  309. this.address = address;
  310. this.capacity = capacity;
  311. }
  312.  
  313. public Car[] getCars() {
  314. return cars;
  315. }
  316.  
  317. public void setCars(Car[] cars) {
  318. this.cars = cars;
  319. }
  320.  
  321. public String getAddress() {
  322. return address;
  323. }
  324.  
  325. public void setAddress(String address) {
  326. this.address = address;
  327. }
  328.  
  329. public int getCapacity() {
  330. return capacity;
  331. }
  332.  
  333. public void setCapacity(int capacity) {
  334. this.capacity = capacity;
  335. }
  336.  
  337. @Override
  338. public String toString() {
  339. return "Garage{" + "cars=" + cars + ", address=" + address + ", capacity=" + capacity + '}';
  340. }
  341.  
  342. public void details(){
  343. System.out.println(toString());
  344. }
  345.  
  346. @Override
  347. protected Garage clone() throws CloneNotSupportedException {
  348. return (Garage)super.clone(); //To change body of generated methods, choose Tools | Templates.
  349. }
  350.  
  351. /* public void carEnter(Car car) {
  352. //kurwa nie wiem
  353. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  354. }
  355. public void carLeave(){
  356. //kuerw tego też nie wiem i wiem, ze ta ma być Car , ale żeby niewywalalo błędu
  357. }; */
  358.  
  359.  
  360. /* public int carCount(){
  361. return System.out.println();
  362. //a ki ci tam wiee
  363. }*/
  364.  
  365.  
  366. }// koniec klasy Garage
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement