Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dominos;
- import java.util.HashSet;
- import java.util.TreeSet;
- import exceptions.InvalidArgumentsException;
- import products.Menu;
- import users.Admin;
- import users.ClientUser;
- import users.User;
- public class Dominos {
- public static final String NO_USERNAME = "No user with this username";
- public static final String WRONG_PASSWORD = "Wrong password";
- public static final String USERNAME_ERROR = "Already exist user with this username";
- private final String name;
- private String address;
- private Menu menu;
- private TreeSet<Order> orders = new TreeSet<>();
- private HashSet<User> users = new HashSet<>();
- private HashSet<ClientUser> clients = new HashSet<>();
- private HashSet<Admin> admins = new HashSet<>();
- private User loggedUser;
- public Dominos(String name, String address, Menu menu) throws InvalidArgumentsException {
- this.name = name;
- this.setAddress(address);
- this.menu = Menu.getInstance();
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) throws InvalidArgumentsException{
- if(address == null) {
- throw new InvalidArgumentsException("Invalid address");
- }
- this.address = address;
- }
- public String getName() {
- return name;
- }
- public void register(User user) throws InvalidArgumentsException{
- if(this.userExist(user.getUsername())) {
- throw new InvalidArgumentsException(USERNAME_ERROR);
- }
- else {
- this.users.add(user);
- user.setRestaurant(this);
- if(user.getIsAdmin()) {
- this.admins.add((Admin) user);
- }
- else {
- this.clients.add((ClientUser) user);
- }
- }
- }
- public boolean login(String username, String password) throws InvalidArgumentsException {
- boolean loginSuccessful = false;
- if(userExist(username)) {
- if(getUser(username).getPassword().equals(password)) {
- loginSuccessful = true;
- this.loggedUser = getUser(username);
- }
- else {
- throw new InvalidArgumentsException(WRONG_PASSWORD);
- }
- }
- return loginSuccessful;
- }
- public boolean userExist(String username) {
- for(User u: this.users) {
- if(u.getUsername().equals(username)) {
- return true;
- }
- }
- return false;
- }
- public User getUser(String username) {
- for(User u: this.users) {
- if(u.getUsername().equals(username)) {
- return u;
- }
- }
- return null;
- }
- public Menu getMenu() {
- return this.menu;
- }
- public void acceptOrder(Order order) {
- this.orders.add(order);
- }
- public TreeSet<Order> getOrders(){
- return this.orders;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement