I have made 2 classes; Customer and Repair.
I have a map<String, Customer>(customerDetails). Customer objects hold a map<String, Repair>(repairs).
My GUI has 2 JTextAreas, notes and results.
In notes ou type some notes and when you choose save the notes appear in results and a new Repair is created with the notes as an attribute and added to repair map for the chosen Customer
I have a method processEditRepair() that when a string that matches a key in map repair is entered the the notes of that Repair object appear in the GUI notes JTextArea for editing. How would I go about saving these notes to ther same Repair object, overwriting the old notes?
Here is my processEditRepair() method

    private void processEditRepair()
    {
        String ref = gui.refText.getText().toUpperCase();
        Customer cus;

        readFile();

        if(ref.length() == 6)
        {
            cus = getCustomerRepair();
            notesString = cus.repairs.get(ref).getNotes(); 
            gui.nameText.setText(cus.getName());
            gui.notes.setText(notesString);
        }
    }

Thanks for looking.

Thanks John, but have you any idea on how I would go about saving the edited details, overwriting the old details?
Thanks
Glen..

I chaned my processEditRepair method to return the Customer object, it now looks like this.

    private Customer processEditRepair()
    {
        String ref = gui.refText.getText().toUpperCase();
        Customer cus = getCustomerRepair();        
        readFile();

        if(ref.length() == 6)
        {
            cus = getCustomerRepair();
            notesString = cus.repairs.get(ref).getNotes();
            gui.notes.setText(notesString);
        }
        return cus;
    }

I made a method saveEditRepair() to create a new Repair object and set it notes attribute to gui.notes.getText(). I can't get this t9o work though. Here it is..

        if(!ref.equals(""))
        {
            customer = processEditRepair();
            if(customer.repairs.containsKey(ref))
            {
                repair = customer.repairs.get(ref);
                repair.setNotes(notesString);
                customer.repairs.put(ref, repair);
            }
        }

Any suggestions?
Thanks

repair.setNotes(notesString); will update the Repair object in the map, so that's all you need to do. The put on line 8 is not needed.
Apart from that, as always, use a load of print statements to see exactly what your code is doing and what the actual values are.

Hi,
I'm still having problems here.

My metho processEditRepair() that puts the notes of the Repair object into the GUI notes JTextArea for editing remains unchanged. Here it is

    private Customer processEditRepair()
    {
        String ref = gui.refText.getText().toUpperCase();
        Customer cus = getCustomerRepair();        
        readFile();

        if(ref.length() == 6)
        {
            cus = getCustomerRepair();
            notesString = cus.repairs.get(ref).getNotes();
            gui.notes.setText(notesString);

        }
        return cus;

    }

My processSaveEdit() method is supposed to save the edited notes to the Repair object. But when I edit the notes then choose the menuitem that fire processSaveEdit() it reverts back to the origionaI notes that I wanted to edit in the first place! Here is the method.

    private void processSaveEdit()
    {
        String ref = gui.refText.getText();
        Customer customer;
        Repair repair;


        readFile();

        if(ref.equals(""))
        {
            customer = processEditRepair();
            repair = customer.repairs.get(ref);
            if(!customer.repairs.containsKey(ref))
            {

                repair.setNotes(notesString);
                customer.repairs.put(ref, repair);
                System.out.print(repair);
            }

        }

        writeFile();

    }

Where is it going wrong?!
Thanks
Glen..

Edited 4 Years Ago by GlenRogers

This looks a bit like it's a cross between a create-a-new-repair and an update-an-existing-repair that won't do either correctly.
Like I said before, repair.setNotes is enough, it's completely redundant to re-add the repair to the map.
What's missing in the code you posted is the bit where you get the text from the text field where the new notes have been enterd/edited.
Also, you only try to update the repair if there is no ref (!) and then if the ref is NOT (!) found - that seems exactly the opposite of what it should be!

This is my method now, its still doing the same thing!

    private void processSaveEdit()
    {
        String ref = gui.refText.getText();
        Customer customer;

        readFile();

        if(!ref.equals(""))
        {
            customer = processEditRepair();
            if(customer.repairs.containsKey(ref))
            {
                customer.repairs.get(ref).setNotes(gui.notes.getText());
                System.out.print(customer.repairs.get(ref));
            }

        }

        writeFile();
    }

That looks a lot better, can't see any obvious faults, so...
Same advice as always - lots of print statements so you can see what's being executed and where the values are going wrong

When I write a print statement

System.out.println(gui.notes.getText();

Before trying to update the Repair objects notes, the output is the string that was originally put into notes and not the string I changed it to!

Any ideas why it is doing this??

  1. Is there some piece of code that's overwriting your input with the original value when you press the button?
  2. Have you managed to declare or create two gui variables or notes variables?
  3. Did you try that print as the very first statement in the processSaveEdit method?

When I put that print statement at the very start of the method, it ouputs what it should(the edited notes). So why would it somehow change between the start of the methood and where it is supposed to actually set the notes of the Repair object?

Some piece of code that you are executing inbetween the two is re-setting it. This is easy to find, it just a bit tedious, that's all: just keep moving your print statements to find out execatly which method call is changing it, then go into that method and find out why/where

I fixed it by declaring a Sting str = gui.notes.getText() at the start, then setting the Repair objects notes to that Stirng.
The methood now looks like this.

    private void processSaveEdit()
    {
        String ref = gui.refText.getText();
        String str = gui.notes.getText();;
        Customer customer;

        readFile();


        if(!ref.equals(""))
        {
            customer = processEditRepair();
            if(customer.repairs.containsKey(ref))
            {                
                customer.repairs.get(ref).setNotes(str);
            }
        }
        getCustomerRepair();// This updates the result JTextArea to the amended Customer object
        writeFile();
    }

This works but does it look ok? I mean could it be better?
Thanks

This question has already been answered. Start a new discussion instead.