Pink Poogle Toy Forum

The official community of Pink Poogle Toy
Main Site
NeoDex
It is currently Mon Nov 25, 2024 10:20 am

All times are UTC




Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: JAVA
PostPosted: Sat Sep 17, 2005 7:45 pm 
Honorary Member
Honorary Member
User avatar

Posts: 1023
Joined: Mon May 31, 2004 4:43 am
Gender: Female
I need help. I'm just starting to learn this stuff in class and apparently it's too confusing for me. Okay were were supposed to make a program that would calculate tips for the servers at a restaurant based on whatever the bill was etc. The code that I worked out was the simplest I could think of. What I want to fix and I don't know how is:

1. how to let the user enter % with the number when entering percentage

2. how to idiot proof the code so that if they enter an invalid statment they are prompted with having to enter it again. I'm guessing this is with an "if" function but I can't get it to work :(.

Help moi please! I'm not a programer and I don't think I'll ever master this. I just don't understand how Hunter and all those others who are programing able can remember everything for like fifty different languages o_o...guess it's like learning French or German to them, with many more rules and grammatical errors lol :P

Code:
        float subtotal;
        float tip;
        float total;
        float percent;
        String input;
       
       
        //enter bill subtotal
           input = JOptionPane.showInputDialog (null, "Enter amount of bill",
                "Amount of Bill Before Tip", JOptionPane.QUESTION_MESSAGE);
           
        //convert the total
       
           subtotal = Float.parseFloat(input);
       
        //enter desired percentage  ie 15% or 20% to give server
         
           input = JOptionPane.showInputDialog(null, "Enter the percentage you desire to tip the server \n (15% for normal service 25% for extraordinary)",
                "Enter Percentage (exclude % sign)", JOptionPane.QUESTION_MESSAGE);
       
        //convert the percentage
       
           percent = Float.parseFloat (input);
               
        //calculate tip
        percent = (percent / 100);
        tip = percent * subtotal;
        total = tip + subtotal;
       
        //display a final results broken down
       
        System.out.println("your subtotal\t= " +subtotal);
        System.out.println("your percent\t= " +percent);
        System.out.println("your tip\t= " +tip);
        System.out.println("your total bill\t= " +total);

        String output = "Your total " + total + " consists of \n" +
        subtotal + "subtotal\n" +
        tip + "tip\n" ;
       
        JOptionPane.showMessageDialog(null, output,
                "Breakdown", JOptionPane.INFORMATION_MESSAGE);


that's pretty much what I had. That's the simple way I could think of because the other ways I wanted to try and make it work wouldn't work because I kept getting syntax erros. I know pretty much the idea of what I need for the other things but I can't get it to execute it for me because I'm not coding it correctly :-/.


Oh and also, how do I limit the decimal places that float gives me? Like limit it to two decimal places like you normally have for money :P


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 10:31 pm 
Honorary Member
Honorary Member

Posts: 731
Joined: Tue Jun 01, 2004 3:26 pm
In Java, there's usually more than one way to do even the simplest of problems. I tried thinking of an easier way to do this, but really couldn't think of anything. o_o

Here's what I have (2 different files)...

Code:
public class test21Main{
   public static void main(String [] args) throws NumberFormatException{         
                test21 testObject = new test21();
      testObject.getSubtotal();
      testObject.getPercent();
      testObject.calculateTip();
      testObject.displayResults();         
   }   
}


Code:
import javax.swing.*;
import java.io.*;
import java.text.*;

public class test21{
float subtotal;
        float tip;
        float total;
        float percent;
        String input;
        DecimalFormat df = new DecimalFormat("0.00");
       
        //enter bill subtotal
       
        public void getSubtotal(){
       
                    input = JOptionPane.showInputDialog (null, "Enter amount of bill",
                "Amount of Bill Before Tip", JOptionPane.QUESTION_MESSAGE);
                
             
                         
        //convert the total
       
           try{
              subtotal = Float.parseFloat(input);
             }
             catch(NumberFormatException nfe){
                //insert error message here
                getSubtotal();
             }
        //enter desired percentage  ie 15% or 20% to give server
           }   
           public void getPercent(){
           input = JOptionPane.showInputDialog(null, "Enter the percentage you desire to tip the server \n (15% for normal service 25% for extraordinary)",
                "Enter Percentage (exclude % sign)", JOptionPane.QUESTION_MESSAGE);
             if(input.endsWith("%")){
                        while(input.endsWith("%")){
                input = input.substring(0, input.length()-1);
                      }
                 }
             else{
                //give error message here
                getPercent();
          }
        //convert the percentage
       
           percent = Float.parseFloat (input);
              }
       
        public void calculateTip(){
        //calculate tip
        percent = (percent / 100);
        tip = percent * subtotal;
        total = tip + subtotal;
          }
       
        public void displayResults(){
        //display a final results broken down
          
        System.out.println("your subtotal\t= " +df.format(subtotal));
        System.out.println("your percent\t= " +df.format(percent*100));
        System.out.println("your tip\t= " +df.format(tip));
        System.out.println("your total bill\t= " +df.format(total));

        String output = "Your total " + df.format(total) + " consists of \n" +
        df.format(subtotal) + "subtotal\n" +
        df.format(tip) + "tip\n" ;
       
        JOptionPane.showMessageDialog(null, output,
                "Breakdown", JOptionPane.INFORMATION_MESSAGE);
          }
          
    }


First off, sorry for it being so messy. I'm not big on keeping it neat as I go along as you can see.

Feel free to change the names of variables, classes, etc. Just be sure to change this bit of code

Code:
[b]test21 testObject = new test21();[/b]
testObject.getSubtotal();
testObject.getPercent();
testObject.calculateTip();
testObject.displayResults();   


You're creating an object of a class (the second, longer file), so you'll need to update that line with the name of the file.

1) Did you want the user to have the option of either entering a number or a number with a percentage sign (ex: 15 or 15%)? The way it's set up, it requires the user to enter it in the format of a percentage sign (15%).

2)
Code:
try{
              subtotal = Float.parseFloat(input);
             }
             catch(NumberFormatException nfe){
                //insert error message here
                getSubtotal();
             }

This is the only way I thought of doing this. You can use try/catch to catch the specific exception it gives you in the command window (in this case, it's the NumberFormatException). I believe the problem with the if statement in this case is that you'll have to make many if statements to satisfy all of the possible inputs your program can get.

3) DecimalFormat df = new DecimalFormat("0.00");

When applying it to doubles/floats, use df.format(DOUBLE/FLOAT);


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 18, 2005 1:35 am 
Honorary Member
Honorary Member
User avatar

Posts: 1023
Joined: Mon May 31, 2004 4:43 am
Gender: Female
hmm nope, too advanced for me. I get the decimal part but all that if and try and what not, I haven't done. Argh, can't get a simple if and else statement to work :x . I'm not going to be doing too swell in this course :( . I'll take a break and then try again tomorrow, maybe read some of the textbook and I'll figure it out because my notes are hopeless.

Thank you for the help though, learned about the decimal stuff, we hadn't covered that and that helped much.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group