首页 > 解决方案 > 使对象变量在另一个类中私有

问题描述

我已经编写了预期输出的代码:

样本输入:

输入乘客姓名:Priya

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

输入年龄:61

输入票号:140

输入票价:500.0

样本输出 1:

票号:143

乘客姓名:普里亚

票价: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 - 人员的访问说明符/修饰符不正确 - 应该是 [private]

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

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

请帮忙

谢谢

标签: java

解决方案


您需要致电calculateTotal更新totalAmount。否则,它将永远是0.0

...
System.out.println("Price of a ticket : "+bt.getTicketPrice());
bt.calculateTotal(); // Add this line
System.out.println("Total Amount : "+bt.getTotalAmount());

推荐阅读