how to implement the string.format method to change the format of this program?

69 views Asked by At

I am trying to figure out how to use the string.format feature into the return statement. Each line has to be a system out? so that means ill have 3 seperate system out?

/*
0 -  4 miles  = $2
         5 - 15 miles  = $5
        16 - 25 miles  = $10
        26 - 50 miles  = $15
    More than 50 miles = $20

WRITE A PROGRAM THAT CALCULATES TOTAL COST OF USER'S ITEMS 
         YOUR OUTPUT SHOULD BE FORMATTED AS SEEN BELOW!!
         
         
                
        
            Product      Qty      Price       Miles      Total
            ----         ---      -----      -----      -----
            Rice          20        5.0       10.0      105.0
                           
                          Thank you. Come Again!!!
 

*/

Above is the format that is wanted, I was able to create a method that calculates and returns the correct values, i just need to format the string variable receipt.

     public class Test {
        public static void main(String[] args) {
            methods Rice = new methods();
            methods Beans = new methods();
            System.out.println(Rice.getTotal("Rice",20,5.0,10));
            System.out.println("\n"+ Beans.getTotal("Cake",200,5.75,102.78));
        }
    }

public class methods {

    int qty;
    double price;
    double miles;

    double total;
    int i;
    String product;

    String receipt;
    public String getTotal(String product, int qty, double price, double miles){
        //       0 -  4 miles  = $2
        //       5 - 15 miles  = $5
        //      16 - 25 miles  = $10
        //      26 - 50 miles  = $15
        //  More than 50 miles = $20
        this.qty = qty;
        this.price=price;
        this.miles = miles;
        this.total = 0;
        this.product = product;


        for(i = 0; i < miles; i++){
            if(i < 5){
                total = qty * price + 2;
            }
             else if(i > 4 && i < 16){
                total = qty * price + 5;
            }
            else if(i > 15 && i < 26){
                total = qty * price + 10;
            }
            else if(i > 25 && i < 51){
                total = qty * price + 15;
            }
            else if(i > 50 ){
                total = qty * price + 20;
            }
        }
        //return 



    }
}

I commented out the return statement where the code should go. I can use print method or string.format method.

0

There are 0 answers