我正在处理我的一个小项目,它没有按预期运行。当我尝试拨打aTicket.getCost()
并将结果值设为0
时,我尝试通过子类public double cost
运行它,我首先尝试将费用保存到aTicket.setCost()
。它应该通过if语句然后覆盖当前默认值0
。
我有另一个班级用于存储客户年龄,姓名和检查他们是否是学生。这在Ticket
类中调用。
这是我的Ticket
课程:
public class Ticket
{
private Customer aCustomer;
private double cost;
public double cost()
{
Ticket aTicket = new Ticket();
if (aCustomer.getAge() >= 18 && aCustomer.getAge() <= 64)
{
aTicket.setCost(10);
if (aCustomer.getStudent() == true && aCustomer.getAge() >= 10 && aCustomer.getAge() <= 25)
{
aTicket.setCost(10 * 0.15);
}
else if (aCustomer.getStudent() == true && aCustomer.getAge() > 25)
{
aTicket.setCost(10 * 0.10);
}
else if (aCustomer.getStudent() == false && aCustomer.getAge() > 64)
{
aTicket.setCost(10 * 0.07);
}
}
else if (aCustomer.getAge() < 18)
{
aTicket.setCost(7);
if (aCustomer.getStudent() == true && aCustomer.getAge() >= 10 && aCustomer.getAge() <= 25)
{
aTicket.setCost(7 * 0.15);
}
else if (aCustomer.getStudent() == true && aCustomer.getAge() > 25)
{
aTicket.setCost(7 * 0.10);
}
else if (aCustomer.getStudent() == false && aCustomer.getAge() > 64)
{
aTicket.setCost(7 * 0.07);
}
}
return aTicket.getCost();
}
public Ticket(double cost)
{
this.setCost(cost);
}
public Ticket()
{
this.setCost(0);
}
public void setCost(double cost)
{
this.cost = cost;
}
public double getCost()
{
return cost;
}
public String toString()
{
return "TOTAL COST: $" + getCost();
}
}
这是我的子类调用Ticket类:
public static Ticket IssueTicket()
{
Ticket aTicket = new Ticket();
System.out.println(aTicket.getCost());
return aTicket;
}
控制台询问他们的姓名,年龄,并检查他们是否是学生。但是,在调用0
时,它始终打印出aTicket.getCost()
。
感谢您的帮助
答案 0 :(得分:1)
也许这是因为aCustomer
是null
,你没有在Ticket()
构造函数中设置acustomer。当调用aTicket.getCost()时,它总是打印出0,因为
public Ticket() {
this.setCost(0);
}
您应该在构造函数中设置Customer,以便if else可以确定其年龄
为门票和客户编辑我的代码
门票类:
public class Ticket {
private Customer aCustomer;
private double cost;
public Ticket(double cost) {
this.setCost(cost);
}
public Ticket() {
this.setCost(0);
}
public double cost() {
Ticket aTicket = new Ticket();
if (aCustomer.getAge() >= 18 && aCustomer.getAge() <= 64) {
aTicket.setCost(10);
if (aCustomer.getStudent() && aCustomer.getAge() >= 10 && aCustomer.getAge() <= 25) {
aTicket.setCost(10 * 0.15);
}
else if (aCustomer.getStudent() && aCustomer.getAge() > 25) {
aTicket.setCost(10 * 0.10);
}
else if (!aCustomer.getStudent() && aCustomer.getAge() > 64) {
aTicket.setCost(10 * 0.07);
}
}
else if (aCustomer.getAge() < 18) {
aTicket.setCost(7);
if (aCustomer.getStudent() && aCustomer.getAge() >= 10 && aCustomer.getAge() <= 25) {
aTicket.setCost(7 * 0.15);
}
else if (aCustomer.getStudent() && aCustomer.getAge() > 25) {
aTicket.setCost(7 * 0.10);
}
else if (!aCustomer.getStudent() && aCustomer.getAge() > 64) {
aTicket.setCost(7 * 0.07);
}
}
return aTicket.getCost();
}
public void setCost(double cost) {
this.cost = cost;
}
public double getCost() {
return cost;
}
public String toString() {
return "TOTAL COST: $" + getCost();
}
}
客户类:
public class Customer {
private String name;
private int age;
private boolean student;
public Customer(String name, int age, boolean student) {
this.setName(name);
this.setAge(age);
this.setStudent(student);
}
public Customer() {
this.setName("Unknown");
this.setAge(0);
this.setStudent(false);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setStudent(boolean student) {
this.student = student;
}
public boolean getStudent() {
return student;
}
public String toString() {
return "CUSTOMER DETAILS: " + getName() + " age: " + getAge() + " Student? ";
}
}
这是我主要的客户类:
private static Customer customerDetailsInput() {
Scanner input = new Scanner(System.in);
Customer aCustomer = new Customer();
System.out.println("Welcome to the Cinema Ticket Purchasing System\n");
System.out.println("Please enter your name:");
aCustomer.setName(input.nextLine());
System.out.println("Please enter your age:");
aCustomer.setAge(input.nextInt());
String student;
System.out.println("Are you a student (Y/N):");
student = input.next();
if (student.equals("Y") || student.equals("y")) {
aCustomer.setStudent(true);
}
else if (student.equals("N") || student.equals("n")) {
aCustomer.setStudent(false);
}
return aCustomer;
}
关于你的编辑,首先你不必声明
Ticket aTicket = new Ticket();
在你自己的班上。
其次,正如我所提到的,您需要一个以客户为参数的故障单构造函数来初始化故障单中的客户。
您不必在票证中使用设置成本,只需使用相等的成本来设置代码。
这对我有用:
public static Ticket issueTicket() {
Ticket t = new Ticket(customerDetailsInput());
System.out.println("cost: " + t.toString());
return null;
}
这个工作