Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class MyException extends Exception {
- private String msg;
- public MyException(String msg) {
- this.msg = msg;
- }
- public MyException() {
- }
- @Override
- public String toString() {
- return "Exception\n";
- }
- }
- class Trip {
- protected String name;
- protected int price;
- public Trip(String name, int price) {
- this.name = name;
- this.price = price;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getPrice() {
- return price;
- }
- public void setPrice(int price) {
- this.price = price;
- }
- public int returnTimeInDays() {
- return 0;
- }
- @Override
- public String toString() {
- return name;
- };
- static public int minPrice(Trip[] trips, Trip holiday) {
- int min = 1000000;
- for (Trip trip : trips) {
- if (holiday.returnTimeInDays() < trip.returnTimeInDays()) {
- if (trip.getPrice() < min) {
- min = trip.getPrice();
- }
- }
- // System.out.println(holiday.getPrice() + " " + trip.getPrice() + " " + min);
- }
- return min;
- }
- }
- class Vacation extends Trip {
- private int days;
- public Vacation(String name, int price, int days) {
- super(name, price - 1000);
- this.days = days;
- }
- public int getDays() {
- return days;
- }
- public void setDays(int days) {
- this.days = days;
- }
- public int returnTimeInDays() {
- return days;
- }
- }
- class FestiveTrip extends Trip {
- private int startDay, startMonth, endDay, endMonth;
- public FestiveTrip(String name, int price, int startDay, int startMonth, int endDay, int endMonth) {
- super(name, price);
- try {
- if (startDay + startMonth * 30 > endDay + endMonth * 30) {
- throw new Exception("Exception");
- }
- } catch (Exception e) {
- String msg = e.getMessage();
- System.out.println(msg);
- int temp = startDay;
- startDay = endDay;
- endDay = temp;
- temp = startMonth;
- startMonth = endMonth;
- endMonth = temp;
- }
- this.startDay = startDay;
- this.startMonth = startMonth;
- this.endDay = endDay;
- this.endMonth = endMonth;
- }
- public int returnTimeInDays() {
- return (endDay + endMonth * 30) - (startDay + startMonth * 30);
- }
- public int getStartDay() {
- return startDay;
- }
- public void setStartDay(int startDay) {
- this.startDay = startDay;
- }
- public int getStartMonth() {
- return startMonth;
- }
- public void setStartMonth(int startMonth) {
- this.startMonth = startMonth;
- }
- public int getEndDay() {
- return endDay;
- }
- public void setEndDay(int endDay) {
- this.endDay = endDay;
- }
- public int getEndMonth() {
- return endMonth;
- }
- public void setEndMonth(int endMonth) {
- this.endMonth = endMonth;
- };
- }
- public class Test {
- public static void main(String[] args) {
- int n;
- Scanner in = new Scanner(System.in);
- n = in.nextInt();
- Trip trips[] = new Trip[n];
- for (int i = 0; i < n; i++) {
- int tip = in.nextInt();
- if (tip == 0) {
- String ime = in.next();
- int cena = in.nextInt();
- int vreme = in.nextInt();
- trips[i] = new Vacation(ime, cena, vreme);
- } else {
- String ime = in.next();
- int cena = in.nextInt();
- int pocD = in.nextInt();
- int pocM = in.nextInt();
- int krajD = in.nextInt();
- int krajM = in.nextInt();
- trips[i] = new FestiveTrip(ime, cena, pocD, pocM, krajD, krajM);
- }
- }
- // решение на барање 1
- StringBuffer sb = new StringBuffer();
- for (Trip trip : trips) {
- if (trip instanceof FestiveTrip) {
- //sb.append(" instanceof " + ((FestiveTrip) trip).getStartMonth());
- if (((FestiveTrip) trip).getStartMonth() == 6) {
- sb.append(trip.toString());
- sb.append(" ");
- }
- }
- }
- System.out.println(sb.toString());
- sb.delete(0, sb.length());
- // решение на барање 2
- double sum = 0;
- for (Trip trip : trips) {
- /*
- System.out.println(trip.toString() +" "+ trip.returnTimeInDays());
- if (trip instanceof FestiveTrip) {
- System.out.println(((FestiveTrip) trip).getStartMonth() +" | "+((FestiveTrip) trip).getEndMonth());
- }
- */
- sum += trip.returnTimeInDays();
- }
- double average = sum / trips.length;
- System.out.println(average);
- // решение на барање 3
- String nameVacation = in.next();
- int priceVacation = in.nextInt();
- int daysVacation = in.nextInt();
- Vacation holiday = new Vacation(nameVacation, priceVacation, daysVacation);
- // решение на барање 4
- System.out.println(Trip.minPrice(trips, holiday));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement