我已经看过这里,花了一些时间在这上面,现在我已经打了一堵砖墙。 我正在使用Java中的停车票模拟器。 我不是很擅长Java,但似乎有大部分工作。 唯一的问题是我输入了一个演示/测试文件,它总是给我相同的答案。即使有荒谬的价值观。 有人能指出我如何解决这个问题的正确方向吗?
谢谢,所有代码如下:
/**
* @(#)ParkedCar.java
*
* ParkedCar application
*
*
* @version 3.00 2014/2/9
*/
public class ParkedCar
{
//Define Variables
private String CarMake;
private String CarModel;
private String CarColour;
private String CarLicensePlate;
private static int NumberOfMinutesParked;
//Define Constructors
// NO ARGUMENT CONSTRUCTOR
// Set Vaules to Zero or null
public ParkedCar()
{
CarMake = " ";
CarModel = " ";
CarColour = " ";
CarLicensePlate = " ";
NumberOfMinutesParked = 0;
}
// CONSTRUCTOR WHICH ACCEPTS AN INPUT
public ParkedCar(String Make,String Model,String Colour,String Reg, int NoMinsPkd)
{
CarMake = Make;
CarModel = Model;
CarColour = Colour;
CarLicensePlate = Reg;
NumberOfMinutesParked = NoMinsPkd;
}
// Use the SET Method
// Set the variables - no needed
public void setMake(String Make)
{
CarMake = Make;
}
public void setModel(String Model)
{
CarModel = Model;
}
public void setColour(String Colour)
{
CarColour = Colour;
}
public void setReg(String Reg)
{
CarLicensePlate = Reg;
}
public void setNoMinsPkd(int NoMinsPkd)
{
NumberOfMinutesParked = NoMinsPkd;
}
// USE THE GET METHODS
// Get the Variables - used to read in values
public String getMake()
{
return CarMake;
}
public String getModel()
{
return CarModel;
}
public String getColour()
{
return CarColour;
}
public String getReg()
{
return CarLicensePlate;
}
public static int getNoMinsPkd()
{
return NumberOfMinutesParked;
}
// USE THE TO STRING METHODS
// Output to a Sting
public String toString()
{
String PkdCar = "Make: " + CarMake
+ "\nModel: " + CarModel
+ "\nColor: " + CarColour
+ "\nLicense Plate: " + CarLicensePlate;
return PkdCar;
}
}
然后
/**
* @(#)ParkingMeter.java
*
* ParkedCar application
*
*
* @version 4.00 2014/2/9
*/
public class ParkingMeter
{
//Define Variables
private static int MinsPurchsed;
//Define Constructors
// NO ARGUMENT CONSTRUCTOR
// Set Vaules to Zero or null
public ParkingMeter()
{
// MinsPurchsed = 0;
}
// CONSTRUCTOR WHICH ACCEPTS AN INPUT
public ParkingMeter(int Purchased)
{
MinsPurchsed = Purchased;
}
// Use the SET Method
// Set the variables - not needed
public void setPurchased(int Purchased)
{
MinsPurchsed = Purchased;
}
// USE THE GET METHODS
// Get the Variables
public static int getPurchased()
{
return MinsPurchsed;
}
// USE THE TO STRING METHODS
// Output to a Sting
public String toString()
{
String MeterString = "Minutes Purchased: " + MinsPurchsed;
return MeterString;
}
}
然后
/**
* @(#)GardaOfficer.java
*
* ParkedCar application
*
*
* @version 3.00 2014/2/9
* @version 4.50 2014/4/13
*/
public class GardaOfficer //extends ParkedCar
{
// Define all the variables
//==========================
private String Name;
private String BadgeNumber;
private double Ticket;
// Constructor to accept all the variables
//========================================
public GardaOfficer(String n, String num)
{
Name = n;
BadgeNumber = num;
}
// NO ARGUMENT CONSTRUCTOR
//========================
public GardaOfficer()
{
Name = "";
BadgeNumber = "";
}
// SET METHODS
//===============
public void setName(String n)
{
Name = n;
}
public void setBadgeNumber(String num)
{
BadgeNumber = num;
}
// GET METHODS
//===============
public String getName()
{
return Name;
}
public String getBadgeNumber()
{
return BadgeNumber;
}
// TO STRING METHOD
//=================
public String toString()
{
String GardaString = "Garda : " + this.Name
+ "\nBadge: " + BadgeNumber
+ "\nTicket: " + Ticket;
return GardaString;
}
public ParkingTicket search(ParkedCar car, ParkingMeter meter)
{
GardaOfficer Garda = new GardaOfficer(this.Name,this.BadgeNumber);
int time = ParkedCar.getNoMinsPkd() - ParkingMeter.getPurchased();
if(ParkedCar.getNoMinsPkd() > ParkingMeter.getPurchased())
{
if(time <= 60)
{
Ticket = 50;
}
else
{
Ticket = 50 + (10 * (time/60));
}
}
if(time <0)
return null;
return new ParkingTicket(car, Garda, getTicket(), time);
}
public double getTicket()
{
return Ticket;
}
public void setTicket(double ticket)
{
this.Ticket = Ticket;
}
}
然后
/**
* @(#)ParkingTicket.java
*
* ParkedCar application
*
*
* @version 4.00 2014/2/9
*/
public class ParkingTicket
{
//Define Variables
private ParkedCar Vehicle;
private GardaOfficer GuardString;
private double ParkingFine;
private int Minutes;
private double firstFine = 50;
private double moreFine = 50;
public ParkingTicket()
{
}
// CONSTRUCTOR WHICH ACCEPTS AN INPUT
public ParkingTicket(ParkedCar car, GardaOfficer Guard, double guyFine, int mins)
{
Vehicle = car;
GuardString = Guard;
ParkingFine = guyFine;
Minutes = mins;
}
// Use the SET Method
// Set the variables - not needed
// USE THE GET METHODS
// Get the Variables
public void getTotalFine()
{
int time = ParkedCar.getNoMinsPkd() - ParkingMeter.getPurchased();
if (time <= 60)
{
ParkingFine = firstFine;
}
else
{
ParkingFine = firstFine + moreFine * (time / 60);
}
}
public double getFirstFine()
{
return firstFine;
}
public double getMoreFine()
{
return moreFine;
}
public ParkedCar getVehicle()
{
return Vehicle;
}
public GardaOfficer getGuardString()
{
return GuardString;
}
public int getMinutes()
{
return Minutes;
}
public double getFine()
{
return ParkingFine;
}
// USE THE TO STRING METHODS
// Output to a Sting
public String toString()
{
String TicketString = "Fine : " + this.ParkingFine
+ "\nMinutes: " + Minutes
+ "\n" + Vehicle.toString()
+ "\n" + this.getGuardString().toString();
return TicketString;
}
}
最后
//This is a demo file to show the program
public class DemoCar
{
public static void main(String[] args)
{
ParkedCar Test1 = new ParkedCar("BMW", "2014", "Yellow", "141D12345", 30);
ParkingMeter parking = new ParkingMeter(50);
GardaOfficer Murphy = new GardaOfficer("Guard Murphy", "10");
ParkingTicket ticket = Murphy.search(Test1, parking);
if (ticket != null)
{
System.out.println(ticket.toString());
}
else
{
System.out.println("No ticket issued!");
}
// A second car checked to see if it passes or not, it's over
ParkedCar Test2 = new ParkedCar("VW", "2001", "Green", "01D321", 225);
ParkingMeter parking2 = new ParkingMeter(200);
ParkingTicket ticket2 = Murphy.search(Test2, parking2);
if (ticket != null)
{
System.out.println(ticket.toString());
}
else
{
System.out.println("No ticket issued!");
}
}
}
答案 0 :(得分:3)
你第二次打印机票而不是票2。
此外,您应始终将变量设为小写。