Data Structure

M

Michael Pablo

Hello Community!

I would like some help with this code:


import java.io.IOException;
import java.util.*;

public class LS_Nao_Ordenada
{

//Definindo Classe que representará cada elemento da lista

private static class PRODUTO
{
public int numProd;
public String nomeProd;
public char tipoDeProduto;
public float percentuaisImposto;
public float precoProd;
public PRODUTO proxProd;
}

public static void main(String args[]) throws IOException
{
Scanner entrada = new Scanner(System.in);

/*a lista está vazia, logo o objeto inicio têm o valor null
o objeto inicio conterá o endereço do primeiro elemento da lista*/
PRODUTO inicioPROD = null;
/*o objeto fim conterá o endereço do último elemnteo da lista*/
PRODUTO fimPROD = null;
// o objeto auxPROD é um objeto auxiliar
PRODUTO auxPROD;
// o objeto anteriorPROD é um objeto auxiliar
PRODUTO anteriorPROD;

//Variavel com o total do produto
float totalPRODIMP = 0.0f;
// Consulta de produtos
String consultaPROD = "";
// apresentando menu de opções
int op, numero, achou;

do
{
System.out.println("\nMenu opções\n");
System.out.println("1 - Cadastrar produto no inicio da fila");
System.out.println("2 - Cadastrar produto no fim da fila");
System.out.println("3 - Consultar o preço de um produto");
System.out.println("4 - Excluir produto");
System.out.println("5 - Esvazia Lista");
System.out.println("6 - Sair");
System.out.println("Digite sua opção: ");
op = entrada.nextInt();

if (op < 1 || op > 6)
{
System.out.println("Opção inválida!!!");
}

if (op == 1)
{
PRODUTO novoPROD = new PRODUTO();

Random generatorPRO = new Random();

novoPROD.numProd = generatorPRO.nextInt();
/*
System.out.println("Digite o nome do Produto: ");
= entrada.nextLine();
*/

System.out.println("Digite o tipo de produto a ser inseridono inicio da fila: ");
novoPROD.tipoDeProduto = (char)System.in.read();

System.out.println("Digite o preço do produto a ser inserido: ");
novoPROD.precoProd = entrada.nextFloat();

System.out.println("Digite o imposto do produto inserido: ");
novoPROD.percentuaisImposto = entrada.nextFloat();

if (inicioPROD == null)
{
// a lista estava vazia e o elemento inserido será o primeiro e o último
inicioPROD = novoPROD;
fimPROD = novoPROD;
novoPROD.proxProd = null;
}
else
{
/*a lista já contém elementos
e o novo elemento será inserido no início da lista*/
//aponta para o inicio da lista como proximo ex: elemento 2 prox <--- elemento 1.
novoPROD.proxProd = inicioPROD;
//"Ponteiro" para novo elemento no inicio da lista
inicioPROD = novoPROD;
}

System.out.println("Tipo de produto e imposto inserido no inicio da lista!!!");
}

if (op == 2)
{
PRODUTO novoPROD = new PRODUTO();

Random generatorPRO = new Random();

novoPROD.numProd = generatorPRO.nextInt();

System.out.println("Digite o nome do Produto: ");
novoPROD.nomeProd = entrada.nextLine();

System.out.println("Digite o tipo de produto a ser inseridono inicio da fila: ");
novoPROD.tipoDeProduto = (char)System.in.read();

System.out.println("Digite o preço do produto a ser inserido: ");
novoPROD.precoProd = entrada.nextFloat();

System.out.println("Digite o imposto do produto inserido: ");
novoPROD.percentuaisImposto = entrada.nextFloat();


if (inicioPROD == null)
{
/*A lista estava vazia e o elemento inserido será o primeiro
e o último*/
inicioPROD = novoPROD;
fimPROD = novoPROD;
novoPROD.proxProd = null;
}
else
{
/*a lista já contém elementos e o novo elemento será inserido
no fim da lista*/
//Armazena o cadastro para nova lista(proximo)
fimPROD.proxProd = novoPROD;
//"Ponteiro" para ultimo elemento.
fimPROD = novoPROD;
fimPROD.proxProd = null;
}
System.out.println("Cadastro inserido no fim da lista!!!");
}

if (op == 3)
{
System.out.println("Digite o produto a ser consultado: ");
consultaPROD = entrada.nextLine();

if (inicioPROD == null)
{
System.out.println("Lista de cadastro vazia!!!");
}
else
{
/*A lista contém elementos e estes serão mostrados do início
ao fim*/
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("Nome do Produto:"+ auxPROD.nomeProd);
System.out.println("O valor do produto foi de R$"+ totalPRODIMP);
}
}

if (op == 4)
{
if (inicioPROD == null)
{
/* a lista está vazia*/
System.out.println("Lista vazia!!");
}
else
{
/* a lista contém elementos e o elemento a ser removido deve
ser digitado*/

System.out.println("\nDigite o produto a ser removido: ");
consultaPROD = entrada.nextLine();

// todas as ocorrências da lista, iguais ao número digitado,
//serão removidas
auxPROD = inicioPROD;
anteriorPROD = null;
achou = 0;

while (auxPROD != null)
{
if (auxPROD.nomeProd.equals(consultaPROD))
{
/* o número digitado foi encontrado na lista e será
removido*/
achou = achou + 1;
if (auxPROD == inicioPROD)
{
/*o número a ser removido é o primeiro da lista*/
inicioPROD = auxPROD.proxProd;
auxPROD = inicioPROD;
}
else
if (auxPROD == fimPROD)
{
/*o número a ser removido é o último da lista*/
anteriorPROD.proxProd = null;
fimPROD = anteriorPROD;
auxPROD = null;
}
else
{
/* o número a ser removido está no meio da lista*/
anteriorPROD.proxProd = auxPROD.proxProd;
auxPROD = auxPROD.proxProd;
}
}
else
{
anteriorPROD = auxPROD;
auxPROD = auxPROD.proxProd;
}
}
if (achou == 0)
{
System.out.println("Produto não encontrado");
}
else
if (achou == 1)
{
System.out.println("Produto removido 1 vez");
}
else
{
System.out.println ("Produto removido "+achou+ " vezes");
}
}
}

if (op == 5)
{
if (inicioPROD == null)
{
/*a lista está vazia*/
System.out.println("Lista vazia");
}
else
{
/* a lista será esvaziada*/
inicioPROD = null;
System.out.println("Lista esvaziada!!!");
}
}
}
while (op != 6);
}
}



The use of this code is merely demonstrative and academic, as everybody cansee it is not too organized.

I hope to get some help with this problem :)

The problems is with the nextLine(); and posteriorly with pointers.

Regard,

Michael
 
J

Jeff Higgins

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);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top