Hello. I'm having some trouble understanding linked lists. I've been working on trying to figure this out all week, reading through the text, looking at examples and I still can't figure it out. This is due for me by midnight tonight, so I'm reaching out as a last ditch effort for help on getting a solution.

Here is the problem:

Write a program that lets the user enter four quarterly sales figures for all the divisions of a company.

Prompt the user for the number of divisions and enter for each division the sales for four quarters.

Create a DivisionSales class that contains
double salesPerQuarter[4]
double getSales(int quarter)

Create a linked list of DivisionsSales (do not use a standard template).

Display the following information:

  • A list of the sales figures by division by quarter
  • Each division's increase or decrease from the previous quarter.
  • The total sales by quarter.
  • The company's increase of decrease from the previous quarter.
  • The average sales for all divisions for each quarter.
  • The division with the highest sales for each quater.

Also, do not accept a negative number for divisions or sales amounts.


What I can't figure out is how to communicate the divisions and sales in a linked list. If it was a 2D array, I would zip right through this.

Also, I'm having trouble understanding how to get my append constructor to work in my driver.

I'm completely lost! Any help would be greatly appreciated!!

-Sammy

import java.util.Scanner;

public class QuarterlySales 

{

	public static void main(String[] args) 
	{
	
		List qSales = new List();
		int numDivisions = 0;
		double total = 0.0, min, max;
		Scanner keyboard = new Scanner(System.in);
		
		// get number of divisions
		System.out.print("Enter number of divisions: ");
		numDivisions = keyboard.nextInt();
		
		// prompt user for sales for each quarter and store it in a linked list
		for (int counter = 0; counter < numDivisions; counter++)
		{
			double[] salesAmount = new double[4];
			for(int j = 0; j < 4; ++j) {
				System.out.print("\tSales for quarter " + (j+1)+ "" + "Division: " + numDivisions);
				salesAmount[j] = keyboard.nextDouble();
			}
			qSales.append(new DivisionSales(salesAmount));		
		
	}
		
		// display data
		ListNode nextNode = qSales.getHead();
		
		if (nextNode == null)
			min = max = 0;
		else
			min = max = nextNode.getSales();
		
		while (nextNode != null)
		{
			System.out.println("Sales for the quarter " + nextNode.getSales());
			
			total += nextNode.getSales();
			
			if (min > nextNode.getSales())
				min = nextNode.getSales();
			
			if (max < nextNode.getSales())
				max = nextNode.getSales();
			
			nextNode = nextNode.getNext();
		}
		
		// calculate total, avarage, min and max
		System.out.println("The total sales is: " + total);
		System.out.println("The average sales is: " + total / numDivisions);
		System.out.println("The minimum sales is: " + min);
		System.out.println("The maximum sales is: " + max);
}}
public class DivisionSales {

	private double amt;
	public double[] salesPerQuarter;

	public DivisionSales(double[] amount)
	{
		salesPerQuarter = amount;
	}
	
	public void setSales(double salesAmount)
	{
		amt = salesAmount;
	}
	
	public double getSales()
	{
		return amt;
	}
}
public class ListNode 
{
	private double amt;
	private ListNode nextNode;
	
	
	public ListNode()
	{
		amt = 0;
		nextNode = null;
	}
	
	public ListNode(double salesAmount)
	{
		amt = salesAmount;
		nextNode = null;
	}
	
	public void setNext(ListNode next)
	{
		nextNode = next;
	}
	
	public ListNode getNext()
	{
		return nextNode;
	}
	
	public void setSales(double salesAmount)
	{
		amt = salesAmount;
	}
	
	public double getSales()
	{
		return amt;
	}
}
public class List 
{
	private ListNode head;
	
	public List()
	{
		head = null;
	}
	
	
	
	public void append(double sale)
	{
		ListNode newNode = new ListNode(sale);
		
		if (head == null)		// empty list
		{
			head = newNode;
		}
		else					// list contains data
		{
			// find last item in list
			ListNode nextNode = head;
			
			while (nextNode.getNext() != null)
			{
				nextNode = nextNode.getNext();
			}
			
			nextNode.setNext(newNode);
		}
	}

	public ListNode getHead()
	{
		return head;
	}

	public void setHead(ListNode listNode)
	{
		head = listNode;
	}

}

Very quickly: you are trying to put raw sales data into your linked list. That's wrong.
The linked list should contain instances of DivisionSales.
You should create new instances of DivisionSales from the user's data and add those to the list. For the reporting you iterate through the list getting all the instances of DivisionSales in turn; you can use each instance of DivisionSales to get its quarterly data or whatever and do the required analyses.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.