[Help] Java Cash Register

Junior Spellweaver
Joined
Sep 1, 2008
Messages
130
Reaction score
0
Hey guys.
Due to boredem, I thought I'd create a Cash Register in Java.

I have it complied and it was done successfully, but for some reason I can't execute the code.
I keep getting the error
Code:
Exception in thread "main" java.lang.NoSuchMethodError: main
And that's all I get.

And this is the code

Code:
/**
   A cash register totals up sales and computes change due.
*/
public class CashRegister
{
   /**
      Constructs a cash register with no money in it.
   */
   public CashRegister()
   {
      purchase = 0;
      payment = 0;
   }

   /**
      Records the sale of an item.
      @param amount the price of the item
   */
   public void recordPurchase(double amount)
   {
      double total = purchase + amount;
      purchase = total;
   }

   /**
      Enters the payment received from the customer.
      @param amount the amount of the payment
   */
   public void enterPayment(double amount)
   {
      payment = amount;
   }

   /**
      Computes the change due and resets the machine for the next customer.
      @return the change due to the customer
   */
   public double giveChange()
   {   
      double change = payment - purchase;
      purchase = 0;
      payment = 0;
      return change;
   }
   private double purchase;
   private double payment;
}

If it helps, I'm using Mac OS X Lion, and I'm running the code through Terminal.

Thanks!
 
Sounds daft...
How do I add that?
I was given this task by a friend as he's a Java Expert. I just threw it together, I'm useless at Java :P
 
If you don't understand how code is executed, you need to return to whatever material you've been learning from without skipping the basics. Ask yourself this: what is it you're expecting to happen when you run this program?
 
Just wondering, i found this example in hm, which book was it.. "Beginning programming with java".
Did you get the example from there?
 
Okay, I'm going to be super nice here for once and give you the material that's used to teach Java to the highschoolers here. Enjoy.
http://www.zephyrr.com/down/Packets.7z


Also, very basic lesson. I'm assuming you have little to no experience with programming, like, at all, so if I'm wrong, feel free to ignore me. Also, if you haven't done any OOP before, I'd avoid those concepts till you get down the basics (so only use one class to start with, thus avoiding constructors and all that crap)~

Every class you write has this basic format.

Code:
public class [name of your file, eg TestClass] {
  public static void main(String[] args) {
    // When you run your program, this is where it actually starts.  
    // If you don't have this method (main), it's like looking at a map
    // with no labels on it.  The compiler just doesn't know where to go.
    // All of your code should be inside of methods, such as main.
  }
  // If you write your code here, you're not inside of a method, and 
  // the compiler will give you an error.  You can see that you are
  // outside of the method because of the braces { }.  Something that I
  // sometimes find helpful is to actually count the braces.  An opening
  // brace { is positive 1, and a closing brace } is negative 1.  You 
  // can see an example of how I count them below.
}

Code:
public class ExampleBraces { // 1
  public static void main(String[] args) { // 2
    if((1 + 1) == 3) { // 3
      System.out.println("The world is ending!!!!!!!!");  // This is a pretty simple example of how to output messages or information.  The stuff in the quotes is the text that's being printed out.  If you have a variable, you do not need to put it in quotes.
      System.out.println(5); // You also don't need to put numbers in quotes, if there isn't anything else.
      System.out.println("The sum of 4 and 9 is " + (4 + 9) + "."); // This is how you combine calculated values, such as variables or a simple addition job like in this example, with text.  If you're doing math, it needs to be in quotes.  The + after the quote and before the ( is basically saying "Hey, take this String of characters, and ADD me on to the end!"
    } // 2
  System.out.println("Am I still here?");
  } // 1
} // 0

// At the end of your class, you should always be down to 0.  If you're 
// at 1, then you're not in a method so it won't actually work.
// At first this won't really seem so difficult, but it can get really 
// hard to keep track later on when you're nesting 3-4 if statements 
// and loops and such.

There's your quick tutorial on how to actually make a program and run it.



EDIT: Very quick thing on how to input data~

Code:
import javax.swing.JOptionPane; // This is just saying that we need to use the JOptionPane class in the swing package.  Often times your IDE will add this for you automatically (I know Netbeans does).

public class Whatever {
public static void main(String[] args) {
  String exampleOne = JOptionPane.showInputDialog("Enter some text!"); // This will pop up a window where the user can type in some text.  The showInputDialog method int he JOptionPane class takes one parameter, which is a String.  Specifically, this String is the message that will be shown to your user when you ask them to input the data.  Calling this method will "return" a String.  In other words, you can consider the showInputDialog method to be EQUAL to the text you input.  Since that text is treated as a String, you can save it to a String variable, as shown here.
  int number = Integer.parseInt(JOptionPane.showInputDialog("Enter a number!")); // What about here?  You're asking for an integer.  showInputDialog will still return a String, and you can't set an int equal to a String.  Luckily, there are some methods to help.  Integer.parseInt() takes a String as a parameter, and if that String represents a number (eg: 1, 17, -4), then it will basically convert this String into an int.  Very helpful.  However, if they input some text that can't be converted to a number, like "Neener neener numbers suck," then your program will crash.  There are ways to avoid this, specifically regex, but don't worry about that for now.
  boolean exampleBool = Boolean.parseBoolean(JOptionPane.showInputDialog("Enter a boolean!")); // This works just like the previous line, except that instead of numbers, this works with the boolean values, true and false.
  double d1 = Double.parseDouble(JOptionPane.showInputDialog("Enter a double.")); // Same, but with doubles, like 1.5 and -2.3.
  char c1 = JOptionPane.showInputDialog("Enter a character.").charAt(0); // Characters are a bit different.  There is no Character.parseChar(), so this requires some creativity.  We established earlier that JOptionPane.showInputDialog("") can be treated as a String.  If you look at the Java API, you'll see a method in the String class called charAt that takes an int parameter and returns a char (character).  Since we can't just call a parse method, we can use this.  Java starts counting at 0 rather than 1, so the first character in a String will be the 0th character, the second character is at index 1, third at index 2, etcetera.  What this code is basically saying is "Ask for a String, and then give me the character at index 0."  If you have more than one character in your String, it'll just ignore the rest.
  }
}




By the way, the reason I actually took the time to super-spoonfeed and be helpful and such is nothing more than that I am REALLY bored right now, lol. I spend about 45 minutes a day first thing in the morning aiding in a Programming 1 class, so I'm used to spoonfeeding, unfortunately.
 
Back