2

having trouble with my output. It prints in the correct format as long as the last digit isn't a zero. In other words, it will output $1.75 but for $1.50 I get $1.5. I know the "%.2f" is supposed to format it but I can't figure out where to put it in my code. Thanks!

/** This Class is designed to work with an application program for a Vending Machine. */ public class VendingMachine { private double money; private double amountDue; private double change; private String selection; /** Constructor for the Class. */ public VendingMachine() { money = 0.00; amountDue = 0.00; change = 0.00; selection = ""; } /** Explicit Constructor for the class that gives variables more specific values. */ public VendingMachine(double money, double change, double amountDue, String selection) { this.money = money; this.amountDue = amountDue; this.change = change; this.selection = selection; } /** Accesssor method for the selection. @return the selection of the user. */ public String getSelection() { return selection; } /** Accessor method to get the amount of money the user puts in the machine. @return the amount of money. */ public double getMoney() { return money; } /** Accessor method for the amount due. @return the amount due. */ public double getAmountDue() { return amountDue; } /** Accessor method for getting the user's change. @return the user's change. */ public double getChange() { return change; } /** Mutator method for the selection. @param the selection of the user. */ public void setSelection(String selection) { this.selection = selection; } /** Mutator method for money. @param the money put into machine. */ public void setMoney(double money) { this.money = money; } /** Mutator method for amount due. @param the selection of the user. */ public double setAmountDue(String selection) { if (selection.equals("A1") || selection.equals("A2") || selection.equals("A3")) { amountDue = 1.25; } else if (selection.equals("B1") || selection.equals("B2") || selection.equals("B3")) { amountDue = 1.00; } else if (selection.equals("C1") || selection.equals("C2") || selection.equals("C3")) { amountDue = 1.50; } return amountDue; } /** Mutator method for the change. @param the money put into machine. */ public void setChange(double money) { change = money - amountDue; this.change = change; } /** Method for converting all variables into a String statement. @return a String of the total transaction. */ public String toString() { return ("Your selection: " + selection + "\nYour amount due: " + amountDue + "\nYour change: " + change); } } import java.util.*; /** This program runs a Vending Machine and interacts with the user. */ public class VendingMachineTester { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input; String[] choices = {"A1~~SNICKERS~~~~~~~~$1.25", "A2~~MILKY WAY~~~~~~~$1.25" , "A3~~TWIX~~~~~~~~~~~~$1.25", "B1~~ORIGINAL CHIPS~~$1.00", "B2~~BBQ CHIPS~~~~~~~$1.00" , "B3~~PRETZELS~~~~~~~~$1.00", "C1~~COKE~~~~~~~~~~~~$1.50", "C2~~SPRITE~~~~~~~~~~$1.50", "C3~~DR. PEPPER~~~~~~$1.50", }; displayItems(choices); VendingMachine user = new VendingMachine(); do { System.out.print("Please make your selection: "); input = in.nextLine(); } while (!(input.equals("A1") || input.equals("A2") || input.equals("A3") || (input.equals("B1") || input.equals("B2") || input.equals("B3") || (input.equals("C1") || input.equals("C2") || input.equals("C3"))))); user.setSelection(input); user.setAmountDue(input); System.out.print("Enter the amount you put in the machine: "); double amount = in.nextDouble(); user.setMoney(amount); user.setChange(amount); System.out.println(user); System.out.print("Thank You for your purchase!"); } /** A method for displaying all of the options in the machine. @param an array of choices @return an array of choices. */ public static void displayItems(String[] choices) { for (int i = 0; i < choices.length; i++) { System.out.print(choices[i]); System.out.println(); } return; } } 
1

1 Answer 1

3

This should do the trick:

public String toString() { return String.format( "Your selection: %s: \nYour amount due: %.2f \nYour change: %.2f", selection, amountDue, change); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.