Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class MyException extends Exception{
- private String showMessage;
- // parametrized constructor
- public MyException(String showMessage) {
- this.showMessage=showMessage;
- }
- //default constructor
- public MyException (){}
- @Override
- public String toString(){
- return "Exception\n";
- }
- }
- class Trip {
- protected String Name ;
- protected int price;
- public Trip(String Name, int price) {
- // super();
- this.Name = Name;
- this.price = price;
- }
- public String getName() {
- return this.Name;
- }
- public void setName(String Name) {
- this.Name = Name;
- }
- public int getPrice() {
- return this.price;
- }
- public void setPrice(int price) {
- this.price = price;
- }
- public int vratiVremeVoDenovi(){
- return 0 ;
- }
- @Override
- public String toString() {
- return Name;
- }
- static public int minPrice(Trip[] nizaPatuvanje, Trip holiday){
- int min = 100000;
- for(Trip trip : nizaPatuvanje){
- if(holiday.vratiVremeVoDenovi() < trip.vratiVremeVoDenovi()){
- if(trip.getPrice() < min ){
- min = trip.getPrice();
- }
- }
- }
- return min ;
- }
- }
- class FestiveTrip extends Trip
- {
- private int startDay;
- private int startMonth;
- private int endDay;
- private int 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 showMessage = e.getMessage();
- System.out.println(showMessage);
- int tmp = startDay;
- startDay = endDay;
- endDay = tmp;
- tmp = startMonth;
- startMonth = endMonth;
- endMonth = tmp;
- }
- this.startDay = startDay;
- this.endDay = endDay;
- this.startMonth = startMonth;
- this.endMonth = endMonth;
- }
- public int getStartDay() {
- return this.startDay;
- }
- public void setStartDay(int startDay) {
- this.startDay = startDay;
- }
- public int getEndDay() {
- return this.endDay;
- }
- public void setEndDay(int endDay) {
- this.endDay = endDay;
- }
- public int getStartMonth() {
- return this.startMonth;
- }
- public void setStartMonth(int startMonth) {
- this.startMonth = startMonth;
- }
- public int getEndMonth() {
- return this.endMonth;
- }
- public void setEndMonth(int endMonth) {
- this.endMonth = endMonth;
- }
- public int vratiVremeVoDenovi (){
- return (endDay + endMonth *30) - (startDay + startMonth * 30);
- }
- }
- 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 this.days;
- }
- public void setDays(int days) {
- this.days = days;
- }
- public int vratiVremeVoDenovi() {
- return days;
- }
- }
- public class Test {
- public static void main(String[] args) {
- int n;
- Scanner in=new Scanner(System.in);
- n=in.nextInt();
- Trip nizaPatuvanje[]=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();
- nizaPatuvanje[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();
- nizaPatuvanje[i]=new FestiveTrip(ime,cena,pocD,pocM, krajD,krajM);
- }
- }
- //решение на барање 1
- StringBuffer sb = new StringBuffer();
- for(Trip trip : nizaPatuvanje){
- if(trip instanceof FestiveTrip){
- 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 : nizaPatuvanje){
- sum+=trip.vratiVremeVoDenovi();
- }
- double average = sum /nizaPatuvanje.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(nizaPatuvanje,holiday));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement