-1

I am doing a payment code. But its not working. How can I make it work?

Edit: Now I have change my code a bit. It can run now but it still didn't print the output I wanted. It only prints 2 lines out of all the output I wanted. I have no idea what was the problem but I think it was at the main part. Any suggestions?

        import java.util.Scanner;
        public class BusPayment {
        public int number_of_adults;
        public int number_of_children;
        public double adult_price;
        public double children_price;
        public double totalprice;

     public BusPayment(){

        }

    public int getnumber_of_adults(){
        return number_of_adults;
    }

    public int getnumber_of_children(){
        return number_of_children;
    }

    public double getadult_price(){
        return adult_price;
    }

    public double getchildren_price(){
        return children_price;
    }

    public double gettotal_price(){
        return totalprice;
    }

    public void setadult_price(double adult_p){
        adult_price=adult_p; 
    }

    public void setchildren_price(double children_p){
        children_price=children_p;
    }

     public void settotalprice(double total_price){
        totalprice=total_price;
    }

    public BusPayment(double adult_p, double children_p){
        adult_price=adult_p;
        children_price=children_p;
    }

    public void BusPaymentPart(){
       number_of_adults=0;
       number_of_children=0;
       adult_price=15.00;
       children_price=10.00;
       totalprice=0.00;
    } 

    public double calculate_totalprice(){
    number_of_adults= 0;
    number_of_children= 0;
    totalprice= (number_of_adults*adult_price)+ 
   (number_of_children*children_price);
    return totalprice;
        }

        public static void main (String []args){
    BusPayment test = new BusPayment();

    Scanner input=new Scanner(System.in);
System.out.println("Payment");
System.out.println("The price of an adult ticket is RM15.00.");
    System.out.println("The price of a children's ticket is RM10.00.");

    int number_of_adults=input.nextInt();
    System.out.println("The number of adults are: " +number_of_adults);
    number_of_adults=input.nextInt();

    int number_of_children=input.nextInt();
    System.out.println("The number of children are: "+number_of_children);
    number_of_children=input.nextInt();

    double totalpri = test.calculate_totalprice();
    System.out.println("The total price you need to pay: " +totalpri);

}

}

I wanted the output to be like:

The price of an adult ticket is 15.00. (this was printed)

The price of a children's ticket is 10.00. (this was printed)

The number of adults are:

The number of children are:

The total price you need to pay:

Payment Successful!

  • please add more code – xiehongguang Mar 26 '19 at 03:36
  • 1
    How are you calling an instance method from a static method? – shmosel Mar 26 '19 at 03:36
  • The codes I didn't include are the declarations, accessors, mutators and constructors because it was too long. Should I create another method? Because for it to print, it must be in the main method right? – novice_coder Mar 26 '19 at 04:00
  • You can print from anywhere in the code if you use `System.out.*`. – Jason Mar 26 '19 at 05:17
  • You are creating a `Scanner` but not using it to retrieve input from the user. I suspect there is more code that we need to see in order to solve you problem. Please provide a [mcve] that demonstrates your problem. At the moment, it appears that you are trying to use variables before you have calculated / assigned their value. Also, you are calling a non-static method from static scope (you need an instance of an object to call a non-static method on that object). – Jason Mar 26 '19 at 05:21
  • Possible duplicate of [Cannot make a static reference to the non-static method](https://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static-method) – David Mar 26 '19 at 09:26

2 Answers2

1

It looks like you are calculating the values after you are trying to print them:

System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
calculate_totalprice();

Also, you are calling a non-static method calculate_totalprice() from within static scope:

public static void main (String []args){
    ...
    calculate_totalprice(); 
}
Jason
  • 11,744
  • 3
  • 42
  • 46
  • Should I move the whole calculate price() into the main method instead? But it still doesn't work though. Or should I put the totalprice calculation into the main method ? – novice_coder Mar 26 '19 at 03:57
0

I think you might need to call the calculations first.

calculate_totalprice();

System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
Ed Yu
  • 1
  • 2