java - 使对象变量在另一个类中私有
问题描述
我已经编写了预期输出的代码:
样本输入:
输入乘客姓名: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)是否正确
请帮忙
谢谢
解决方案
您需要致电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());
推荐阅读
- mysql - 如何为 hibernate 5.4.17 和 MySQL 8.0 提及 MYSQLDialect?
- css - 更改列堆叠顺序布尔玛,使中间列向下跳
- java - 发布 apk 上的 Google Map API
- arrays - 如何在 ReactJS 中创建和使用地图
- python - 如何防止用户在 24 小时 django 后更新帖子
- xlwings - xlwings:如何从 python UDF 将值写入组合框
- google-cloud-platform - Google Monitoring Query Language Metric 时间序列标识符列过多
- keras - Keras ValueError:尺寸必须相等 - 如何将标签相关值传递给自定义损失函数
- python - 关于 Python 中的方法退出需要的建议
- asp.net - 具有两个用户名的 Azure AD B2C 注册用户流