首页 > 解决方案 > 为什么这个 java 代码没有产生我期望的结果?

问题描述

age程序需要根据客户的情况开出正确的票价gender

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Person p1 = new Person();

        System.out.println("Enter the passenger name:");
        String name = sc.next();
        p1.setName(name);

        System.out.println("Enter the gender:");
        char gen = sc.next().charAt(0);
        p1.setGender(gen);

        System.out.println("Enter the age:");
        int age = sc.nextInt();
        p1.setAge(age);

        BusTicket b1 = getTicketDetails();
        b1.setPerson(p1);

        b1.calculateTotal();

        System.out.println("Ticket no:"+b1.getTicketno());
        System.out.println("Passenger Name:"+p1.getName());
        System.out.println("Price of a ticket:"+b1.getTicketprice());
        System.out.println("Total Amount:"+b1.getTotalamt());
    }

    public static BusTicket getTicketDetails() {
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter the ticket no:");
        int no=sc.nextInt();

        BusTicket t1=new BusTicket();
        t1.setTicketno(no);

        System.out.println("Enter the ticket price:");`enter code here`
        float cost=sc.nextFloat();
        t1.setTicketprice(cost);

        return t1;
    }
}
public class Person {
    private String name;
    private char gender;
    private int age;

    public void setName(String name){
        this.name=name;
    }

    public String getName() {
        return this.name;
    }

    public void setGender(char gender){
        this.gender = gender;
    }

    public char getGender() {
        return this.gender;
    }

    public void setAge(int age){
        this.age = age;
    }

    public int getAge() {
        return this.age;
    }
}
public class BusTicket {
private int ticketNo;
    private float ticketPrice;
    private float totalAmount;
    private Person person;

    public void setTicketno(int no) {
        ticketNo = no;
    }

    public int getTicketno() {
        return this.ticketNo;
    }

    public void setTicketprice(float price){
        ticketPrice = price;
    }

    public float getTicketprice(){
        return this.ticketPrice;
      }

    public void setTotalamt(float amt) {
        totalAmount=amt;
    }

    public float getTotalamt() {
        return this.totalAmount;
    }

    public void setPerson(Person p) {
        p=new Person();
        person=p;
    }

    public Person getPerson() {
        return this.person;
    }

    public void calculateTotal() {
        if(16 > person.getAge())
            this.totalAmount = ticketPrice - (float) (ticketPrice * 0.50);
        else if(person.getAge() < 60)
            this.totalAmount = ticketPrice - (float) (ticketPrice * 0.25);
        else if (person.getGender() == 'f' || person.getGender() == 'F')
            this.totalAmount = ticketPrice - (float) (ticketPrice * 0.10);
        else
            this.totalAmount = ticketPrice;
    }
}

calculateTotal方法没有给出正确的输出。对于每个票价,输出给出的金额是它的一半。函数中的 if-else 条件calculateAmount未正确执行。

为了给我期望的结果,我需要将其更改为什么?

测试用例

其中 Name 无关紧要,基本票价为$100.00

+-----|--------|-------------------|-----------------------------+
| Age | Gender | Expected Discount | Expected total ticket price |
|-----|--------|-------------------|-----------------------------|
| 15  |   M    |       0.50        |             $50.00          |
| 15  |   F    |       0.50        |             $50.00          |
|  4  |   M    |       0.50        |             $50.00          |
| 12  |   F    |       0.50        |             $50.00          |
|-----|--------|-------------------|-----------------------------|
| 61  |   M    |       0.25        |             $75.00          |
| 61  |   F    |       0.25        |             $75.00          |
| 99  |   M    |       0.25        |             $75.00          |
| 75  |   F    |       0.25        |             $75.00          |
|-----|--------|-------------------|-----------------------------|
| 16  |   F    |       0.10        |             $90.00          |
| 27  |   F    |       0.10        |             $90.00          |
| 48  |   F    |       0.10        |             $90.00          |
| 60  |   F    |       0.10        |             $90.00          |
|-----|--------|-------------------|-----------------------------|
| 16  |   M    |       0.00        |             $100.00         |
| 27  |   M    |       0.00        |             $100.00         |
| 48  |   M    |       0.00        |             $100.00         |
| 60  |   M    |       0.00        |             $100.00         |
+-----|--------|-------------------|-----------------------------+

标签: javaimmutabilitysetterside-effectsmutability

解决方案


问题是您在执行以下操作时正在覆盖人:

public void setPerson(Person p){
      p =new Person();
      person=p;
}

将此设置器替换为:

public void setPerson(Person p){
    this.person=p;
}

推荐阅读