Advertisement
desislava_topuzakova

02. Articles

Jul 3rd, 2022
1,416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. клас Article:
  2. package articles;
  3.  
  4. public class Article {
  5.     //характеристики -> полета
  6.     private String title;
  7.     private String content;
  8.     private String author;
  9.  
  10.     //конструктор -> създаваме обекти
  11.     public Article (String title, String content, String author) {
  12.         //нов празен обект
  13.         //title = null
  14.         //content = null
  15.         //author = null
  16.         this.title = title;
  17.         this.content = content;
  18.         this.author = author;
  19.     }
  20.  
  21.     //функционалности -> методи
  22.     public void edit (String newContent) {
  23.         this.content = newContent;
  24.     }
  25.  
  26.     public void changeAuthor (String newAuthor) {
  27.         this.author = newAuthor;
  28.     }
  29.  
  30.     public void rename (String newTitle) {
  31.         this.title = newTitle;
  32.     }
  33.  
  34.     //default toString -> "{package}:{class}@{address}"
  35.    @Override //пренаписвам -> работи по мой избор
  36.     public String toString() {
  37.         //"{title} - {content}:{author}"
  38.        return this.title + " - " + this.content + ": " + this.author;
  39.    }
  40. }
  41.  
  42.  
  43.  
  44. Main метод:
  45. package articles;
  46.  
  47. import java.util.Scanner;
  48.  
  49. public class Main {
  50.     public static void main(String[] args) {
  51.         Scanner scanner = new Scanner(System.in);
  52.         String articleData = scanner.nextLine();
  53.         //"Holy Ghost, content, John Sandford".split(", ")
  54.         //-> ["Holy Ghost", "content", "John Sandford"]
  55.         String title = articleData.split(", ")[0];
  56.         String content = articleData.split(", ")[1];
  57.         String author = articleData.split(", ")[2];
  58.  
  59.          Article article = new Article(title, content, author);
  60.          int n = Integer.parseInt(scanner.nextLine());
  61.         for (int i = 1; i <= n; i++) {
  62.             String command = scanner.nextLine();
  63.             //"{име на команда}: {нова стойност}".split(": ") -> ["{име на команда}", "{нова стойност}"]
  64.             //"Edit: {new content}"
  65.             //"ChangeAuthor: {new author}"
  66.             //"Rename: {new title}"
  67.             String commandName = command.split("\\: ")[0]; //"Edit", "ChangeAuthor", "Rename"
  68.             String newValue = command.split("\\: ")[1];
  69.  
  70.             switch (commandName) {
  71.                 case "Edit":
  72.                     article.edit(newValue);
  73.                     break;
  74.                 case "ChangeAuthor":
  75.                     article.changeAuthor(newValue);
  76.                     break;
  77.                 case "Rename":
  78.                     article.rename(newValue);
  79.                     break;
  80.             }
  81.         }
  82.  
  83.         System.out.println(article.toString());
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement