So as a part of java assignment - i am required to make CarRent Project - i am quite new to java - i am using blueJ - i am receiving an error that has confused me a bit - looking for some guidance
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());
}
}
just in case the code is to long to read : int cannot be converted into java lang.String is the error when hovering over getPrice()
public void addCarToSell(){
CarToBuy newCar = new CarToBuy(getPrice(),getYearOfRegistration(),
getMileage(), descriptionTextField.getText());
carList.add(newCar);
}
The code of CarToBuy class cannot be seen but the first parameter of its constructor is probably String. The return type of getPrice() is int. To eliminate the problem either
new CarToBuy(Integer.toString(getPrice()),getYearOfRegistration(),
getMileage(), descriptionTextField.getText());