所以作为java赋值的一部分 - 我需要制作CarRent项目 - 我对java很新 - 我使用的是blueJ - 我收到的错误让我有点困惑 - 寻找一些指导
import java.util.ArrayList;
import java.util.ArrayList;
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.lang.String;
@author Mohsin
public class CarCompany implements ActionListener{
private JFrame frame;
private Container contentPane;
private JLabel descriptionLabel;
private JTextField descriptionTextField;
private JLabel downPayLabel;
private JTextField downPayTextField;
private JLabel dailyRateLabel;
private JTextField dailyRateTextField;
private JLabel priceLabel;
private JTextField priceTextField;
private JLabel yearOfRegLabel;
private JTextField yearOfRegTextField;
private JLabel mileageLabel;
private JTextField mileageTextField;
private JLabel customerNameLabel;
private JTextField customerNameTextField;
private JLabel dateOfHireLabel;
private JTextField dateOfHireTextField;
private JLabel dateOfReturnLabel;
private JTextField dateOfReturnTextField;
private JLabel numberOfDaysLabel;
private JTextField numberOfDaysTextField;
private JLabel carNumberLabel;
private JTextField carNumberTextField;
private JButton addCarToRentButton;
private JButton addCarToBuyButton;
private JButton clearButton;
private JButton displayAllButton;
private JButton rentCarButton;
private JButton sellCarButton;
private ArrayList <CAR>carList;
public static void main(String[] args){
CarCompany company = new CarCompany();
}
public CarCompany(){
carList = new ArrayList<CAR>()
frame = new JFrame("Car Company");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(5,6));
JLabel descriptionLabel = new JLabel("Description"); //(1) the
description
contentPane.add(descriptionLabel);
contentPane.add(descriptionTextField);
JLabel downPayLabel = new JLabel("Down Payment"); //(2) the
down payment
contentPane.add(downPayLabel);
contentPane.add(downPayTextField);
JLabel dailyRateLabel = new JLabel("Daily Rate"); //(3) the
daily rate
contentPane.add(dailyRateLabel);
contentPane.add(dailyRateTextField);
JLabel priceLabel = new JLabel("Price"); //(4) the
price
contentPane.add(priceLabel);
contentPane.add(priceTextField);
JLabel yearOfRegLabel = new JLabel("Year Of Registration"); //(5) the
year of registration
contentPane.add(yearOfRegLabel);
contentPane.add(yearOfRegTextField);
JLabel mileageLabel = new JLabel("mileage"); //(6) the
mileage
contentPane.add(mileageLabel);
contentPane.add(mileageTextField);
JLabel customerNameLabel = new JLabel("Customer Name"); //(7) the
customer name
contentPane.add(customerNameLabel);
contentPane.add(customerNameTextField);
JLabel dateOfHireLabel = new JLabel("Date Of Hire"); //(8) the
date of hire
contentPane.add(dateOfHireLabel);
contentPane.add(dateOfHireTextField);
JLabel dateOfReturnLabel = new JLabel("Date Of Return"); //(9) the
date of return
contentPane.add(dateOfReturnLabel);
contentPane.add(dateOfReturnTextField);
JLabel numberOfDaysLabel = new JLabel("Number Of Days"); //(10) the
number of days
contentPane.add(numberOfDaysLabel);
contentPane.add(numberOfDaysTextField);
JLabel carNumberLabel = new JLabel("Car Number"); //(11) the
car number (its position in an array list of cars)
contentPane.add(carNumberLabel);
contentPane.add(carNumberTextField);
JButton addCarToRentButton = new JButton("Add Car To Rent");
contentPane.add(addCarToRentButton);
JButton addCarToBuyButton = new JButton("Add Car To Buy");
contentPane.add(addCarToBuyButton);
JButton rentCarButton = new JButton("Rent Car");
contentPane.add(rentCarButton);
JButton sellCarButton = new JButton("Sell Car");
contentPane.add(sellCarButton);
JButton clearButton = new JButton("Clear");
contentPane.add(clearButton);
JButton displayAllButton = new JButton("Display All Cars");
contentPane.add(displayAllButton);
frame.pack();
frame.setVisible(true);
clearButton.addActionListener(this);
addCarToRentButton.addActionListener(this);
addCarToBuyButton.addActionListener(this);
displayAllButton.addActionListener(this);
rentCarButton.addActionListener(this);
sellCarButton.addActionListener(this);
}
public void addCarToRent(){
CarToRent newCar = new CarToRent(descriptionTextField.getText(),
getDownPayment(),
getDailyRate());
carList.add(newCar);
}
/* Add Car To Sell
public void addCarToSell(){ //error is here getPrice -
CarToBuy newCar = new CarToBuy(getPrice(),getYearOfRegistration(),
getMileage(), descriptionTextField.getText());
carList.add(newCar);
}
public void rentCar(){
CarToRent thisCar;
thisCar = (CarToRent) carList.get(getCarNumber());
thisCar.rentCar(customerNameTextField.getText(),
dateOfHireTextField.getText(), dateOfReturnTextField.getText(),
getNumberOfDays());
}
/* Return a car
public void returnCar(){
CarToRent thisCar = (CarToRent) carList.get(getCarNumber());
thisCar.returnCar();
}
public void sellCar(){
CarToBuy thisCar = (CarToBuy) carList.get(getCarNumber());
if (carList.size() >= getCarNumber() && getCarNumber()>=0 && thisCar
instanceof CarToBuy){
thisCar = (CarToBuy) carList.get(getCarNumber());
thisCar.sellCar(customerNameTextField.getText());
}
}
public void displayAll(){ // Displays all the information relating to
properties is displayed
for (CAR thisCar: carList)
{
if (thisCar instanceof CarToRent){
CarToRent carToRent =(CarToRent)thisCar;
carToRent.displayDetails();
}
else if (thisCar instanceof CarToBuy){
CarToBuy carToBuy =(CarToBuy)thisCar;
carToBuy.displayDetails();
}
}
}
public void actionPerformed(ActionEvent event){
String command = event.getActionCommand();
if (command.equals("Add Car To Rent")){
addCarToRent();
}
else if (command.equals("Add Car To Buy")){
addCarToSell();
}
else if (command.equals("Clear")){
clear();
}
else if (command.equals("Display All Cars")){
displayAll();
}
else if (command.equals("Rent Car")){
rentCar();
}
else if (command.equals("Sell Car")){
sellCar();
}
}
public void clear(){ // Clears text from all eleven text fields
descriptionTextField.setText("");
downPayTextField.setText("");
dailyRateTextField.setText("");
priceTextField.setText("");
yearOfRegTextField.setText("");
mileageTextField.setText("");
customerNameTextField.setText("");
dateOfHireTextField.setText("");
dateOfReturnTextField.setText("");
numberOfDaysTextField.setText("");
carNumberTextField.setText("");
}
//*****accessors
public int getDailyRate(){
return Integer.parseInt(dailyRateTextField.getText());
}
public int getDownPayment(){
return Integer.parseInt(downPayTextField.getText());
}
public int getNumberOfDays(){
return Integer.parseInt(numberOfDaysTextField.getText());
}
public int getMileage(){
return Integer.parseInt(mileageTextField.getText());
}
public int getYearOfRegistration(){
return Integer.parseInt(yearOfRegTextField.getText());
}
public int getCarNumber(){
return Integer.parseInt(carNumberTextField.getText());
}
public int getPrice(){
return Integer.parseInt(priceTextField.getText());
}
}
以防代码长读:int无法转换为 将鼠标悬停在getPrice()
上时,java lang.String是错误 public void addCarToSell(){
CarToBuy newCar = new CarToBuy(getPrice(),getYearOfRegistration(),
getMileage(), descriptionTextField.getText());
carList.add(newCar);
}
答案 0 :(得分:0)
类CarToBuy
似乎没有(可访问的)构造函数采用四个参数,其中第一个是int
,但是一个string
作为第一个参数。提供匹配参数或相应地更改CarToBuy
类,具体取决于哪一方是错误的。
答案 1 :(得分:0)
无法看到CarToBuy类的代码,但其构造函数的第一个参数可能是String。 getPrice()的返回类型是int。 要消除这个问题
new CarToBuy(Integer.toString(getPrice()),getYearOfRegistration(),
getMileage(), descriptionTextField.getText());