Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static BaseProduct input(Class<? extends BaseProduct> productClass, InputStream in) throws IOException {
- DataInputStream dataInputStream = new DataInputStream(in);
- String brand = dataInputStream.readUTF();
- String productName = dataInputStream.readUTF();
- BigDecimal price = new BigDecimal(dataInputStream.readUTF());
- int totalQuantity = dataInputStream.readInt();
- int defectiveQuantity = dataInputStream.readInt();
- List<Review> reviews = new ArrayList<>();
- for (Review review : reviews) {
- String userName = dataInputStream.readUTF();
- int rating = dataInputStream.readInt();
- }
- return createProductInstance(brand, productName, price, totalQuantity, defectiveQuantity, reviews, productClass);
- }
- private static BaseProduct createProductInstance(String brand, String productName, BigDecimal price, int totalQuantity, int defectiveQuantity, List<Review> reviews, Class<? extends BaseProduct> productClass) {
- try {
- Constructor<? extends BaseProduct> productConstructor = productClass.getConstructor(String.class, String.class, BigDecimal.class, int.class, int.class, List.class);
- return productConstructor.newInstance(brand, productName, price, totalQuantity, defectiveQuantity, reviews);
- } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
- throw new IllegalArgumentException("Could not create product instance: " + e);
- }
- }
Add Comment
Please, Sign In to add comment