Advertisement
deyanmalinov

Problem 2. Book Shop

Jun 25th, 2020
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package DPM;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         try {
  7.             String author = scan.nextLine();
  8.             String title = scan.nextLine();
  9.             double price = Double.parseDouble(scan.nextLine());
  10.             Book book = new Book(author,title,price);
  11.             GoldenEditionBook goldenEditionBook = new GoldenEditionBook(author,title,price);
  12.             System.out.println(book.toString());
  13.             System.out.println(goldenEditionBook.toString());
  14.         }catch (IllegalArgumentException msg){
  15.             System.out.println(msg.getMessage());
  16.         }
  17.     }
  18. }
  19. ---------------------------------------------------
  20. package DPM;
  21. public class Book {
  22.     private String author;
  23.     private String title;
  24.     private double price;
  25.     public Book(String author, String title, double price){
  26.         this.setAuthor(author);
  27.         this.setTitle(title);
  28.         this.setPrice(price);
  29.     }
  30.     public String getAuthor() {
  31.         return author;
  32.     }
  33.     public String getTitle() {
  34.         return title;
  35.     }
  36.     public double getPrice() {
  37.         return price;
  38.     }
  39.     public void setAuthor(String author) {
  40.         String[] authorNames = author.split(" ");
  41.         if (authorNames.length == 2 && Character.isDigit(authorNames[1].charAt(0))){
  42.             throw new IllegalArgumentException("Author not valid!");
  43.         }
  44.         this.author = author;
  45.     }
  46.     public void setTitle(String title) {
  47.         if (title.length() < 3){
  48.             throw new IllegalArgumentException("Title not valid!");
  49.         }
  50.         this.title = title;
  51.     }
  52.     public void setPrice(double price) {
  53.         if (price < 1){
  54.             throw new IllegalArgumentException("Price not valid!");
  55.         }
  56.         this.price = price;
  57.     }
  58.     @Override
  59.     public String toString() {
  60.         final StringBuilder sb = new StringBuilder();
  61.         sb.append("Type: ").append(this.getClass().getSimpleName())
  62.                 .append(System.lineSeparator())
  63.                 .append("Title: ").append(this.getTitle())
  64.                 .append(System.lineSeparator())
  65.                 .append("Author: ").append(this.getAuthor())
  66.                 .append(System.lineSeparator())
  67.                 .append("Price: ").append(this.getPrice())
  68.                 .append(System.lineSeparator());
  69.         return sb.toString();
  70.     }
  71. }
  72. ----------------------------------------------
  73. package DPM;
  74. public class GoldenEditionBook extends Book{
  75.     public GoldenEditionBook(String author, String title, double price){
  76.         super(author, title, price);
  77.     }
  78.     @Override
  79.     public double getPrice() {
  80.         return super.getPrice() + super.getPrice() * 0.3;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement