创建一个java方法查找费用CD类

时间:2013-11-07 01:28:58

标签: java

我已经创建了一个允许从一个集合中借用CD的java程序,但是我现在需要创建一个方法查找费用(),它为cds借用1-20次收取10p费用,20s收取cds借来的费用21 -30次,购买cds的费用的5%已经超过30次,我的尝试代码到目前为止:

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); } } 

(上面的代码来自底线)

我的代码:

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); } } 
}

任何答案或回复和帮助将非常感激,因为我真的很困惑,无法弄清楚这一点。

2 个答案:

答案 0 :(得分: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,

答案 1 :(得分:1)

(timesborrowed >= 1-19)  This is actually saying if timesborrowed is >= -18
(timesborrowed >= 1-29)  This is actually saying if timesborrowed is >= -28

改为

 if (timesborrowed < 20)
 eiseif (timesborrowed < 30)  
 else