Hi guys, I been working on a project that involves in managing a phone inventory for a company.
So for this project we are to use classes. For the most part I got everything except two areas of
code that I am having trouble with. I have two classes, Class Phone and Class Store.
In the class Store, where it has the method def removePhone,
when the user says yes, the counter should minus one. When the user says no, the counter should
not
minus one. Instead, the counter is still subtracting even if the user input is "no".
Any ideas to fix that?

Actually The most important part I am having trouble with is not getting is the def update_phone_info(self) method, which is the
last one in the class Store. Here the user has 5 options to choose from. So if they choose one,
it should do only one and ignore the other 4. The problem is that it's still doing those other
4 no matter what. I was thinking can I use "and" or something like that as a boolean but I don't know
if that will work? If anyone has any suggestions, it would be a great help. I put the whole program so if need be, can be understood from it and have bold the area
of code that is of importance.

# this is the first class
class Phone:

    # this is the constructor
    def __init__(self, manufacturer, model, retail, quantity):
        self.manufact = manufacturer
        self.model = model
        self.retail_price = retail
        self.quantity = quantity
    
    # these next 4 are my mutators:
    def set_manufact(self, manufacturer):
        self.manufact = manufacturer

    def set_model(self,model):
        self.model = model

    def set_retail_price(self,retail):
        self.retail = retail

    def set_quantity(self, quantity):
        self.quantity = quantity
#-------------------------------------------------------------------
    # these next 4 are the accessors:
    def get_manufact(self):
        return self.manufact

    def get_model(self):
        return self.model

    def get_retail_price(self):
        return self.retail_price

    def get_quantity(self):
        return self.quantity
#------------------------------------------------------------------

# this is the 2nd class, a class for Store
# using as inheritance by putting Phone inside paranthesis.
class Store(Phone):

    # this is the constructor
    def __init__(self, manufacturer, model, retail, quantity):

         self.phones = []
         self.number_of_phones = 0
         Phone.__init__(self, manufacturer, model, retail, quantity)
      
    # adds phone to store inventory
    # add phone to inventory if not in or if it is in inventory.
    def addPhone(self):
        if Phone not in self.phones:
            self.phones.append(Phone)
            self.number_of_phones += 1
        else:
            self.number_of_phones += 1

   [B] [I]# removes phone from the store inventory
    def removePhone(self):
        y=1
        n=0
        a=input('are you sure you want to delete this phone y or n >>')
        if y:
            print(self.manufact,self.model,self.retail_price)
            self.number_of_phones -= 1
        else:
            pass[/I][/B]
            

    
    # to return the amount of phones
    def get_number_of_phones(self):
        return self.number_of_phones
    

 [B]   [I]def update_phone_info(self):
        print("Please choose an option to update.")
        print('[1] manufacturer')
        print('[2] model')
        print('[3] retail')
        print('[4] quantitiy')
        print('[5] EXIT ')
        user_choice=input('>')[/B]
        x=1
        y=2
        z=3
        w=4
        v=5
        x=input(str('enter new manufacturer'))
        self.manufact=x

    
        y=input(str("enter new model"))
        self.model=y
     
        z=input(str('enter adjusted retail price'))
        self.retail_price=z

    
        w=input(str("enter the quantity wanted"))
        self.quantity=w

    
        v=print ('thank you goodbye')
        pass[/I][/B]

Edited 5 Years Ago by Sprewell184: Did not correctly put the [code]

In removePhone(), if y: should be if a == 'y': . In the same way, you must select an action in update_phone_info(), depending on the user choice:

if user_choice == '1':
            x = input(str('enter new manufacturer'))
            self.manufact = x
        elif user_choice == '2':
            y = input(str("enter new model"))
            self.model = y
        elif user_choice == '3':
            z = input(str('enter adjusted retail price'))
            self.retail_price = z
        elif user_choice == '4':
            w = input(str("enter the quantity wanted"))
            self.quantity = w
        elif user_choice == '5':
            print ('thank you goodbye')
        else:
            pass # XXX what should we do if the user inputs 'abracadabra' ?

Edited 5 Years Ago by Gribouillis: n/a

thank you so much.

in the remove part, it's still not working.
Like for example, if I have 2 phones, and if I delete a phone, the counter should minus 1, then the phone should be 1.
Right now It stays at two no matter what. I spent alot of hours on that part lol and I am not sure what's going on.

what is this line with parent class of the Store class?

if Phone not in self.phones:
            self.phones.append(Phone)

As your system does not make so much sense to me, here is my different one (setters and getters are bad style in Python):

try:
    input = raw_input
except ValueError:
    pass

class Phone(object):
    def __init__(self, manufacturer, model, retail, quantity):
        self.manufacturer = manufacturer
        self.model = model
        self.retail_price = retail
        self.quantity = quantity
    
    def update_menu(self):
        "Changing manufacturer or model on existing phones does not make sense, only retail and quantity"
        while True:
            print('''
            CURRENT INFO:

            %s

            UPDATE MENU
            
            Please choose an option to update
            [1] retail price
            [2] storage quantity
            [3] EXIT
            ''' % self)
            user_choice=input('>')
            if user_choice == '1':
                self.retail_price = int(input('Enter adjusted retail price: '))
            elif user_choice == '2':
                self.quantity = int(input("Enter the current quantity: "))             
            elif user_choice == '3':
                print('Thank you, final phone info is:\n%s\n' % self)
                break
            else:
                print('Invalid choice, %r, enter again!' % user_choice)

    @property
    def storage_value(self):
        return self.quantity * self.retail_price

    def __repr__(self):
        return "Phone(manufacturer=%r, model=%r, retail=%r, quantity=%r)" % (
                self.manufacturer, self.model, self.retail_price, self.quantity
                )

    def __str__ (self):
        return "%r, storage value: $%.2f" % (self, self.storage_value)

class Store(list):
    def __init__(self, *args):
        # args allow us to enter store as arguments instead of sequence
        list.__init__(self, args)        

    def __repr__(self):
        return "Store%s" % (tuple(self),)
                                  
    def __str__(self):
        return '%r, total value $%.2f' % (self, self.storage_value)

    def find(self, manufacturer, model):
        for item_no, phone in enumerate(self):
            if phone.manufacturer == manufacturer and phone.model == model:
                return item_no

    def remove(self, manufacturer, model):
        del self[self.find(manufacturer, model)]

    def add(self, manufacturer, model, quantity):
        found = self.find(manufacturer, model)
        self[found].quantity += quantity
    
    @property
    def storage_value(self):
        return sum(phone.storage_value for phone in self)

my_store = Store(Phone('Nokia', 'C5', 123, 43), Phone('Nokia', 'E5', 234, 23))

print(my_store)
my_store[1].update_menu()
print(str(my_store))

print('Adding new Samsung Galaxy II')
my_store.append(Phone('Samsung', 'Galaxy II', 456, 33))
print(str(my_store))

print('Adding 10 Nokia E5')
my_store.add('Nokia', 'E5', 10)
print(str(my_store))

print('Removing Nokia C5')
del my_store[my_store.find('Nokia', 'C5')]
print(str(my_store))

Edited 4 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.