使对象变量在另一个类中私有

问题描述 投票:-1回答:2

我已经将此预期输出的代码编写为:

样本输入:

输入乘客姓名:Priya

输入性别(M或F / m或f):F

输入年龄:61

输入门票编号:140

输入门票价格:500.0

样本输出1:

票号:143

旅客姓名:Priya

门票价格:500.0

总金额:375.0

我必须根据我所担任的职务的年龄和性别来更改总金额。

我的代码:Person.java

public class Person {
    private String name;
    private char gender;
    private int age;
    public void setName(String name ){
        this.name = name;
    }
    public void setGender(char gender){
        this.gender = gender ;
    }
    public void setAge(int age ){
        this.age = age;
    }
    public String getName(){
        return this.name;
    }
    public char getGender(){
        return this.gender;
    }
    public int getAge(){
        return this.age;
    }
}

BusTicket.java

public class BusTicket {
    private int ticketNo;
    private float ticketPrice;
    private float totalAmount;
    Person person = new Person();
    int age = person.getAge();
    char g = person.getGender();
    public void setTicketNo(int ticketNo){
        this.ticketNo = ticketNo;
    }
    public void setTicketPrice(float ticketPrice){
        this.ticketPrice = ticketPrice;
    }
    public void setTotalAmount(float totalAmount){
        this.totalAmount = totalAmount;
    }
    public void calculateTotal()
    {  
        if(age<16)
        {
            totalAmount = ticketPrice/2;
            setTotalAmount(totalAmount);
        }
            else if(age>=60)
        {
            totalAmount = 3*(ticketPrice/4);
            setTotalAmount(totalAmount);
        }
        else if(g == 'f'|| g== 'F')
        {
            totalAmount = 9*(ticketPrice/10);
            setTotalAmount(totalAmount);
        }
        else{
            setTotalAmount(ticketPrice);
        }
    }
    public int getTicketNo(){
        return this.ticketNo;
    }
    public float getTicketPrice(){
        return this.ticketPrice;
    }
    public float getTotalAmount(){
        return this.totalAmount;
    } 
}

TestMain.java

import java.util.Scanner;
public class TestMain {
    public static BusTicket getTicketDetails()
    {
        Scanner sc = new Scanner(System.in);
        BusTicket bt = new BusTicket();
        System.out.println("Enter the ticket no:");
        bt.setTicketNo(sc.nextInt());
        System.out.println("Enter the ticket price:");
        bt.setTicketPrice(sc.nextFloat());
        return bt;
    }
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        Person p = new Person();
        BusTicket bt;
        System.out.println("Enter the passenger name:");
        p.setName(sc.nextLine());
        System.out.println("Enter the gender(M or F/ m or f):");
        p.setGender(sc.next().charAt(0));
        System.out.println("Enter the age:");
        p.setAge(sc.nextInt());
        bt = getTicketDetails();
        System.out.println("Ticket no:"+bt.getTicketNo());
        System.out.println("Passenger Name:"+p.getName());
        System.out.println("Price of a ticket : "+bt.getTicketPrice());
        System.out.println("Total Amount : "+bt.getTotalAmount());

    }

}

但是我的TotalAmount值始终为0.0,没有更新。并且一些测试用例失败,请帮助解决它们:

失败1-人的访问说明符/修饰符不正确-应该是[私人]

失败2-检查方法setPerson的签名(Returntype / Argument / AccessSpecifier / MethodName)是否正确失败3-检查方法getPerson的签名(Returntype / Argument / AccessSpecifier / MethodName)是否正确]

请帮助

谢谢

[我已将此代码写成此预期输出:样本输入:输入乘客姓名:Priya输入性别(男,女/男/女或男):F输入年龄:61输入票号:140 ...

java
2个回答
0
投票

您需要呼叫calculateTotal来更新totalAmount。否则,它将始终为0.0


0
投票

在您的BusTicket类中,将一个新的Person对象分配给Person属性,然后您尝试从该新创建的Person对象中获取年龄和性别的详细信息,但此刻尚未填充Person的年龄和性别。

© www.soinside.com 2019 - 2024. All rights reserved.