首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建一个java方法find fee CD类

创建一个java方法find fee CD类
EN

Stack Overflow用户
提问于 2013-11-07 09:28:58
回答 2查看 541关注 0票数 0

我已经创建了一个允许从集合中借用CD的java程序,但是我现在需要创建一个find fee ()方法,它对借入1-20次的CD收取10p费用,对借入21-30次的CD收取20p费用,对于借入30次以上的CD收取购买成本的5%,到目前为止我尝试过的代码如下:

代码语言:javascript
复制
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); } } 

(以上代码是从底线开始的)

我的代码:

代码语言:javascript
复制
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); } } 
}

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

EN

回答 2

Stack Overflow用户

发布于 2013-11-07 09:33:20

以下内容毫无意义:

代码语言:javascript
复制
 { if (timesborrowed >= 1-19) { cost = '+10p'; } 
    else if (timesborrowed >= 1-29) { cost = '20p'; } 
    else if (timesborrowed >= 30) { cost = '+ 10.00/5= 2.00'; } 

使用:

代码语言:javascript
复制
if(timesborrowed<20) {cost+=0.10;}
else if (timesborrowed <30) {cost +=-.20;}
else {cost +=2;}

我猜你说的p指的是便士或便士,也就是面额的1/100,

票数 2
EN

Stack Overflow用户

发布于 2013-11-07 09:32:06

代码语言:javascript
复制
(timesborrowed >= 1-19)  This is actually saying if timesborrowed is >= -18
(timesborrowed >= 1-29)  This is actually saying if timesborrowed is >= -28

改为这样做

代码语言:javascript
复制
 if (timesborrowed < 20)
 eiseif (timesborrowed < 30)  
 else
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19826314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档