0

I have created a java program that allows CD to be borrowed from a collection, however i now need to create a method find fee (), which applies 10p charge for cds borrowed 1-20 times, 20p charge for cds borrowed 21-30 times, 5% of purchase cost charged for cds borrowed more than 30 times, my attempted code so far :

public void borrow(String personBorrowed)
    {
    person = personBorrowed;
    inStock = false;
    timesBorrowed = timesBorrowed + 1;
    }
    public void main(String[] args) 
    { if (timesborrowed >= 1-19) { cost = '+10p'; } 
    else if (timesborrowed >= 1-29) { cost = '20p'; } 
    else if (timesborrowed >= 30) { cost = '+ 10.00/5= 2.00'; } 
    System.out.println("Cost = " + cost); } } 

(above code is from the bottom line)

my code:

public class CD
{
    // instance variables - replace the example below with your own
    private String title;
    private String artist;
    private int noOfTracks;
    private double cost;
    private boolean inStock;
    private String person;
    private int timesBorrowed;
    private boolean returnCD;

    /**
     * Constructor for objects of class CD
     */
    public CD(String newTitle, String newArtist,int newNoOfTracks,double newCost)
    {
        // initialise instance variables
        title = newTitle;
        artist = newArtist;
        noOfTracks = newNoOfTracks;
        cost = newCost;
        inStock = true;
        person = null;
        timesBorrowed = 0;
    }


     /**
     * Default Constructor for Testing
     */
    public CD()
    {
        // initialise instance variables
        title = "Blue Print";
        artist = "Jay Z";
        noOfTracks = 15;
        cost = 10.00;
        inStock = true;
        person = null;
        timesBorrowed = 0;
    }



    /**
     * An example of a method - replace this comment with your own
     */

    public String getTitle()
    {
    return title;
    }


    /**
     * An example of a method - replace this comment with your own
     */

    public String getArtist()
    {
    return artist;
    }


    /**
     * An example of a method - replace this comment with your own
     */

    public int getNoOfTracks()
    {
    return noOfTracks;
    }


    /**
     * An example of a method - replace this comment with your own
     */

    public double getCost()
    {
    return cost;
    }


    /**
     * An example of a method - replace this comment with your own
     */

    public void printDetails()
    {
    System.out.println("Title: " + title);
    System.out.println(" ");
    System.out.println("Artist: " + artist);
    System.out.println(" ");
    System.out.println("Number of Tracks: " + noOfTracks);
    System.out.println(" ");
    System.out.println("Cost: " + cost);
    }


    /**
     * An example of a method - replace this comment with your own
     */ 

    public void borrow(String personBorrowed)
    {
    person = personBorrowed;
    inStock = false;
    timesBorrowed = timesBorrowed + 1;
    }
    public void main(String[] args) 
    { if (timesborrowed >= 1-19) { cost = '+10p'; } 
    else if (timesborrowed >= 1-29) { cost = '20p'; } 
    else if (timesborrowed >= 30) { cost = '+ 10.00/5= 2.00'; } 
    System.out.println("Cost = " + cost); } } 
}

any answers or replies and help would be greatly appreciated as I really confused and cant figure this out.

4

2 回答 2

2

以下是没有意义的:

 { if (timesborrowed >= 1-19) { cost = '+10p'; } 
    else if (timesborrowed >= 1-29) { cost = '20p'; } 
    else if (timesborrowed >= 30) { cost = '+ 10.00/5= 2.00'; } 

利用:

if(timesborrowed<20) {cost+=0.10;}
else if (timesborrowed <30) {cost +=-.20;}
else {cost +=2;}

我假设p你的意思是便士或便士,因此是面额的 1/100,

于 2013-11-07T01:33:20.183 回答
1
(timesborrowed >= 1-19)  This is actually saying if timesborrowed is >= -18
(timesborrowed >= 1-29)  This is actually saying if timesborrowed is >= -28

Do this instead

 if (timesborrowed < 20)
 eiseif (timesborrowed < 30)  
 else
于 2013-11-07T01:32:06.193 回答