Advertisement
desislava_topuzakova

02. Articles

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