Originally Posted by
aussiemcgr
There are a few things wrong with your code:
1) You do not use quotes unless your are creating a String (like raizalnapi said). Although you are, in theory, attempting to create a String, you are just creating the String Object in your parameters. So, instead of this:
You want:
2) You want to return the type declared in the method (like raizalnapi said). Since you declared your method to return a double with the following code:
you must return a double. I do not know if
priceChild and/or
priceAdult are doubles or not, you did not specify in your question.
3) You should never compare String with the double equals (==). Really, the only thing you want to compare with the double equals is number types. Instead, for Strings and other Objects, you want to use the .equals() method. For Strings, there are two useful .equals() methods. The first is the
.equals(String) method that returns a boolean if the two Strings have the same value (case sensitive). The second is the
.equalsIgnoreCase(String) method that returns a boolean if the two String have the same value (not case sensitive).
So instead of:
else if (showPrice == "Adult")
You would want to say:
else if (showPrice.equals("Adult"))
These are the things wrong with your code based on what you have provided. There are clearly large chunks of your code that you havent provided, and without providing us with those, we really cant help you out too much.
I have created two classes, TicketMachine and User. The TicketMachine is supposed to print a ticket with adult tickets and child tickets, amount of money inserted (method), total income, balance etc. The User class is a user-friendly class with options to buy tickets and adjust ticketprices. Her is the java source code for the two classes:
import java.lang.*;
import java.util.*;
/**
*
*/
public class TicketMachine
{
private double priceAdult;
private double priceChild;
private double balance;
private double totalIncome;
public TicketMachine()
{
priceChild = 0;
priceAdult = 0;
balance = 0;
totalIncome = 0;
}
public double getOrderPrice(String Child, String Adult)
{
if (getOrderPrice.equalsIgnoreCase("Child"))
{
return priceChild;
}
else if (getOrderPrice.equalsIgnoreCase("Adult"))
{
return priceAdult;
}
}
/*public double showPriceChild()
{
return priceChild;
}
public double showPriceAdult()
{
return priceAdult;
}*/
public double showBalance()
{
return balance;
}
public void insertMoney(double amount)
{
if(amount > 0)
{
balance = balance + amount;
}
else
{
System.out.println("Du kan berre sette inn eit positivt beløp: " + amount);
}
}
public void adjustPrice(double child, double adult)
{
this.priceChild = child;
this.priceAdult = adult;
}
public void buyTicket(int noChild, int noAdult)
{
double orderPrice = (noChild * priceChild) + (noAdult * priceAdult);
double refund = balance - orderPrice;
}
public void showTickets()
{
if(balance >= orderPrice)
{
System.out.println("##################");
System.out.println("# Billettautomat");
//System.out.println("# Antall barn: " + noChild + "Antall voksne: " + noAdult);
System.out.println("# Total pris på billettene er: " + orderPrice + "kroner");
System.out.println("# Du har betalt: " + balance + "kroner");
System.out.println("# Pengar tilbake: " + (balance - orderPrice) + "kroner");
System.out.println("# Ha en fortsatt god dag og velkommen tilbake! ");
System.out.println("##################");
System.out.println();
totalIncome = totalIncome + orderPrice;
balance = balance - orderPrice;
}
else
{
System.out.println("Du må sette inn: " + (orderPrice - balance)
+ " fleire kroner!");
}
}
public double refundBalance()
{
double amountToRefund;
amountToRefund = balance;
balance = 0;
return amountToRefund;
}
}
import javax.swing.JOptionPane;
import java.lang.*;
import java.util.*;
public class User
{
private TicketMachine ticketMachine;
public User()
{
ticketMachine = new TicketMachine();
}
public void adjustPrices()
{
String s = JOptionPane.showInputDialog("Skriv inn ny pris i kroner");
int price = Integer.parseInt(s);
}
public void buyTicket()
{
String s1 = JOptionPane.showInputDialog("Antall barn: ");
String s2 = JOptionPane.showInputDialog("Antall voksne: ");
int child = Integer.parseInt(s1);
int adult = Integer.parseInt(s2);
double price = ticketMachine.getOrderPrice(child , adult);
string s3 = JOptionPane.showInputDialog("Prisen for " + child + "barn og " + adult + " voksne er: " + price + "kroner" +
"legg på beløp i kroner");
double amountInserted = Double.parseDouble(s3);
while (amountInserted < price)
{
String s4 = JOptionPane.showInputDialog("Innbetalt beløp er for lite, mangler " + (price - amountInserted) + " kroner" +
"legg på beløp som mangler");
amountInserted = amountInserted + Double.parseDouble(s4);
}
double refund = ticketMachine.buyTickets(child, adult, amountInserted);
JOptionPane.showMessageDialog(null,"Kvittering for \n + child + barn \n" +
price + "kroner ");
}
}