Advertisement
StefiIOE

Untitled

Oct 21st, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class MyException extends Exception{
  4. private String showMessage;
  5. // parametrized constructor
  6. public MyException(String showMessage) {
  7. this.showMessage=showMessage;
  8. }
  9. //default constructor
  10. public MyException (){}
  11. @Override
  12. public String toString(){
  13. return "Exception\n";
  14. }
  15. }
  16.  
  17.  
  18. class Trip {
  19.  
  20. protected String Name ;
  21. protected int price;
  22.  
  23. public Trip(String Name, int price) {
  24. // super();
  25. this.Name = Name;
  26. this.price = price;
  27. }
  28.  
  29. public String getName() {
  30. return this.Name;
  31. }
  32.  
  33. public void setName(String Name) {
  34. this.Name = Name;
  35. }
  36.  
  37. public int getPrice() {
  38. return this.price;
  39. }
  40.  
  41. public void setPrice(int price) {
  42. this.price = price;
  43. }
  44.  
  45. public int vratiVremeVoDenovi(){
  46. return 0 ;
  47. }
  48.  
  49.  
  50. @Override
  51. public String toString() {
  52. return Name;
  53. }
  54.  
  55. static public int minPrice(Trip[] nizaPatuvanje, Trip holiday){
  56. int min = 100000;
  57. for(Trip trip : nizaPatuvanje){
  58. if(holiday.vratiVremeVoDenovi() < trip.vratiVremeVoDenovi()){
  59. if(trip.getPrice() < min ){
  60. min = trip.getPrice();
  61. }
  62. }
  63. }
  64. return min ;
  65. }
  66.  
  67. }
  68.  
  69.  
  70. class FestiveTrip extends Trip
  71. {
  72. private int startDay;
  73. private int startMonth;
  74. private int endDay;
  75. private int endMonth;
  76.  
  77.  
  78. public FestiveTrip(String Name, int price , int startDay, int startMonth, int endDay, int endMonth) {
  79. super(Name, price);
  80. try{
  81.  
  82. if(startDay + startMonth *30 > endDay + endMonth*30)
  83. throw new Exception ("Exception");
  84. }
  85. catch (Exception e){
  86. String showMessage = e.getMessage();
  87. System.out.println(showMessage);
  88. int tmp = startDay;
  89. startDay = endDay;
  90. endDay = tmp;
  91.  
  92. tmp = startMonth;
  93. startMonth = endMonth;
  94. endMonth = tmp;
  95. }
  96. this.startDay = startDay;
  97. this.endDay = endDay;
  98. this.startMonth = startMonth;
  99. this.endMonth = endMonth;
  100. }
  101.  
  102. public int getStartDay() {
  103. return this.startDay;
  104. }
  105.  
  106. public void setStartDay(int startDay) {
  107. this.startDay = startDay;
  108. }
  109.  
  110. public int getEndDay() {
  111. return this.endDay;
  112. }
  113.  
  114. public void setEndDay(int endDay) {
  115. this.endDay = endDay;
  116. }
  117.  
  118. public int getStartMonth() {
  119. return this.startMonth;
  120. }
  121.  
  122. public void setStartMonth(int startMonth) {
  123. this.startMonth = startMonth;
  124. }
  125.  
  126. public int getEndMonth() {
  127. return this.endMonth;
  128. }
  129.  
  130. public void setEndMonth(int endMonth) {
  131. this.endMonth = endMonth;
  132. }
  133. public int vratiVremeVoDenovi (){
  134. return (endDay + endMonth *30) - (startDay + startMonth * 30);
  135. }
  136.  
  137.  
  138. }
  139.  
  140. class Vacation extends Trip{
  141. private int days;
  142.  
  143.  
  144. public Vacation(String Name, int price ,int days) {
  145. super(Name, price - 1000);
  146. this.days = days;
  147. }
  148.  
  149.  
  150. public int getDays() {
  151. return this.days;
  152. }
  153.  
  154. public void setDays(int days) {
  155. this.days = days;
  156. }
  157. public int vratiVremeVoDenovi() {
  158. return days;
  159. }
  160.  
  161. }
  162. public class Test {
  163.  
  164.  
  165. public static void main(String[] args) {
  166.  
  167.  
  168. int n;
  169. Scanner in=new Scanner(System.in);
  170. n=in.nextInt();
  171.  
  172. Trip nizaPatuvanje[]=new Trip[n];
  173.  
  174. for (int i=0;i<n;i++){
  175. int tip=in.nextInt();
  176. if (tip==0){
  177. String ime=in.next();
  178. int cena =in.nextInt();
  179. int vreme=in.nextInt();
  180. nizaPatuvanje[i]=new Vacation(ime,cena,vreme);
  181. }
  182. else {
  183. String ime=in.next();
  184. int cena =in.nextInt();
  185. int pocD=in.nextInt();
  186. int pocM=in.nextInt();
  187. int krajD=in.nextInt();
  188. int krajM=in.nextInt();
  189. nizaPatuvanje[i]=new FestiveTrip(ime,cena,pocD,pocM, krajD,krajM);
  190.  
  191. }
  192. }
  193.  
  194. //решение на барање 1
  195. StringBuffer sb = new StringBuffer();
  196. for(Trip trip : nizaPatuvanje){
  197. if(trip instanceof FestiveTrip){
  198. if(((FestiveTrip)trip).getStartMonth () == 6){
  199. sb.append(trip.toString());
  200. sb.append(" ");
  201. }
  202. }
  203. }
  204. System.out.println(sb.toString());
  205. sb.delete(0, sb.length());
  206.  
  207. //решение на барање 2
  208.  
  209. double sum = 0 ;
  210. for(Trip trip : nizaPatuvanje){
  211.  
  212. sum+=trip.vratiVremeVoDenovi();
  213.  
  214. }
  215. double average = sum /nizaPatuvanje.length;
  216. System.out.println(average);
  217. //решение на барање 3
  218.  
  219. String nameVacation = in.next();
  220. int priceVacation = in.nextInt();
  221. int daysVacation = in.nextInt();
  222. Vacation holiday = new Vacation(nameVacation, priceVacation, daysVacation);
  223. //решение на барање 4
  224. System.out.println(Trip.minPrice(nizaPatuvanje,holiday));
  225.  
  226.  
  227. }
  228.  
  229. }
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement