Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- клас Article:
- package articles;
- public class Article {
- //характеристики -> полета
- private String title;
- private String content;
- private String author;
- //конструктор -> създаваме обекти
- public Article (String title, String content, String author) {
- //нов празен обект
- //title = null
- //content = null
- //author = null
- this.title = title;
- this.content = content;
- this.author = author;
- }
- //функционалности -> методи
- public void edit (String newContent) {
- this.content = newContent;
- }
- public void changeAuthor (String newAuthor) {
- this.author = newAuthor;
- }
- public void rename (String newTitle) {
- this.title = newTitle;
- }
- //default toString -> "{package}:{class}@{address}"
- @Override //пренаписвам -> работи по мой избор
- public String toString() {
- //"{title} - {content}:{author}"
- return this.title + " - " + this.content + ": " + this.author;
- }
- }
- Main метод:
- package articles;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String articleData = scanner.nextLine();
- //"Holy Ghost, content, John Sandford".split(", ")
- //-> ["Holy Ghost", "content", "John Sandford"]
- String title = articleData.split(", ")[0];
- String content = articleData.split(", ")[1];
- String author = articleData.split(", ")[2];
- Article article = new Article(title, content, author);
- int n = Integer.parseInt(scanner.nextLine());
- for (int i = 1; i <= n; i++) {
- String command = scanner.nextLine();
- //"{име на команда}: {нова стойност}".split(": ") -> ["{име на команда}", "{нова стойност}"]
- //"Edit: {new content}"
- //"ChangeAuthor: {new author}"
- //"Rename: {new title}"
- String commandName = command.split("\\: ")[0]; //"Edit", "ChangeAuthor", "Rename"
- String newValue = command.split("\\: ")[1];
- switch (commandName) {
- case "Edit":
- article.edit(newValue);
- break;
- case "ChangeAuthor":
- article.changeAuthor(newValue);
- break;
- case "Rename":
- article.rename(newValue);
- break;
- }
- }
- System.out.println(article.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement