Разработка приложения для организации работы турагентства
Скачать 306.71 Kb.
|
private ArrayList public TravelAgency() { tours = new ArrayList hotels = new ArrayList requests = new ArrayList } public ArrayList return tours; } public ArrayList return hotels; } public ArrayList return requests; } public Tour getTour(String name) { for (Tour tour : tours) { if (tour.getTitle().equals(name)) { return tour; } } return null; } public Hotel getHotel(String name) { for (Hotel hotel : hotels) { if (hotel.getTitle().equals(name)) { return hotel; } } return null; } public void addTour(String title, String country, Integer price, String hotel, Boolean isFire) { tours.add(new Tour(title, country, price, getHotel(hotel), isFire)); } public void addHotel(String title, Integer stars) { hotels.add(new Hotel(title, stars)); } public ArrayList ArrayList for (Tour tour : this.tours) { if (tour.getCountry().equals(country)) { tours.add(tour); } } return tours; } public ArrayList ArrayList for (Tour tour : this.tours) { if (tour.getPrice() <= price) { tours.add(tour); } } return tours; } public ArrayList ArrayList for (Tour tour : this.tours) { if (tour.isFire()) { tours.add(tour); } } return tours; } public ArrayList ArrayList for (Hotel hotel : this.hotels) { if (hotel.getStars() == stars) { hotels.add(hotel); } } return hotels; } public void createRequest(String tourTitle, Integer count) { requests.add(new Request(tourTitle, count, count * getTour(tourTitle).getPrice())); } @Override public String toString() { final StringBuilder sb = new StringBuilder("\nTravelAgency{"); sb.append("\ntours=").append(tours); sb.append(", \nhotels=").append(hotels); sb.append(", \nrequests=").append(requests); sb.append('}'); return sb.toString(); } } package TravelAgency; import java.io.Serializable; public class Request implements Serializable { private static final long serialVersionUID = 2L; private String tourTitle; private Integer count; private Integer totalCost; public Request(String tourTitle, Integer count, Integer totalCost) { this.tourTitle = tourTitle; this.count = count; this.totalCost = totalCost; } public String getTourTitle() { return tourTitle; } public Integer getCount() { return count; } public Integer getTotalCost() { return totalCost; } @Override public String toString() { final StringBuilder sb = new StringBuilder("\nRequest{"); sb.append("\ntourTitle='").append(tourTitle).append('\''); sb.append(", \ncount=").append(count); sb.append(", \ntotalCost=").append(totalCost); sb.append('}'); return sb.toString(); } } package TravelAgency; import java.io.Serializable; public class Tour implements Serializable { private static final long serialVersionUID = 2L; private String title; private String country; private Integer price; private Hotel hotel; private Boolean isFire; public Tour(String title, String country, Integer price, Hotel hotel, Boolean isFire) { this.title = title; this.country = country; this.price = price; this.hotel = hotel; this.isFire = isFire; } public String getTitle() { return title; } public String getCountry() { return country; } public Integer getPrice() { return price; } public Hotel getHotel() { return hotel; } public Boolean isFire() { return isFire; } @Override public String toString() { final StringBuilder sb = new StringBuilder("\nTour{"); sb.append("\ntitle='").append(title).append('\''); sb.append(", \ncountry='").append(country).append('\''); sb.append(", \nprice=").append(price); sb.append(", \nhotel=").append(hotel); sb.append(", \nisFire=").append(isFire); sb.append('}'); return sb.toString(); } } package TravelAgency; import java.io.Serializable; public class Hotel implements Serializable{ private static final long serialVersionUID = 1L; private String title; private Integer stars; public Hotel(String title, Integer stars) { this.title = title; this.stars = stars; } public String getTitle() { return title; } public Integer getStars() { return stars; } @Override public String toString() { final StringBuilder sb = new StringBuilder("\nHotel{"); sb.append("title='").append(title).append('\''); sb.append(", stars=").append(stars); sb.append('}'); return sb.toString(); } } package TravelAgency; import java.io.*; import java.util.Random; import java.util.Scanner; public class Main { private static final String BINARY_FILE_NAME = "travelAgency.out"; private static TravelAgency travelAgency; private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); travelAgency = new TravelAgency(); try { loadFromBinaryFile(); } catch (Exception exc) { generateTestData(); } System.out.println("Турагенство"); main: while (true) { System.out.println("\nВведите номер команды"); System.out.println("" + "1 - Показать все туры\n" + "2 - Показать туры по стране\n" + "3 - Показать туры по цене\n" + "4 - Показать горящие туры\n" + "5 - Показать все отели\n" + "6 - Показать отели по количеству звезд\n" + "7 - Подать заявку на тур\n" + "8 - Показать все запросы\n" + "9 - Сохранить изменения\n" + "0 - Выход\n"); switch (sc.next()) { case "1": travelAgency.getTours().forEach(System.out::println); break; case "2": System.out.println("Введите страну"); travelAgency.getToursByCountry(sc.next()).forEach(System.out::println); break; case "3": System.out.println("Введите максимальную цену"); travelAgency.getToursByPrice(sc.nextInt()).forEach(System.out::println); break; case "4": travelAgency.getToursByFire().forEach(System.out::println); break; case "5": travelAgency.getHotels().forEach(System.out::println); break; case "6": System.out.println("Введите желаемое количество звезд"); travelAgency.getHotelsByStars(sc.nextInt()).forEach(System.out::println); break; case "7": System.out.println("Введите название тура, и желаемое количество путевок"); travelAgency.createRequest(sc.next(), sc.nextInt()); break; case "8": travelAgency.getRequests().forEach(System.out::println); break; case "9": saveToBinaryFile(); break; case "0": break main; default: System.out.println("Неверная команда"); } } } private static void saveToBinaryFile() { FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(BINARY_FILE_NAME); oos = new ObjectOutputStream(fos); oos.writeObject(travelAgency); } catch (IOException e) { System.err.println("Error while store to binary file"); // e.printStackTrace(); } finally { try { oos.flush(); oos.close(); } catch (IOException ex) { System.err.println("Error while trying close IO"); // ex.printStackTrace(); } } } private static void loadFromBinaryFile() { try { FileInputStream fis = new FileInputStream(BINARY_FILE_NAME); ObjectInputStream oin = new ObjectInputStream(fis); travelAgency = (TravelAgency) oin.readObject(); } catch (FileNotFoundException ex) { System.out.println("Generate test"); generateTestData(); } catch (IOException e) { e.printStackTrace(); System.out.println("Generate test"); generateTestData(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static void generateTestData() { Random r = new Random(); for (int i = 0; i < 10; i++) { travelAgency.addHotel("Hotel_"+i,r.nextInt(4)+1); } for (int i = 0; i < 10; i++) { travelAgency.addTour("Tour_"+i,"Country_"+r.nextInt(10),r.nextInt(100000),travelAgency.getHotels().get(r.nextInt(travelAgency.getHotels().size())).getTitle(),r.nextBoolean()); } for (int i = 0; i < 5; i++) { travelAgency.createRequest(travelAgency.getTours().get(r.nextInt(travelAgency.getTours().size())).getTitle(),r.nextInt(4)+1); } } } |