Created
April 11, 2013 06:16
-
-
Save Howard29/5361140 to your computer and use it in GitHub Desktop.
Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mangocity.tmc.hotel.controller; | |
import com.mangocity.tmc.hotel.model.SearchCriteria; | |
import com.mangocity.tmc.hotel.service.BookingService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.ui.Model; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.servlet.mvc.support.RedirectAttributes; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: heyunhao | |
* Date: 13-4-9 | |
* Time: 下午1:24 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
@Controller | |
public class HotelController { | |
private BookingService bookingService; | |
@Autowired | |
public void setBookingService(BookingService bookingService) { | |
this.bookingService = bookingService; | |
} | |
@RequestMapping(value = "/hotels/search", method = RequestMethod.GET) | |
public void search(SearchCriteria searchCriteria, Model model) { | |
model.addAttribute(bookingService.findBookings(searchCriteria)); | |
} | |
@RequestMapping(value = "/hotels", method = RequestMethod.GET) | |
public String list(SearchCriteria searchCriteria, Model model,RedirectAttributes redirect) { | |
model.addAttribute(bookingService.findHotels(searchCriteria)); | |
return "redirect:hotels/list"; | |
} | |
@RequestMapping(value = "/",method = RequestMethod.GET) | |
public String index(){ | |
return "index"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mangocity.tmc.hotel.model; | |
public enum Amenity { | |
OCEAN_VIEW, LATE_CHECKOUT, MINIBAR; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mangocity.tmc.hotel.model; | |
import java.io.Serializable; | |
import java.math.BigDecimal; | |
import java.text.DateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.Set; | |
/** | |
* A Hotel Booking made by a User. | |
*/ | |
public class Booking implements Serializable { | |
private int id; | |
private User user; | |
private Hotel hotel; | |
private Date checkinDate; | |
private Date checkoutDate; | |
private String creditCard; | |
private String creditCardName; | |
private int creditCardExpiryMonth; | |
private int creditCardExpiryYear; | |
private boolean smoking; | |
private int beds; | |
private Set<Amenity> amenities; | |
public Booking() { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.add(Calendar.DAY_OF_MONTH, 1); | |
setCheckinDate(calendar.getTime()); | |
calendar.add(Calendar.DAY_OF_MONTH, 1); | |
setCheckoutDate(calendar.getTime()); | |
} | |
public Booking(Hotel hotel, User user) { | |
this(); | |
this.hotel = hotel; | |
this.user = user; | |
} | |
public BigDecimal getTotal() { | |
return hotel.getPrice().multiply(new BigDecimal(getNights())); | |
} | |
public int getNights() { | |
if (checkinDate == null || checkoutDate == null) { | |
return 0; | |
} else { | |
return (int) ((checkoutDate.getTime() - checkinDate.getTime()) / 1000 / 60 / 60 / 24); | |
} | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public Date getCheckinDate() { | |
return checkinDate; | |
} | |
public void setCheckinDate(Date datetime) { | |
this.checkinDate = datetime; | |
} | |
public Hotel getHotel() { | |
return hotel; | |
} | |
public void setHotel(Hotel hotel) { | |
this.hotel = hotel; | |
} | |
public User getUser() { | |
return user; | |
} | |
public void setUser(User user) { | |
this.user = user; | |
} | |
public Date getCheckoutDate() { | |
return checkoutDate; | |
} | |
public void setCheckoutDate(Date checkoutDate) { | |
this.checkoutDate = checkoutDate; | |
} | |
public String getCreditCard() { | |
return creditCard; | |
} | |
public void setCreditCard(String creditCard) { | |
this.creditCard = creditCard; | |
} | |
public String getDescription() { | |
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM); | |
return hotel == null ? null : hotel.getName() + ", " + df.format(getCheckinDate()) + " to " | |
+ df.format(getCheckoutDate()); | |
} | |
public boolean isSmoking() { | |
return smoking; | |
} | |
public void setSmoking(boolean smoking) { | |
this.smoking = smoking; | |
} | |
public int getBeds() { | |
return beds; | |
} | |
public void setBeds(int beds) { | |
this.beds = beds; | |
} | |
public String getCreditCardName() { | |
return creditCardName; | |
} | |
public void setCreditCardName(String creditCardName) { | |
this.creditCardName = creditCardName; | |
} | |
public int getCreditCardExpiryMonth() { | |
return creditCardExpiryMonth; | |
} | |
public void setCreditCardExpiryMonth(int creditCardExpiryMonth) { | |
this.creditCardExpiryMonth = creditCardExpiryMonth; | |
} | |
public int getCreditCardExpiryYear() { | |
return creditCardExpiryYear; | |
} | |
public void setCreditCardExpiryYear(int creditCardExpiryYear) { | |
this.creditCardExpiryYear = creditCardExpiryYear; | |
} | |
public Set<Amenity> getAmenities() { | |
return amenities; | |
} | |
public void setAmenities(Set<Amenity> amenities) { | |
this.amenities = amenities; | |
} | |
private Date today() { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.add(Calendar.DAY_OF_MONTH, -1); | |
return calendar.getTime(); | |
} | |
@Override | |
public String toString() { | |
return "Booking(" + user + "," + hotel + ")"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mangocity.tmc.hotel.model; | |
import java.io.Serializable; | |
import java.math.BigDecimal; | |
/** | |
* A hotel where users may book stays. | |
*/ | |
public class Hotel implements Serializable { | |
private int id; | |
private String name; | |
private String address; | |
private String city; | |
private String state; | |
private String zip; | |
private String country; | |
private BigDecimal price; | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getAddress() { | |
return address; | |
} | |
public void setAddress(String address) { | |
this.address = address; | |
} | |
public String getCity() { | |
return city; | |
} | |
public void setCity(String city) { | |
this.city = city; | |
} | |
public String getZip() { | |
return zip; | |
} | |
public void setZip(String zip) { | |
this.zip = zip; | |
} | |
public String getState() { | |
return state; | |
} | |
public void setState(String state) { | |
this.state = state; | |
} | |
public String getCountry() { | |
return country; | |
} | |
public void setCountry(String country) { | |
this.country = country; | |
} | |
public BigDecimal getPrice() { | |
return price; | |
} | |
public void setPrice(BigDecimal price) { | |
this.price = price; | |
} | |
public Booking createBooking(User user) { | |
return new Booking(this, user); | |
} | |
@Override | |
public String toString() { | |
return "Hotel(" + name + "," + address + "," + city + "," + zip + ")"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mangocity.tmc.hotel.model; | |
import java.io.Serializable; | |
/** | |
* A backing bean for the main hotel search form. Encapsulates the criteria needed to perform a hotel search. | |
* | |
* It is expected a future milestone of Spring Web Flow 2.0 will allow flow-scoped beans like this one to hold | |
* references to transient services that are restored automatically when the flow is resumed on subsequent requests. | |
* This would allow this SearchCriteria object to delegate to the {@link BookingService} directly, for example, | |
* eliminating the need for the actions in {@link MainActions}. | |
*/ | |
public class SearchCriteria implements Serializable { | |
private static final long serialVersionUID = 1L; | |
/** | |
* The user-provided search criteria for finding Hotels. | |
*/ | |
private String searchString; | |
/** | |
* The maximum page size of the Hotel result list | |
*/ | |
private int pageSize; | |
/** | |
* The current page of the Hotel result list. | |
*/ | |
private int page; | |
public String getSearchString() { | |
return searchString; | |
} | |
public void setSearchString(String searchString) { | |
this.searchString = searchString; | |
} | |
public int getPageSize() { | |
return pageSize; | |
} | |
public void setPageSize(int pageSize) { | |
this.pageSize = pageSize; | |
} | |
public int getPage() { | |
return page; | |
} | |
public void setPage(int page) { | |
this.page = page; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mangocity.tmc.hotel.model; | |
import java.io.Serializable; | |
/** | |
* A user who can book hotels. | |
*/ | |
public class User implements Serializable { | |
private String username; | |
private String password; | |
private String name; | |
public User() { | |
} | |
public User(String username, String password, String name) { | |
this.username = username; | |
this.password = password; | |
this.name = name; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return "User(" + username + ")"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mangocity.tmc.hotel.service; | |
import com.mangocity.tmc.hotel.model.Booking; | |
import com.mangocity.tmc.hotel.model.Hotel; | |
import com.mangocity.tmc.hotel.model.SearchCriteria; | |
import java.util.List; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: heyunhao | |
* Date: 13-4-9 | |
* Time: 下午1:57 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public interface BookingService { | |
/** | |
* Find bookings made by the given user | |
* @param username the user's name | |
* @return their bookings | |
*/ | |
public List<Booking> findBookings(SearchCriteria username); | |
/** | |
* Find hotels available for booking by some criteria. | |
* @param criteria the search criteria | |
* @return a list of hotels meeting the criteria | |
*/ | |
public List<Hotel> findHotels(SearchCriteria criteria); | |
/** | |
* Find hotels by their identifier. | |
* @param id the hotel id | |
* @return the hotel | |
*/ | |
public Hotel findHotelById(Long id); | |
/** | |
* Create a new, transient hotel booking instance for the given user. | |
* @param hotelId the hotelId | |
* @param userName the user name | |
* @return the new transient booking instance | |
*/ | |
public Booking createBooking(Long hotelId, String userName); | |
/** | |
* Persist the booking to the database | |
* @param booking the booking | |
*/ | |
public void persistBooking(Booking booking); | |
/** | |
* Cancel an existing booking. | |
* @param id the booking id | |
*/ | |
public void cancelBooking(Long id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mangocity.tmc.hotel.service; | |
import com.mangocity.tmc.hotel.model.Booking; | |
import com.mangocity.tmc.hotel.model.Hotel; | |
import com.mangocity.tmc.hotel.model.SearchCriteria; | |
import org.springframework.stereotype.Service; | |
import java.math.BigDecimal; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: heyunhao | |
* Date: 13-4-9 | |
* Time: 下午2:03 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
@Service("bookingService") | |
public class BookingServiceImpl implements BookingService { | |
@Override | |
public List<Booking> findBookings(SearchCriteria username) { | |
List<Booking> bookings = new ArrayList<Booking>(); | |
for (int i = 0; i < 10; i++) { | |
Booking bok = new Booking(); | |
bok.setBeds(2); | |
bok.setId(3); | |
bok.setCreditCard("123456"+i); | |
bok.setCheckinDate(new Date()); | |
bok.setCheckoutDate(new Date()); | |
Hotel hotel = new Hotel(); | |
hotel.setName("皇冠假日酒店" + i); | |
hotel.setAddress("冰海大道" + i); | |
hotel.setPrice(new BigDecimal(1500)); | |
bok.setHotel(hotel); | |
bookings.add(bok); | |
} | |
return bookings; | |
} | |
@Override | |
public List<Hotel> findHotels(SearchCriteria criteria) { | |
List<Hotel> hotels = new ArrayList<Hotel>(); | |
for(int i=0;i<10;i++){ | |
Hotel hotel = new Hotel(); | |
hotel.setId(i); | |
hotel.setName("Philips Hotel"+i); | |
hotels.add(hotel); | |
} | |
return hotels; | |
} | |
@Override | |
public Hotel findHotelById(Long id) { | |
return null; | |
} | |
@Override | |
public Booking createBooking(Long hotelId, String userName) { | |
return null; //To change body of implemented methods use File | Settings | File Templates. | |
} | |
@Override | |
public void persistBooking(Booking booking) { | |
//To change body of implemented methods use File | Settings | File Templates. | |
} | |
@Override | |
public void cancelBooking(Long id) { | |
//To change body of implemented methods use File | Settings | File Templates. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"> | |
<xs:complexType name="bookingServiceImpl"> | |
<xs:sequence/> | |
</xs:complexType> | |
</xs:schema> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GIF89a� | |