Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: The cash register program

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post The cash register program

    hey there, I am new to Java programming.
    While solving Cash Register challenge I had an issue with my java code if anyone can help me out or can give suggestion that would help me alot.


    Here is the code:

    import java.io.File;
    import java.io.IOException;
    import java.text.DecimalFormat;
    import java.util.Scanner;

    public class CashRegister{

    public static void main(String[] args) throws IOException{

    Scanner fileScanner = (args.length > 0) ? new Scanner(new File(args[0])) : new Scanner(System.in);

    while(fileScanner.hasNextLine()){
    String line = fileScanner.nextLine();
    if(!line.equalsIgnoreCase("")){
    String elements[] = line.split(";");
    float price = Float.parseFloat(elements[0]);
    float cash = Float.parseFloat(elements[1]);

    double change = cash - price;

    if(change < 0){
    System.out.println("ERROR");
    }else if(change == 0){
    System.out.println("ZERO");
    }else{
    System.out.println(findChange(change));
    }
    }
    }
    fileScanner.close();
    }

    private static String findChange(double change){

    String textChange = "";

    DecimalFormat df = new DecimalFormat("########.##");
    int intChange = (int)(Double.valueOf(df.format(change)) * 100);

    while(intChange >= 0.01){
    if(intChange >= 10000){
    textChange += "ONE HUNDRED,";
    intChange -= 10000;
    } else if(intChange >= 5000){
    textChange += "FIFTY,";
    intChange -= 5000;
    } else if(intChange >= 2000){
    textChange += "TWENTY,";
    intChange -= 2000;
    } else if(intChange >= 1000){
    textChange += "TEN,";
    intChange -= 1000;
    } else if(intChange >= 500){
    textChange += "FIVE,";
    intChange -= 500;
    } else if(intChange >= 200){
    textChange += "TWO,";
    intChange -= 200;
    } else if(intChange >= 100){
    textChange += "ONE,";
    intChange -= 100;
    } else if(intChange >= 50){
    textChange += "HALF DOLLAR,";
    intChange -= 50;
    } else if(intChange >= 25){
    textChange += "QUARTER,";
    intChange -= 25;
    } else if(intChange >= 10){
    textChange += "DIME,";
    intChange -= 10;
    } else if(intChange >= 5){
    textChange += "NICKEL,";
    intChange -= 5;
    } else if(intChange >= 1){
    textChange += "PENNY,";
    intChange -= 1;
    }
    }
    return textChange.substring(0, textChange.length() - 1);
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: The cash register program

    I had an issue
    Please explain. If there is console output, please copy the full text and paste it here with some comments about the issue.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to acess and print cash register from web application
    By ethiouser in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 25th, 2014, 04:16 AM
  2. how to acess and print cash register from web application
    By ethiouser in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 25th, 2014, 04:10 AM
  3. Cash register
    By itzmexei in forum What's Wrong With My Code?
    Replies: 13
    Last Post: July 21st, 2014, 08:18 AM
  4. Cash Register Simulation Help
    By Gont in forum Object Oriented Programming
    Replies: 1
    Last Post: September 22nd, 2012, 06:58 PM
  5. Cash Register Exercise
    By Consus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 11:52 PM

Tags for this Thread