sorting price in ascending order

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 680 Views - Last Post: 03 June 2019 - 10:17 AM Rate Topic: -----

#1 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

sorting price in ascending order

Posted 31 May 2019 - 11:46 AM

do you can help me how to sorting the price ? i try to learn from your previous topic/solution in sorting with mine but i still cannot solve it. there is an error when it is incompatible when i try your previous sorting.this is my listNode and linked list. i really need your help.

Attached image(s)

  • Attached Image
  • Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: sorting price in ascending order

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: sorting price in ascending order

Posted 31 May 2019 - 11:49 AM

Quote

i try to learn from your previous topic/solution in sorting with mine but i still cannot solve it.

Which topics?


Quote

there is an error when it is incompatible

Copy paste the error message here.

Also please copy/paste your current code and your current attempt at sorting.
Was This Post Helpful? 0
  • +
  • -

#3 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 31 May 2019 - 11:45 PM

i try to follow this sample My link

and this is my coding. error at the bubble sort where there is incompatible type lastNode = new ListNode(0);

public class LinkedList{
    private ListNode firstNode;
    private ListNode lastNode;
    private ListNode currNode;
    private ListNode head;
    private ListNode a;
    private String type;
    
    public LinkedList(String t){
        type = t;
        firstNode = lastNode = currNode = null;
    }
    
    public LinkedList(){
        this("linked list");
    }
    
    public void insertAtFront(bedMattress insertItem){
        if(isEmpty())
            firstNode = lastNode = new ListNode(insertItem);
        else
            firstNode = new ListNode(insertItem,firstNode);
    }
    
    public void insertAtBack(bedMattress insertItem){
        if(isEmpty())
            firstNode = lastNode = new ListNode(insertItem);
        else
            lastNode = lastNode.next = new ListNode(insertItem);
    }
    
    public void insertAfter(bedMattress insertItem, ListNode next){
        ListNode tmp = firstNode;
        while(tmp != null && !tmp.data.equals(insertItem))
            tmp = tmp.next;
        
        if(tmp != null){
            tmp.next = new ListNode(insertItem,tmp.next);
        }
    }
    
    public Object removeFromFront() throws EmptyListException{
        Object removeItem = null;
        if(isEmpty())
            throw new EmptyListException(type);
            
        removeItem = firstNode.data;
        
        if(firstNode.equals(lastNode))
            firstNode = lastNode = null;
        else
            firstNode = firstNode.next;
            
        return removeItem;
    }
    
    public Object removeFromBack()throws EmptyListException{
        Object removeItem = null;
        if(isEmpty())
            throw new EmptyListException(type);
            
        removeItem = lastNode.data;
        
        if(firstNode.equals(lastNode))
            firstNode = lastNode = null;
        else{
            ListNode current = firstNode;
            while(current.next != firstNode)
                current = current.next;
            lastNode = current;
            current.next = null;
        }
        return removeItem;
    }
    
    public boolean isEmpty(){
        return firstNode == null;
    }
    
    public Object getFirst(){
        if(isEmpty())
            return null;
        else{
            currNode = firstNode;
            return currNode.data;
        }
    }
    
    public bedMattress getNext(){
        if(currNode != lastNode){
            currNode = currNode.next;
            return currNode.data;
        }
        else
            return null;
    }
    
    public void bedDetail(String search){
        currNode = firstNode;
        while(currNode!= null){
            if(currNode.data.getBed().equalsIgnoreCase(search)){
                System.out.println("Type of Bed: " + currNode.data.getBed());
                System.out.println("Bed Price:RM " + currNode.data.calcBed());
            }
            currNode = currNode.next;
        }
    }
    
    public void mattDetail(String search){
        currNode = firstNode;
        while(currNode!= null){
            if(currNode.data.getMattress().equalsIgnoreCase(search)){
                System.out.println("Type of Mattress: " + currNode.data.getMattress());
                System.out.println("Mattress Price:RM " + currNode.data.calcMattress());
            }
            currNode = currNode.next;
        }
    }
    
    public void bubbleSort(){
        lastNode = new ListNode(0);
        lastNode.next = head;
        head = lastNode;
        
        while(lastNode.next != null){
            currNode = lastNode.next;
            firstNode = lastNode;
            a = currNode.next;
            while(a != null){
                if(a.data.calcBed() < currNode.data.calcBed()){
                    ListNode temp = a.next;
                    a.next = firstNode.next;
                    firstNode.next = currNode.next;
                    currNode.next = temp;
                    firstNode = a;
                    a = temp;
                }
                else{
                    a = a.next;
                    currNode = currNode.next;
                    firstNode = firstNode.next;
                }
            }
            lastNode = lastNode.next;
        }
        head = head.next;
    }
}
:code:

This post has been edited by modi123_1: 01 June 2019 - 03:48 AM
Reason for edit:: In the future please use the [code] tag button in the editor

Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,833
  • Joined: 25-December 13

Re: sorting price in ascending order

Posted 01 June 2019 - 04:32 AM

Where is the definition of the LastNode class? Does its constructor take an int value?

It's better if the full text of the error is copied and pasted, not just part of one line.
Was This Post Helpful? 0
  • +
  • -

#5 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 01 June 2019 - 04:54 AM

sorry ..the constructor take an object as parameter.
my ListNode:
public class ListNode{
    bedMattress data;
    ListNode next;
    
    ListNode(bedMattress o){
        this(o,null);
    }
    
    ListNode(bedMattress o, ListNode nextNode){
        data = o;
        next = nextNode;
    }
    
    bedMattress getbedMattress(){
        return data;
    }
    
    ListNode getLink(){
        return next;
    }
}

This post has been edited by NormR: 01 June 2019 - 05:32 AM
Reason for edit:: Added code tags - Please use [CODE] button

Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,833
  • Joined: 25-December 13

Re: sorting price in ascending order

Posted 01 June 2019 - 05:31 AM

Quote

the constructor take an object as parameter.

Then coding a 0 as an argument would be incompatible.
new ListNode(0);

What is the purpose of the 0 arg?
What would work instead of a 0?

This post has been edited by NormR: 01 June 2019 - 05:33 AM

Was This Post Helpful? 0
  • +
  • -

#7 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 01 June 2019 - 05:56 AM

sorry Hunter, i don't quite understand since this is my first subject to learn..is it i must put an object as parameter since my listNode using an object ?
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,833
  • Joined: 25-December 13

Re: sorting price in ascending order

Posted 01 June 2019 - 06:11 AM

The code needs to use a compatible argument for the constructor. The int 0 is not good.
Why did you code a 0 as an argument? What was the purpose of that code?
Was This Post Helpful? 0
  • +
  • -

#9 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 01 June 2019 - 07:38 AM

alright .i corrected it . i declare my object in LinkedList at put as my parameter. so there is no error but my sorting is not work out..then i would like to ask why when i do method searching it is not come out .before this it came out but it display two times for bed and for mattress it not come out.
here the ouput:

Searching for bed selected....
NOT AVAILABLE....

Searching for mattress selected....
NOT AVAILABLE....
------------------------------------------
Sorting price in ascending order

Bed: Cabin Price of Bed: RM239.0
Mattress: Price of Mattress: RM0.0

and here my test class for method searching:

System.out.println("------------------------------------------");
temp = (bedMattress)bedMatt.getFirst();
String ansB;
do{
String srcB = JOptionPane.showInputDialog("Search for bed: ");
System.out.println("\nSearching for bed selected....");
if(temp.getBed().equalsIgnoreCase(srcB)){
bedMatt.bedDetail(srcB);
}
else{
System.out.println("NOT AVAILABLE....");
}
ansB = JOptionPane.showInputDialog("Research again [Yes||No]: ");
}
while(ansB.equalsIgnoreCase("Yes"));

temp = (bedMattress)bedMatt.getFirst();
String ansM;
do{
String srcM = JOptionPane.showInputDialog("Search for mattress: ");
System.out.println("\nSearching for mattress selected....");
if(temp.getMattress().equalsIgnoreCase(srcM)){
bedMatt.mattDetail(srcM);
}
else{
System.out.println("NOT AVAILABLE....");
}
ansM = JOptionPane.showInputDialog("Research again [Yes||No]: ");
}
while(ansM.equalsIgnoreCase("Yes"));
System.out.println("------------------------------------------");
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,833
  • Joined: 25-December 13

Re: sorting price in ascending order

Posted 01 June 2019 - 07:40 AM

Please be sure to wrap all posted code in code tags:Posted Image
Was This Post Helpful? 0
  • +
  • -

#11 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 01 June 2019 - 08:01 AM

sorry for troubling you. alright .i corrected it . i declare my object in LinkedList at put as my parameter. so there is no error but my sorting is not work out..then i would like to ask why when i do method searching it is not come out .before this it came out but it display two times for bed and for mattress it not come out.

here the ouput:

Searching for bed selected....
NOT AVAILABLE....

Searching for mattress selected....
NOT AVAILABLE....
------------------------------------------
Sorting price in ascending order

Bed: Cabin Price of Bed: RM239.0
Mattress: Price of Mattress: RM0.0

and here my test class for method searching:

System.out.println("------------------------------------------");
        temp = (bedMattress)bedMatt.getFirst();
        String ansB;
        do{
            String srcB = JOptionPane.showInputDialog("Search for bed: ");
            System.out.println("\nSearching for bed selected....");
            if(temp.getBed().equalsIgnoreCase(srcB)){
                bedMatt.bedDetail(srcB);
            }
            else{
                System.out.println("NOT AVAILABLE....");
            }
            ansB = JOptionPane.showInputDialog("Research again [Yes||No]: ");
        } 
        while(ansB.equalsIgnoreCase("Yes"));
        
        temp = (bedMattress)bedMatt.getFirst();
        String ansM;
        do{
            String srcM = JOptionPane.showInputDialog("Search for mattress: ");
            System.out.println("\nSearching for mattress selected....");
            if(temp.getMattress().equalsIgnoreCase(srcM)){
                bedMatt.mattDetail(srcM);
            }
            else{
                System.out.println("NOT AVAILABLE....");
            }
            ansM = JOptionPane.showInputDialog("Research again [Yes||No]: ");
        } 
        while(ansM.equalsIgnoreCase("Yes"));
        System.out.println("------------------------------------------");


Was This Post Helpful? 0
  • +
  • -

#12 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,833
  • Joined: 25-December 13

Re: sorting price in ascending order

Posted 01 June 2019 - 08:06 AM

How does the search code go through the items in the list? Is it looking at more items than just the first item in the list?

Change the print statements so that they print out each item from the list as it is tested for the match.

Also post the code for the BedMattress class so we can compile and execute the program for testing.
Was This Post Helpful? 0
  • +
  • -

#13 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 01 June 2019 - 08:11 AM

here the class bedMattress :

public class bedMattress
{
    private String type;
    private String typeBed;
    private String typeMattress;

    //normal constructor
    public bedMattress(String type,String typeBed,String typeMattress)
    {
        this.type = type;
        this.typeBed=typeBed;
        this.typeMattress=typeMattress;
    }
    
    //mutator
    public void setType(String type){
        type = type;
    }
    public void setBed(String typeBed)
    {
        typeBed=typeBed;
    }
    public void setPriceBed(double priceBed)
    {
        priceBed=priceBed;
    }
    public void setMattress(String typeMattress)
    {
        typeMattress=typeMattress;
    }
    public void setPriceMattress(double priceMattress)
    {
        priceMattress=priceMattress;
    }
    
    //accessor
    public String getType(){
        return type;
    }
    public String getBed()
    {
        return typeBed;
    }
    public String getMattress()
    {
        return typeMattress;
    }
    
    //method
    public double calcBed()
    {
        double priceBed=0.00;
        if(typeBed.equalsIgnoreCase("Cabin"))
        {
            priceBed=239;
            typeBed="Cabin";
        }
        else if(typeBed.equalsIgnoreCase("Bunk"))
        {
            priceBed=279;
            typeBed="Bunk";
        }
        else if(typeBed.equalsIgnoreCase("Ottoman"))
        {
            priceBed=289;
            typeBed="Ottoman";
        }
        else if(typeBed.equalsIgnoreCase("Guest"))
        {
            priceBed=299;
            typeBed="Guest";
        }
        else if(typeBed.equalsIgnoreCase("Divan"))
        {
            priceBed=329;
            typeBed="Divan";
        }
        else if(typeBed.equalsIgnoreCase("Sleigh"))
        {
            priceBed=349;
            typeBed="Sleigh";
        }
        else
        {
            priceBed=0.00;
        }
        return priceBed;
    }  
    
    public double calcMattress()
    {      
        double priceMattress=0.00;
        if(typeMattress.equalsIgnoreCase("Foam"))
        {
            priceMattress=890;
            typeMattress="Foam";
        }
        else if(typeMattress.equalsIgnoreCase("Innerspring"))
        {
            priceMattress=950;
            typeMattress="Innerspring";
        }
        else if(typeMattress.equalsIgnoreCase("MemoryFoam"))
        {
            priceMattress=1370;
            typeMattress="MemoryFoam";
        }
        else if(typeMattress.equalsIgnoreCase("Hybrid"))
        {
            priceMattress=1975;
            typeMattress="Hybrid";
        }
        else if(typeMattress.equalsIgnoreCase("Latex"))
        {
            priceMattress=2200;
            typeMattress="Latex";
        }
        else
        {
            priceMattress=0.00;
        }
        return priceMattress;
    }
    
    public String toString()
    {
        return //"\nType: "+ type 
                "\nBed: "+ typeBed + "\tPrice of Bed: RM" + calcBed()
                +"\nMattress: "+ typeMattress + "\tPrice of Mattress: RM" + calcMattress();
    }
}



for the print statement i simply do just right this where i call the method in LinkedList
public void bedDetail(String search){
        currNode = firstNode;
        while(currNode!= null){
            if(currNode.data.getBed().equalsIgnoreCase(search)){
                System.out.println("Type of Bed: " + currNode.data.getBed());
                System.out.println("Bed Price:RM " + currNode.data.calcBed());
            }
            currNode = currNode.next;
        }
    }
    
    public void mattDetail(String search){
        currNode = firstNode;
        while(currNode!= null){
            if(currNode.data.getMattress().equalsIgnoreCase(search)){
                System.out.println("Type of Mattress: " + currNode.data.getMattress());
                System.out.println("Mattress Price:RM " + currNode.data.calcMattress());
            }
            currNode = currNode.next;
        }
    }


Was This Post Helpful? 0
  • +
  • -

#14 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • Posts: 5,833
  • Joined: 25-December 13

Re: sorting price in ascending order

Posted 01 June 2019 - 08:14 AM

What is printed when a search is done? The printed output should help you find out what is wrong with the program.
Was This Post Helpful? 0
  • +
  • -

#15 arexxa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-May 19

Re: sorting price in ascending order

Posted 01 June 2019 - 08:17 AM

when i input the data..everything there..but when i search it should search for what i input..but the ouput is "Not available" in if else statement. i do not know what is wrong with my searching method.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3