因此,这有点冗长。我正在尝试结束一个票价根据购买日期而变化的程序。我需要Tester.Java从对象中获取信息,并根据票证类型输出适当的价格。我已经在测试器中设置了一组if语句,但是现在对如何完成该程序的工作陷入僵局。我将在下面粘贴我的代码。

测试仪(包含主要方法):

package tester;
import java.util.Scanner;

public class Tester extends Ticket{
/**
 * @param args the command line arguments
 */
public static void main(String[] args){
    Scanner db = new Scanner(System.in);
    Ticket firstTicket = new Ticket();
    System.out.println("The first ticket: "+firstTicket.toString());
    int x = 0;
    while(x!=2){
        if(x==2){
            System.out.println("Goodbye.");
        }
        else{
           System.out.println("What type of ticket are you purchasing?");
           System.out.println("1.Walk Up");
           System.out.println("2.Advance");
           System.out.println("3.Student Advance");
           int t = db.nextInt();
           if(t==1){

           }
           if(t==2){

           }
           if(t==3){

           }

        }
     System.out.println("Do you need another ticket?");
     x= db.nextInt();
    }
  }
}


门票(高级舱):

package tester;

import java.util.Scanner;

public class Ticket {
    public int ticket;
    public double price;
  /**
   * holds default values for ticket number and price
   */
  public Ticket(){
    super();
    this.ticket=1;
    this.price=15.0;
  }

  /**
   * Stores the values for ticket number and the price, based upon ticket type
   * @param ticket
   * @param price
   */
  public Ticket(int ticket, double price){
    this.ticket=ticket;
    this.price=price;
  }

  /**
   * returns the value of price
   * @return price
   */
  public double getPrice(){
    return price;
  }

  @Override
  public String toString(){
    return "Ticket #" + ticket + " Ticket price: $"+ price;
  }
}


门票:

package tester;

/**
 *
 * @author dylan
 */
public class WalkupTicket extends Ticket{

/**
 * holds the price of a walkup ticket 50$
 */
public WalkupTicket(){
    this.price=50;
    ticket++;
  }
}


预售票:

package tester;

import java.util.Scanner;

public class AdvanceTicket extends Ticket {
  /**
   * stores the values of an advance ticket, depending on how many days before
   * the event it is purchased
   */
  public AdvanceTicket(){
    Scanner db = new Scanner(System.in);
    System.out.println("How many days before the event are you purchasing your ticket?");
    int days = db.nextInt();
    // days before is 10 or less days
    if(days >= 10){
      price=30;
      ticket++;
    }
    // days before is more than 10
    else{
        this.price=40;
        ticket++;
    }
  }
}


学生预售票:

package tester;

import java.util.Scanner;

public class StudentAdvanceTicket extends AdvanceTicket{
  /**
   * stores the values of an advance ticket, depending on how many days before
   * the event it is purchased, with student discount.
   */
  public StudentAdvanceTicket(){
   Scanner db = new Scanner(System.in);
    System.out.println("How many days before the event are you purchasing your ticket?");
    int days = db.nextInt();
    System.out.println("Are you a student?");
    System.out.println("1. Yes");
    System.out.println("2. No");
    int stud = db.nextInt();
    // days before is 10 or less days
    if(days >= 10 && stud == 1){
      price=15;
      ticket++;
    }
    // days before is more than 10
    if(days <= 10 && stud == 1){
        this.price=20;
        ticket++;
    }
  }
}


我觉得我犯了一个简单的错误,但是我是OOP的新手,所以对此有点麻烦。

最佳答案

您是否应该为所有购买的机票节省总金额,或者一次只节省一张机票?

对于门票,您无需做任何事情。总共$ 50。

对于Advance和StudentAdvance,您将创建一个该类型的新对象,并且您拥有该对象的方式,构造函数将显示菜单,提前多少天,什么不提前。然后,您可以从中获得总计。

至于代码的结构,这并不理想。对象的构造函数中不应包含所有代码。他们应该具有ShowMenu函数,该函数将向用户显示菜单并读取他们的输入。构造函数在大多数情况下应该为空白。

您也不需要三个不同的票证对象。一个票证对象应该能够自己处理所有这一切。票证对象可以显示菜单并根据用户输入处理不同的价格。如果您需要保存总计或其他票证,则可以在main方法上拥有一个票证对象数组。然后,您可以遍历该数组以显示或汇总票证。

希望这可以帮助。

关于java - OOP门票价格计划,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43811302/

10-09 19:45