Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.io.PrintStream;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- abstract class Product implements Comparable<Product> {
- private String id;
- private String name;
- private int price;
- public Product(String id, String name, int price) {
- this.id = id;
- this.name = name;
- this.price = price;
- }
- public String getId() {
- return id;
- }
- public abstract float getTotalPrice();
- public String getName() {
- return name;
- }
- public int getPrice() {
- return price;
- }
- }
- class ProductPerKilogram extends Product{
- private float quantity;
- public ProductPerKilogram(String id, String name, int price, float quantity) {
- super(id, name, price);
- this.quantity= quantity;
- }
- public float getTotalPrice(){
- return super.getPrice()*quantity/1000.0f;
- }
- @Override
- public int compareTo(Product o) {
- return Float.compare(this.getTotalPrice(),o.getTotalPrice());
- }
- @Override
- public String toString() {
- return String.format("%s - %.2f",super.getId(),getTotalPrice());
- }
- }
- class WholeProduct extends Product{
- private float quantity;
- public WholeProduct(String id, String name, int price, int quantity) {
- super(id, name, price);
- this.quantity= quantity;
- }
- public float getTotalPrice(){
- return super.getPrice()*quantity;
- }
- @Override
- public int compareTo(Product o) {
- return Float.compare(this.getTotalPrice(),o.getTotalPrice());
- }
- @Override
- public String toString() {
- return String.format("%s - %.2f",super.getId(),getTotalPrice());
- }
- }
- class InvalidOperationException extends Exception{
- public InvalidOperationException(String message) {
- super(message);
- }
- }
- public class ShoppingTest {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- ShoppingCart cart = new ShoppingCart();
- int items = Integer.parseInt(sc.nextLine());
- for (int i = 0; i < items; i++) {
- try {
- cart.addItem(sc.nextLine());
- } catch (InvalidOperationException e) {
- System.out.println(e.getMessage());
- }
- }
- List<Integer> discountItems = new ArrayList<>();
- int discountItemsCount = Integer.parseInt(sc.nextLine());
- for (int i = 0; i < discountItemsCount; i++) {
- discountItems.add(Integer.parseInt(sc.nextLine()));
- }
- int testCase = Integer.parseInt(sc.nextLine());
- if (testCase == 1) {
- cart.printShoppingCart(System.out);
- } else if (testCase == 2) {
- try {
- cart.blackFridayOffer(discountItems, System.out);
- } catch (InvalidOperationException e) {
- System.out.println(e.getMessage());
- }
- } else {
- System.out.println("Invalid test case");
- }
- }
- }
- class ShoppingCart {
- List<Product> products;
- public ShoppingCart() {
- this.products = new ArrayList<>();
- }
- public void addItem(String s) throws InvalidOperationException {
- String[] parts= s.split(";");
- String id = parts[1];
- String name = parts[2];
- int price = Integer.parseInt(parts[3]);
- float quantity = Float.parseFloat(parts[4]);
- if(quantity==0){
- throw new InvalidOperationException(String.format("The quantity of the product with id %s can not be 0.",id));
- }
- if(parts[0].equals("WS")){
- products.add(new WholeProduct(id,name,price,(int)quantity));
- }
- else{
- products.add(new ProductPerKilogram(id,name,price,quantity));
- }
- }
- public void printShoppingCart(PrintStream out) {
- PrintWriter printWriter= new PrintWriter(out);
- products.stream().sorted(Comparator.reverseOrder()).forEach(i->printWriter.println(i));
- printWriter.flush();
- }
- public void blackFridayOffer(List<Integer> discountItems, PrintStream out) throws InvalidOperationException {
- PrintWriter printWriter= new PrintWriter(out);
- if(discountItems.size()==0)
- throw new InvalidOperationException("There are no products with discount.");
- products.stream()
- .filter(i->discountItems.contains(Integer.parseInt(i.getId())))
- .map(i -> i.getId() + " - " + String.format("%.2f", i.getTotalPrice() * 0.1f))
- .forEach(i -> printWriter.println(i));
- printWriter.flush();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement