Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

InfoTc 1040 Introduction to Problem Solving and Programming Users Class (Use Pyt

ID: 3820054 • Letter: I

Question

InfoTc 1040 Introduction to Problem Solving and Programming Users Class (Use Python 3.4.2)

In this programming assignment, you will create the following:

- A User class that stores information about an user.(last module)

- A Users class that stores and displays User objects.

- A usermanager program that uses the User and Users classes.

These elements are described below.

User Class

Write a class named User that has the following attributes and methods. Save this class as User.py

Attributes:

Methods:

_init_:

This method takes 4 parameters, the username, wins, ties, and losses (when initializing the last 3 will be zeroes eg. (John,0,0,0)) It should create __username, __wins, __ties, and __losses based on those parameters and it should create __created based on the current time

get_username:

Returns the value of __username field

get_user_wins:

Return the value of the __wins field

set_user_wins:

Takes in 1 parameter and sets __wins to that parameter

get_user_ties:

Returns the value of the __ties field

set_user_ties:

Takes in 1 parameters and sets __ties to that parameter

get_user_losses:

Returns the value of the __losses field

set_user_losses:

Takes in 1 parameter and sets __losses to that parameter

display_user:

Displays the username and stats (check sample output for formatting)

Users Class

Write a class named Users with the following attributes and methods. Save this class as Users.py

Attributes

__user_list: a list used to store User objects.

Methods

#You will be making these same functions for the final project but adding some more error checking to them

__init__: use this method to create an empty list for __user_list.

create_user: this method should receive a User object and append it to the __user_list list.

#THIS IS EXTRA CREDIT
read_user: this method should receive a name and return a found user object

delete_user: this method should receive a user name and then remove them from the list.

display_user: this method should display the current read user. By current. If we have a list of object and we read an object (grab one from the list) and have it locally to work with we would display that local object.

display_users: this method will print information about each of the User objects within __user_list. If no users have been added to the list, it should print out a message saying that there are no users.

UserManager Program

Once you have written the User and Users classes, create another program called usermanager.py. This program will use User.py and Users.py as modules.

In usermanager.py, create a Users object and print a menu with three options(four with extra credit): Create User, Read User, Delete User, Display User, Display Users, and Exit.

Create User:

Takes in 2 parameters, username and wins. Then checks to see if the user names exists. If it exists we return False, if it doesn’t exist, we add it to the list and return True.
#NOTE we are setting the wins with what the user types in, the ties and losses are still done by random.randint(1,10)

If the name exists, we print the error:

[!]Error: This User already exists.

Read User:

Takes in a parameter, username, and checks to see if it exists. If it exists, we return the True AND the user object which has the same name. If it doesn’t exist, we return False and the user object. At the beginning of the function set found_user = None. We will return True, found_user or return False, found_user. To check if a user exists, we will compare each user name in the list (for user in self.__user_list:) to username. So compare each user.get_name() to username
If the name doesn’t exist, we print the error:
[!]Error: This User doesn't exist.

Delete User:

Takes in a parameter, a username, and deletes that user from the list. If the name doesn’t exist, we print the error:
[!]Error: This User doesn't exist.

Display Current User:
If you have read in a User or have created a User this will display the information about the current user. This will trigger the user.display_user() method

Display Users: choosing this option should trigger the Users object’s display_users. This should output information about all of the users from the list.

Exit: this should exit the program. If exit is not selected, the program should loop and display the options again.

Program Output

User Menu ---------
[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 4

There is no current user to display

User Menu ---------
[1] Create User

[2] Read User

[3] Delete User [4] Display User [5] Display Users [6] Exit

What would you like to do? 5

User List
---------
There are no users!

User Menu

---------
[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 1
What is the user's name? John
How many wins does John have? 300

User Menu ---------

[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 1
What is the user's name? John
How many wins does John have? 200

[!]Error: This User already exists.

User Menu ---------
[1] Create User

[2] Read User

[3] Delete User

[4] Display User

[5] Display Users [6] Exit

What would you like to do? 2

What is the user's name? Claire

[!]Error: This User doesn't exist.

User Menu ---------
[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 1
What is the user's name? Claire
How many wins does Claire have? 10

User Menu ---------
[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 1
What is the user's name? Suzy
How many wins does Suzy have? 200

User Menu ---------
[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 2

What is the user's name? John

User Menu ---------
[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 4

Username: John

User Menu ---------
[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 5
User List
---------

User Menu ---------
[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 2

What is the user's name? Claire

User Menu ---------
[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 4

Username: Claire

User Menu ---------
[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 3

What is the user's name? Suzy

Suzy has been removed!

User Menu ---------
[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 5
User List
---------

User Menu

---------
[1] Create User

[2] Read User
[3] Delete User

[4] Display User

[5] Display Users

[6] Exit

What would you like to do? 6

Thanks for using the user manager!

Testing

Run the program you write and verify that the information entered matches the information displayed and that the input prompts and output utilize the format specification provided.

_username To store the name of the player _wins To store the number of wins _ties To store the number of ties _losses To store the number of losses

Explanation / Answer

CODE:

Please have all the 3 files in same location.
-----------------------------------------------------------------------------------------------------
User.py

class User():
   _username = None
   _wins = None
   _ties = None
   _losses = None
  
   def __init__(self, userName, wins, ties, losses):
       self._username = userName
       self._wins = wins
       self._ties = ties
       self._losses = losses
          
   def get_username(self):
       return self._username
      
   def get_user_wins(self):
       return self._wins
      
   def set_user_wins(self, wins):
       self._wins = wins
      
   def get_user_ties(self):
       return self._ties
      
   def set_user_ties(self,ties):
       set_user_ties = ties
      
   def get_user_losses(self):
       return self._losses

   def set_user_losses(self,loss):
       set_user_losses = loss
      
   def display_user(self):
       print "Name" + " " + "Wins" + " " + "Ties" + " " + "Losses"
       print str(self._username) + " " + str(self._wins) + " " + str(self._ties) + " " + str(self._losses)
-----------------------------------------------------------------------------------------------------

Users.py

from User import User

class Users():
   _userList = None
  
   def __init__(self):
       self._userList = []
      
   def create_user(self, userObject):
       if len(self._userList) > 0:
           for user in self._userList:
               if userObject.get_username() == user.get_username():
                   print "[!]Error: This User already exists."
                   return False
          
       self._userList.append(userObject)
       return True
      
   def read_user(self, name):
       for user in self._userList:
           if user.get_username() == name.strip():
               return user
      
       return None
      
   def delete_user(self, userName):
       for user in self._userList:
           if user.get_username() == userName.strip():
               self._userList.remove(user)
               return True
       return False
      
   def display_user(self, name):
       for user in self._userList:
           if user.get_username() == name.strip():
               user.display_user()
               return True
       return False
              
   def display_users(self):
       for user in self._userList:
           print user.display_user()
      
----------------------------------------------------------------------------------------------
Usermanager.py

from Users import Users
from User import User

users = Users()

while True:
   print "1. Create User"
   print "2. Read User"
   print "3. Delete User"
   print "4. Display User"
   print "5. Display Users"
   print "6. Exit"
  
   choice = int(input("What would you like to do ?"))
   if(choice == 6):
       break
  
   if(choice == 1):
       name = input("What is the user's name? ")
       msg = "How many wins does " + name + " have?"
      
       wins = input(msg)
      
       user = User(name.strip(),wins.strip(),0,0)
       users.create_user(user)

   elif(choice == 2):
       name = input("What is the user's name?")
       retVal = users.read_user(name)
      
       if(retVal is None):
           print "[!]Error: This User doesn't exist."
          
       else:
           print retVal.display_user()
          
   elif(choice == 3):
       name = input("What is the user's name?")
       if(users.delete_user(name)):
           msg = name + " has been removed!"
           print msg
          
       else:
           print "[!]Error: This User doesn't exist."
          
   elif(choice == 4):
       name = input("What is the user's name?")
       if(not users.display_user(name)):
           print "[!]Error: This User doesn't exist."
  
   elif(choice == 5):
       users.display_users()
      
   else:
       break
          
----------------------------------------------------------------------------------------------

Output:

$python Usermanager.py
1. Create User
2. Read User
3. Delete User
4. Display User
5. Display Users
6. Exit
What would you like to do ?1
What is the user's name? julie
How many wins does julie have?12
1. Create User
2. Read User
3. Delete User
4. Display User
5. Display Users
6. Exit
What would you like to do ?1
What is the user's name? harry
How many wins does harry have?56
1. Create User
2. Read User
3. Delete User
4. Display User
5. Display Users
6. Exit
What would you like to do ?5
Name Wins Ties Losses
julie 12 0 0
None
Name Wins Ties Losses
harry 56 0 0
None
1. Create User
2. Read User
3. Delete User
4. Display User
5. Display Users
6. Exit
What would you like to do ?4
What is the user's name?julie
Name Wins Ties Losses
julie 12 0 0
1. Create User
2. Read User
3. Delete User
4. Display User
5. Display Users
6. Exit
What would you like to do ?2
What is the user's name?harry
Name Wins Ties Losses
harry 56 0 0
None
1. Create User
2. Read User
3. Delete User
4. Display User
5. Display Users
6. Exit
What would you like to do ?3
What is the user's name?harry
julie
harry
harry
harry
harry has been removed!
1. Create User
2. Read User
3. Delete User
4. Display User
5. Display Users
6. Exit
What would you like to do ?5
Name Wins Ties Losses
julie 12 0 0
None
1. Create User
2. Read User
3. Delete User
4. Display User
5. Display Users
6. Exit
What would you like to do ?3
What is the user's name?harry
julie
harry
[!]Error: This User doesn't exist.
1. Create User
2. Read User
3. Delete User
4. Display User
5. Display Users
6. Exit
What would you like to do ?6