Java源码示例:com.packtpub.mmj.mcrsrvc.domain.model.RestaurantService
示例1
/**
* main method of the Application
*
* @param args
*/
public static void main(String[] args) {
try {
// Initialize the RestaurantService
RestaurantService restaurantService = new RestaurantService(new InMemRestaurantRepository());
// Data Creation for Restaurants
List<Table> tableList = Arrays.asList(
new Table("Table 1", BigInteger.ONE, 6),
new Table("Table 2", BigInteger.valueOf(2), 4),
new Table("Table 3", BigInteger.valueOf(3), 2)
);
// Add few restaurants using Service
// Note: To raise an exception give same restaurant name to one of the below restaurant
restaurantService
.add(new Restaurant("Big-O Restaurant", "1", Optional.ofNullable(tableList)));
restaurantService.add(new Restaurant("Pizza Shops", "2", Optional.empty()));
restaurantService.add(new Restaurant("La Pasta", "3", Optional.empty()));
// Retrieving all restaurants using Service
Collection<Restaurant> restaurants = restaurantService.getAll();
// Print the retrieved restaurants on console
System.out.println("Restaurants List:");
restaurants.stream()
.map(r -> String.format("Restaurant: %s", r))
.forEach(System.out::println);
} catch (Exception ex) {
System.out.println(String.format("Exception: %s", ex.getMessage()));
// Exception Handling Code
}
}
示例2
/**
* main method of the Application
*
* @param args
*/
public static void main(String[] args) {
try {
// Initialize the RestaurantService
RestaurantService restaurantService = new RestaurantService(new InMemRestaurantRepository());
// Data Creation for Restaurants
Table table1 = new Table("Table 1", BigInteger.ONE, 6);
Table table2 = new Table("Table 2", BigInteger.valueOf(2), 4);
Table table3 = new Table("Table 3", BigInteger.valueOf(3), 2);
List<Table> tableList = new ArrayList();
tableList.add(table1);
tableList.add(table2);
tableList.add(table3);
Restaurant restaurant1 = new Restaurant("Big-O Restaurant", "1", tableList);
// Adding the created restaurant using Service
restaurantService.add(restaurant1);
// Note: To raise an exception give Same restaurant name to one of the below restaurant
Restaurant restaurant2 = new Restaurant("Pizza Shops", "2", null);
restaurantService.add(restaurant2);
Restaurant restaurant3 = new Restaurant("La Pasta", "3", null);
restaurantService.add(restaurant3);
// Retrieving all restaurants using Service
Collection<Restaurant> restaurants = restaurantService.getAll();
// Print the retrieved restaurants on console
System.out.println("Restaurants List:");
restaurants.stream().forEach((restaurant) -> {
System.out.println(String.format("Restaurant: %s", restaurant));
});
} catch (Exception ex) {
System.out.println(String.format("Exception: %s", ex.getMessage()));
// Exception Handling Code
}
}
示例3
/**
* main method of the Application
*
* @param args
*/
public static void main(String[] args) {
try {
// Initialize the RestaurantService
RestaurantService restaurantService = new RestaurantService(new InMemRestaurantRepository());
// Data Creation for Restaurants
Table table1 = new Table("Table 1", BigInteger.ONE, 6);
Table table2 = new Table("Table 2", BigInteger.valueOf(2), 4);
Table table3 = new Table("Table 3", BigInteger.valueOf(3), 2);
List<Table> tableList = new ArrayList();
tableList.add(table1);
tableList.add(table2);
tableList.add(table3);
Restaurant restaurant1 = new Restaurant("Big-O Restaurant", "1", tableList);
// Adding the created restaurant using Service
restaurantService.add(restaurant1);
// Note: To raise an exception give Same restaurant name to one of the below restaurant
Restaurant restaurant2 = new Restaurant("Pizza Shops", "2", null);
restaurantService.add(restaurant2);
Restaurant restaurant3 = new Restaurant("La Pasta", "3", null);
restaurantService.add(restaurant3);
// Retrieving all restaurants using Service
Collection<Restaurant> restaurants = restaurantService.getAll();
// Print the retrieved restaurants on console
System.out.println("Restaurants List:");
restaurants.stream().forEach((restaurant) -> {
System.out.println("Restaurant: " + restaurant);
});
} catch (Exception ex) {
System.out.println("Exception: " + ex.getMessage());
// Exception Handling Code
}
}
示例4
/**
* main method of the Application
*
* @param args
*/
public static void main(String[] args) {
try {
// Initialize the RestaurantService
RestaurantService restaurantService = new RestaurantService(new InMemRestaurantRepository());
// Data Creation for Restaurants
Table table1 = new Table("Table 1", BigInteger.ONE, 6);
Table table2 = new Table("Table 2", BigInteger.valueOf(2), 4);
Table table3 = new Table("Table 3", BigInteger.valueOf(3), 2);
List<Table> tableList = new ArrayList();
tableList.add(table1);
tableList.add(table2);
tableList.add(table3);
Restaurant restaurant1 = new Restaurant("Big-O Restaurant", "1", tableList);
// Adding the created restaurant using Service
restaurantService.add(restaurant1);
// Note: To raise an exception give Same restaurant name to one of the below restaurant
Restaurant restaurant2 = new Restaurant("Pizza Shops", "2", null);
restaurantService.add(restaurant2);
Restaurant restaurant3 = new Restaurant("La Pasta", "3", null);
restaurantService.add(restaurant3);
// Retrieving all restaurants using Service
Collection<Restaurant> restaurants = restaurantService.getAll();
// Print the retrieved restaurants on console
System.out.println("Restaurants List:");
restaurants.stream().forEach((restaurant) -> {
System.out.println("Restaurant: " + restaurant);
});
} catch (Exception ex) {
System.out.println("Exception: " + ex.getMessage());
// Exception Handling Code
}
}