1
Guys, I got this week a really fucked-up college job and I’m having a problem with a part of it... The thing is, I have to set up a system for a bracelet store, which gives the choice of size and pendants, each pendant and size with its length and price. So far what I learned was the basics of JAVA so the difficulty of assembling this system. However I managed to calm down, I’m only in trouble that I do not know how to make him repeat the parts of choice of pendant, in case the total size of pendant that the user choose does not close the size of the bracelet.. The code is heavily polluted by the fact that this is the knowledge that I have at the moment... If anyone has balls and tells me how to do I’m waiting. Hugs!
import java.util.Scanner;
public class Bichara_System{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Digite o tamanho da pulseira\nPequena (17cm) - R$180,00\nMédia (18cm) - R$200,00\nGrande (20cm) - R$240,00");
String pulseira = in.nextLine();
int valorpulseira = 0;
int tamanhopulseira = 0;
if(pulseira.toLowerCase().equals("pequena")){
pulseira = "pequena";
valorpulseira = 180;
tamanhopulseira = 17;
System.out.println("Total: R$180,00\nAgora escolha o pingente:");
} else if(pulseira.toLowerCase().equals("media")){
pulseira = "media";
valorpulseira = 200;
tamanhopulseira = 18;
System.out.println("Total: R$200,00\nAgora escolha o pingente:");
} else if(pulseira.toLowerCase().equals("grande")){
pulseira = "grande";
valorpulseira = 240;
tamanhopulseira = 20;
System.out.println("Total: R$240,00\nAgora escolha o pingente:");
}
System.out.println("Bichinhos (1,3cm) - R$225,00\nBonequinhos (1,5cm) - R$185,00\nBerloques (0,5cm) - R$200,00\nCubinhos (1cm) - R$190,00\nRolinho (2cm) - Prata, Pedra e Esmaltados.");
String pingente = in.nextLine();
int valorpingente = 0;
double tamanhopingente = 0;
int valor = 0;
double tamanhofinal = 0;
if(pingente.toLowerCase().equals("bichinhos")){
pingente = "bichinhos";
valorpingente = 225;
tamanhopingente = 1.3;
System.out.println("Quantos?");
int qntsbichinhos = in.nextInt();
double tamanhobichinhos = qntsbichinhos*tamanhopingente;
if(tamanhobichinhos > tamanhopulseira){
System.out.println("Você escolheu um número de bichinhos que excede o tamanho da pulseira!");
} else {
int valorbichinhos = qntsbichinhos*valorpingente;
valor = valorpulseira+valorbichinhos;
tamanhofinal = qntsbichinhos*tamanhopingente+tamanhopulseira;
System.out.println("\nTotal:" +valor);
}
}
double tamanhofinal2 = 0;
if(pingente.toLowerCase().equals("bonequinhos")){
pingente = "bonequinhos";
valorpingente = 185;
tamanhopingente = 1.5;
System.out.println("Quantos?");
int qntsbonequinhos = in.nextInt();
double tamanhobonequinhos = qntsbonequinhos*tamanhopingente;
if(tamanhobonequinhos > tamanhopulseira){
System.out.println("Você escolheu um número de bonequinhos que excede o tamanho da pulseira!");
} else {
int valorbonequinhos = qntsbonequinhos*valorpingente;
valor = valorpulseira+valorbonequinhos;
tamanhofinal2 = qntsbonequinhos*tamanhopingente+tamanhopulseira;
System.out.println("\nTotal:" +valor);
}
}
double tamanhofinal3 = 0;
if(pingente.toLowerCase().equals("berloques")){
pingente = "berloques";
valorpingente = 200;
tamanhopingente = 0.5;
System.out.println("Quantos?");
int qntsberloques = in.nextInt();
double tamanhoberloques = qntsberloques*tamanhopingente;
if(tamanhoberloques > tamanhopulseira){
System.out.println("Você escolheu um número de berloques que excede o tamanho da pulseira!");
} else {
int valorberloques = qntsberloques*valorpingente;
valor = valorpulseira+valorberloques;
tamanhofinal3 = qntsberloques*tamanhopingente+tamanhopulseira;
System.out.println("\nTotal:" +valor);
}
}
double tamanhofinal4 = 0;
if(pingente.toLowerCase().equals("cubinhos")){
pingente = "cubinhos";
valorpingente = 190;
tamanhopingente = 1;
System.out.println("Quantos?");
int qntscubinhos = in.nextInt();
double tamanhocubinhos = qntscubinhos*tamanhopingente;
if(tamanhocubinhos > tamanhopulseira){
System.out.println("Você escolheu um número de cubinhos que excede o tamanho da pulseira!");
} else {
int valorcubinhos = qntscubinhos*valorpingente;
valor = valorpulseira+valorcubinhos;
tamanhofinal4 = qntscubinhos*tamanhopingente+tamanhopulseira;
System.out.println("\nTotal:" +valor);
}
}
double tamanhofinal5 = 0;
if(pingente.toLowerCase().equals("rolinho")){
pingente = "rolinho";
tamanhopingente = 2;
System.out.println("Escolha o material:\nPrata - R$150,00\nPedras - R$220,00\nEsmaltados - R$198,00");
String rolinho = in.nextLine();
if(rolinho.toLowerCase().equals("prata")){
valorpingente = 150;
}if(rolinho.toLowerCase().equals("pedras")){
valorpingente = 220;
}if(rolinho.toLowerCase().equals("esmaltados")){
valorpingente = 198;
}
System.out.println("Quantos?");
int qntsrolinhos = in.nextInt();
double tamanhorolinhos = qntsrolinhos*tamanhopingente;
if(tamanhorolinhos > tamanhopulseira){
System.out.println("Você escolheu um número de rolinhos que excede o tamanho da pulseira!");
} else {
int valorcubinhos = qntsrolinhos*valorpingente;
valor = valorpulseira+valorcubinhos;
tamanhofinal4 = qntsrolinhos*tamanhopingente+tamanhopulseira;
System.out.println("\nTotal:" +valor);
}
}
System.out.println("Você deseja adquirir um suporte de segurança pelo preço de R$95,00? (Sim ou Não)");
String suporte = in.nextLine();
int valorsuporte = 0;
if(suporte.toLowerCase().equals("sim")){
valorsuporte = 95;
valor = valorpulseira+valorpingente+valorsuporte;
System.out.println("Total: "+valor);
} else {
valor = valorpulseira+valorpingente+valorsuporte;
System.out.println("Total: "+valor);
}
}
}
I gave a identada in the code to make it easier to read, if I’ve harmed anything without wanting to please reverse the edit, ok?
– mgibsonbr
Okay, anything I revert xD
– GGirotto
Start by breaking your code into smaller methods.
escolherPulseira
,escolherPingente
,checaTamanho
,escolherMaterial
,adquirirSuporte
, etc. If you have ever taken object orientation classes it is interesting to create model classes as well (Pulseira
,Pingente
, etc.).– Anthony Accioly
In possession of methods like this you can do something like
do {montaPulseira(pulseira, bichinhos);} while (pulseira.getTamanho() > tamanhoPulseira)
.– Anthony Accioly
Well, then, I haven’t had any lessons about it yet! I could even pick up and learn by seeing examples on Iternet but I have to do the work with what was worked in class. I believe that this teacher is a little clueless to have such a system made only using ifs and Switchs and methods to declare variables... I wanted to do something like
if(TamanhoTotalComPingentes < tamanhoDaPulseiraEscolhida{ refaz o processo de escolha de pingente armazenando o tamanho e o preço do ja escolhido}
– GGirotto
William, what you’re wanting to do is a
goto
(which does not exist in Java). The best Ness option in this case is to break in methods. If you can’t do that (I would, your teacher won’t cash note for doing the code the right way) you can surround the entire piece of assembly with awhile (tamanhoPingentes < tamanhoDaPulseiraEscolhida)
, so it only exits the loop when the user has chosen a valid combination.– Anthony Accioly
ta but using the while it will continue with the data that are already stored??
– GGirotto
Yes if you declare the variable outside the loop scope.
– Anthony Accioly
Perfect then. However I don’t know how to break into methods :/ Have any link to pass me? then in case I have more problems I ask another question..
– GGirotto
What about loops, was that seen in class? No loops and no recursion (either) this repetition you want is impossible (unless you copy and paste your code N times... baita gambiarra...)
– mgibsonbr
exactly would be a hell of a stunt.. I just found out that I can use while, so I think I’ll opt for the option of breaking into methods and using while. To do this, besides breaking the code into methods, just remove the variable definitions from within the IFS and leave it in the correct main function?
– GGirotto
@Guilherme Você não accurate break into methods to use
while
- It would only be a question of organization, but since your code is essentially sequential, I don’t see any big problems in having a single giant function. Let me give you an example in response.– mgibsonbr
@William Ready! I would also add that - if you were to break into methods, you would also have to pass parameters and receive a return value, or maybe create fields (probably static) to share data between the
main
and the other functions. Eventually you will learn all this, but it is not necessary to run over things... Splitting your code into sections already helps a lot, like/* Escolha da pulseira */ código /* Escolha do tipo do pingente */ mais código /* Adquirir suporte */ mais código
– mgibsonbr
Hello @Guilherme, just adding what the noble friends have already said, it would be good for you to take a look at Object Orientation, as support material here I leave the link to the excellent booklet of Caelum: https://www.caelum.com.br/apostila-java-orientacao-objetos/
– eliocapelati