天天看點

知識付費源碼|知識付費網站搭建|知識付費小程式源碼

作者:騎毛驢的巴依老爺

 設計一個線上教育知識付費源碼系統(www.xiaofeifei666.top)。

  

  解決方案:假設我們想要設計一個基本的内容付費閱讀系統并帶小程式,提供以下功能:

  

  •查找知識資料庫并閱讀付費内容;

  

  •使用者成員的建立和擴充;

  

  •基于使用者分級權限管理,并且這個使用者隻能獲權後閱讀。

倉庫完整源碼:zs.xcxyms.top

 

  類OnlineReaderSystem代表程式的主體。我們可以實作這個類,讓它存儲有關所有涉及使用者管理的權限分級的資訊,并重新整理顯示,但這會使這個類變得相當龐大。相反,我們選擇将這些元件拆分成Library、UserManager和Display類。

  

  類:

  

  1、使用者

  

  2、知識

  

  3、UserManager

  

  4、OnlineReaderSystem

知識付費源碼|知識付費網站搭建|知識付費小程式源碼

  代碼如下:

  import java.util.HashMap;

  /*

  * This class represents the system

  */

  class OnlineReaderSystem {

  private Library library;

  private UserManager userManager;

  private Display display;

  private Book activeBook;

  private User activeUser;

  public OnlineReaderSystem()

  {

  userManager = new UserManager();

  library = new Library();

  display = new Display();

  }

  public Library getLibrary()

  {

  return library;

  }

  public UserManager getUserManager()

  {

  return userManager;

  }

  public Display getDisplay()

  {

  return display;

  }

  public Book getActiveBook()

  {

  return activeBook;

  }

  public void setActiveBook(Book book)

  {

  activeBook = book;

  display.displayBook(book);

  }

  public User getActiveUser()

  {

  return activeUser;

  }

  public void setActiveUser(User user)

  {

  activeUser = user;

  display.displayUser(user);

  }

  }

  /*

  * We then implement separate classes to handle the user

  * manager, the library, and the display components

  */

  /*

  * This class represents the Library which is responsible

  * for storing and searching the books.

  */

  class Library {

  private HashMap<Integer, Book> books;

  public Library()

  {

  books = new HashMap<Integer, Book>();

  }

  public Boolean addBook(int id, String details, String title)

  {

  if (books.containsKey(id)) {

  return false;

  }

  Book book = new Book(id, details, title);

  books.put(id, book);

  return true;

  }

  public Boolean addBook(Book book)

  {

  if (books.containsKey(book.getId())) {

  return false;

  }

  books.put(book.getId(), book);

  return true;

  }

  public boolean remove(Book b)

  {

  return remove(b.getId());

  }

  public boolean remove(int id)

  {

  if (!books.containsKey(id)) {

  return false;

  }

  books.remove(id);

  return true;

  }

  public Book find(int id)

  {

  return books.get(id);

  }

  }

  /*

  * This class represents the UserManager which is responsible

  * for managing the users, their membership etc.

  */

  class UserManager {

  private HashMap<Integer, User> users;

  public UserManager()

  {

  users = new HashMap<Integer, User>();

  }

  public Boolean addUser(int id, String details, String name)

  {

  if (users.containsKey(id)) {

  return false;

  }

  User user = new User(id, details, name);

  users.put(id, user);

  return true;

  }

  public Boolean addUser(User user)

  {

  if (users.containsKey(user.getId())) {

  return false;

  }

  users.put(user.getId(), user);

  return true;

  }

  public boolean remove(User u)

  {

  return remove(u.getId());

  }

  public boolean remove(int id)

  {

  if (users.containsKey(id)) {

  return false;

  }

  users.remove(id);

  return true;

  }

  public User find(int id)

  {

  return users.get(id);

  }

  }

  /*

  * This class represents the Display, which is responsible

  * for displaying the book, it's pages and contents. It also

  * shows the current user. * It provides the method

  * turnPageForward, turnPageBackward, refreshPage etc.

  */

  class Display {

  private Book activeBook;

  private User activeUser;

  private int pageNumber = 0;

  public void displayUser(User user)

  {

  activeUser = user;

  refreshUsername();

  }

  public void displayBook(Book book)

  {

  pageNumber = 0;

  activeBook = book;

  refreshTitle();

  refreshDetails();

  refreshPage();

  }

  public void turnPageForward()

  {

  pageNumber++;

  System.out.println("Turning forward to page no " +

  pageNumber + " of book having title " +

  activeBook.getTitle());

  refreshPage();

  }

  public void turnPageBackward()

  {

  pageNumber--;

  System.out.println("Turning backward to page no " +

  pageNumber + " of book having title " +

  activeBook.getTitle());

  refreshPage();

  }

  public void refreshUsername()

  {

  /* updates username display */

  System.out.println("User name " + activeUser.getName() +

  " is refreshed");

  }

  public void refreshTitle()

  {

  /* updates title display */

  System.out.println("Title of the book " +

  activeBook.getTitle() + " refreshed");

  }

  public void refreshDetails()

  {

  /* updates details display */

  System.out.println("Details of the book " +

  activeBook.getTitle() + " refreshed");

  }

  public void refreshPage()

  {

  /* updated page display */

  System.out.println("Page no " + pageNumber + " refreshed");

  }

  }

  /*

  * The classes for User and Book simply hold data and

  * provide little functionality.

  * This class represents the Book which is a simple POJO

  */

  class Book {

  private int bookId;

  private String details;

  private String title;

  public Book(int id, String details, String title)

  {

  bookId = id;

  this.details = details;

  this.title = title;

  }

  public int getId()

  {

  return bookId;

  }

  public void setId(int id)

  {

  bookId = id;

  }

  public String getDetails()

  {

  return details;

  }

  public void setDetails(String details)

  {

  this.details = details;

  }

  public String getTitle()

  {

  return title;

  }

  public void setTitle(String title)

  {

  this.title = title;

  }

  }

  /*

  * This class represents the User which is a simple POJO

  */

  class User {

  private int userId;

  private String name;

  private String details;

  public void renewMembership()

  {

  }

  public User(int id, String details, String name)

  {

  this.userId = id;

  this.details = details;

  this.name = name;

  }

  public int getId()

  {

  return userId;

  }

  public void setId(int id)

  {

  userId = id;

  }

  public String getDetails()

  {

  return details;

  }

  public void setDetails(String details)

  {

  this.details = details;

  }

  public String getName()

  {

  return name;

  }

  public void setName(String name)

  {

  this.name = name;

  }

  }

  // This class is used to test the Application

  public class AppTest {

  public static void main(String[] args)

  {

  OnlineReaderSystem onlineReaderSystem = new OnlineReaderSystem();

  Book dsBook = new Book(1, "It contains Data Structures", "Ds");

  Book algoBook = new Book(2, "It contains Algorithms", "Algo");

  onlineReaderSystem.getLibrary().addBook(dsBook);

  onlineReaderSystem.getLibrary().addBook(algoBook);

  User user1 = new User(1, " ", "Ram");

  User user2 = new User(2, " ", "Gopal");

  onlineReaderSystem.getUserManager().addUser(user1);

  onlineReaderSystem.getUserManager().addUser(user2);

  onlineReaderSystem.setActiveBook(algoBook);

  onlineReaderSystem.setActiveUser(user1);

  onlineReaderSystem.getDisplay().turnPageForward();

  onlineReaderSystem.getDisplay().turnPageForward();

  onlineReaderSystem.getDisplay().turnPageBackward();

  }

  }

  知識付費源碼系統類圖:聚合和多重性是設計類圖時需要考慮的兩個重要問題。讓我們來詳細了解一下。

  聚合,

  

  聚合隻是表示一種關系,其中一件事可以獨立于其他事存在。它意味着在定義類時建立或組合不同的抽象。聚合表示為類圖中關系的一部分。在下面的圖中,我們可以看到聚合是由一個指向超類的菱形末端的邊表示的。“圖書管理系統”是由各種類組成的超類。

  

  這些類是User、Book和Librarian。此外,對于“Account”類,“User”是一個超類。所有這些,共享一種關系,這些關系被稱為聚合關系。

  

  多重性,

  

  多重性意味着一個類的元素數量與另一個類相關聯。這些關系可以是一對一、多對多、多對一或一對多。用來表示我們使用的一個元素1,表示我們使用的零元素0,以及我們使用的許多元素*. 我們可以從圖表中看到;許多使用者與表示的許多書相關聯*這表示多對多類型的關系。一個使用者隻有一個用1表示的帳戶,這表示a一對一的類型的關系。

  

  知識付費系統類圖簡單地描述了知識管理系統類的結構、屬性、方法或操作、對象之間的關系。

————————————————

版權聲明:本文為CSDN部落客「scxcyzm」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/scxcyzm/article/details/123729468

繼續閱讀