Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #27704 > unrolled thread
| Started by | Michael Pablo <skypabloboy@gmail.com> |
|---|---|
| First post | 2014-03-09 10:13 -0700 |
| Last post | 2014-03-09 23:45 -0400 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
Data Structure Michael Pablo <skypabloboy@gmail.com> - 2014-03-09 10:13 -0700
Re: Data Structure markspace <markspace@nospam.nospam> - 2014-03-09 15:33 -0700
Re: Data Structure Jeff Higgins <jeff@invalid.invalid> - 2014-03-09 23:45 -0400
| From | Michael Pablo <skypabloboy@gmail.com> |
|---|---|
| Date | 2014-03-09 10:13 -0700 |
| Subject | Data Structure |
| Message-ID | <7366a5dd-ee0e-4b07-8664-45e58b399515@googlegroups.com> |
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 inserido no 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 inserido no 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 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.
Regard,
Michael
[toc] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2014-03-09 15:33 -0700 |
| Message-ID | <lfiq77$n4h$1@dont-email.me> |
| In reply to | #27704 |
On 3/9/2014 10:22 AM, Stefan Ram wrote: > E qual é o problema neste caso? Warning: smells like homework.
[toc] | [prev] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2014-03-09 23:45 -0400 |
| Message-ID | <lfjcie$api$1@dont-email.me> |
| In reply to | #27704 |
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);
}
}
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web