Re: Data Structure
On 03/09/2014 01:13 PM, Michael Pablo wrote:
Hello Community!
I would like some help with this code:
The use of this code is merely demonstrative and academic, as everybody can see it is not too organized.
I hope to get some help with this problem :)
The problems is with the nextLine();
and posteriorly with pointers.
I would say anteriorly in this case
Are you having problems with PRODUTO proxProd?
I've taken the liberty to translate:
package scratch;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class LS_Nao_Ordenada {
// Defining class to represent each element of the list
private static class PRODUTO {
public int numProd;
public String nomeProd;
public char tipoDeProduto;
public float percentuaisImposto;
public float precoProd;
PRODUTO proxProd;
};
public static void main(String[] args) throws IOException {
Scanner entrada = new Scanner(System.in);
/*
* the list is empty , then the object start have the value null
the object
* will contain the start address of the first element of the list
*/
PRODUTO inicioPROD = null;
/*
* close the object will contain the address of the last elemnteo
the list
*/
PRODUTO fimPROD = null;
// AuxPROD the object is a helper object
PRODUTO auxPROD;
// AnteriorPROD the object is a helper object
PRODUTO anteriorPROD;
// Variable to total product
float totalPRODIMP = 0.0f;
// Query product
String consultaPROD = " ";
// Showing the options menu
int op, numero, found;
do {
System.out.println(" \nmenu options \n");
System.out
.println(" 1 - Register the product at the beginning of the
queue ");
System.out.println(" 2 - Register product at the end of the queue ");
System.out.println(" 3 - Consult the price of a product ");
System.out.println(" 4 - Delete product");
System.out.println(" 5 - Empty list ");
System.out.println(" 6 - Exit ");
System.out.println(" Enter your choice : ");
op = entrada.nextInt();
if (op < 1 || op > 6) {
System.out.println(" Invalid option ! ");
}
if (op == 1) {
PRODUTO novoPROD = new PRODUTO();
Random generatorPRO = new Random();
novoPROD.numProd = generatorPRO.nextInt();
/*
* System.out.println ( " Enter the Product name : " ) ;
* Entrada.nextLine = ();
*/
System.out
.println(" Enter the type of product to be inserted at the
beginning of the queue: ");
novoPROD.tipoDeProduto = (char) System.in.read();
System.out.println(" Enter the price of the product to be
inserted : ");
novoPROD.precoProd = entrada.nextFloat();
System.out.println(" Enter the tax item inserted : ");
novoPROD.percentuaisImposto = entrada.nextFloat();
if (null == inicioPROD) {
// List was empty and the inserted element will be the first
// and last
inicioPROD = novoPROD;
fimPROD = novoPROD;
novoPROD.proxProd = null;
} else {
/*
* the list already contains elements and the new element is
inserted
* at the beginning of the list
*/
// points to start the next list as eg element 2 prox <--- 1
// element .
novoPROD.proxProd = inicioPROD;
// " Pointer " to new element at the beginning of the list
inicioPROD = novoPROD;
}
System.out
.println(" Type of product and inserted at the beginning of
the tax list ! ");
}
if (op == 2) {
PRODUTO novoPROD = new PRODUTO();
Random generatorPRO = new Random();
novoPROD.numProd = generatorPRO.nextInt();
System.out.println(" Enter the Product name : ");
novoPROD.nomeProd = entrada.nextLine();
System.out
.println(" Enter the type of product to be inserted at the
beginning of the queue: ");
novoPROD.tipoDeProduto = (char) System.in.read();
System.out.println(" Enter the price of the product to be
inserted : ");
novoPROD.precoProd = entrada.nextFloat();
System.out.println(" Enter the tax item inserted : ");
novoPROD.percentuaisImposto = entrada.nextFloat();
if (null == inicioPROD) {
/*
* List was empty and the inserted element will be the first
and the
* last
*/
inicioPROD = novoPROD;
fimPROD = novoPROD;
novoPROD.proxProd = null;
} else {
/*
* the list already contains elements and the new element will be
* inserted end of the list
*/
// Holds the record for the new list ( next)
fimPROD.proxProd = novoPROD;
// " Pointer " to last element.
fimPROD = novoPROD;
fimPROD.proxProd = null;
}
System.out.println(" Sign inserted at the end of the list ! ");
}
if (op == 3) {
System.out.println(" Enter the item to be queried : ");
consultaPROD = entrada.nextLine();
if (null == inicioPROD) {
System.out.println(" List of empty registry ! ");
} else {
/*
* The list contains elements and these will be shown from
start the
* end
*/
auxPROD = inicioPROD;
while (auxPROD != null) {
if (auxPROD.nomeProd.equals(consultaPROD)) {
totalPRODIMP = (auxPROD.percentuaisImposto *
auxPROD.precoProd)
+ auxPROD.precoProd;
auxPROD.proxProd = null;
auxPROD = auxPROD.proxProd;
} else {
auxPROD = auxPROD.proxProd;
}
}
System.out.println(" Product Name : " + auxPROD.nomeProd);
System.out.println(" The value of the product was R $ "
+ totalPRODIMP);
}
}
if (op == 4) {
if (null == inicioPROD) {
/* List is empty */
System.out.println(" empty list ! ");
} else {
/*
* The list contains elements and the element to be removed
must be
* entered
*/
System.out.println(" \nEnter the product to be removed : ");
consultaPROD = entrada.nextLine();
// All occurrences of the list , equal to the number entered
// ,
// will be removed
auxPROD = inicioPROD;
anteriorPROD = null;
found = 0;
while (auxPROD != null) {
if (auxPROD.nomeProd.equals(consultaPROD)) {
/*
* The number entered was found in the list and will removed
*/
found = found + 1;
if (auxPROD == inicioPROD) {
/* the number to be removed is first in list */
inicioPROD = auxPROD.proxProd;
auxPROD = inicioPROD;
} else if (auxPROD == fimPROD) {
/*
* the number to be removed is the last of the list
*/
anteriorPROD.proxProd = null;
fimPROD = anteriorPROD;
auxPROD = null;
} else {
/*
* The number to be removed is in the middle of the list
*/
anteriorPROD.proxProd = auxPROD.proxProd;
auxPROD = auxPROD.proxProd;
}
} else {
anteriorPROD = auxPROD;
auxPROD = auxPROD.proxProd;
}
}
if (found == 0) {
System.out.println(" Product not found ");
} else if (found == 1) {
System.out.println(" Product 1 time removed ");
} else {
System.out.println(" Material removed " + found + " times ");
}
}
}
if (op == 5) {
if (null == inicioPROD) {
/* list is empty */
System.out.println(" Empty list ");
} else {
/* List is empty */
inicioPROD = null;
System.out.println(" empty list ! ");
}
}
} while (op != 6);
}
}