Help with removing element from arraylist specific to my program

Discussion in 'Java' started by Dharmin, Jun 21, 2015.

?

How do I delete elements in my program

  1. deleting specific to my program

    0 vote(s)
    0.0%
  2. why sellCar method is not working

    0 vote(s)
    0.0%
  3. Graphical explanation of how arrayList stores references specific to a situation in program

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. Dharmin

    Dharmin

    Joined:
    Jun 20, 2015
    Messages:
    1
    Likes Received:
    0
    Location:
    Sydney
    Hi All,

    I have written carSales program and I have stored values in arraylist by creating each time new object.
    while program is running I would like to set the function which allows to sell car and during program's life cycle it should remove e.g. "1999" "Auto" 10000,50000. At the moment it does not give me anything in return to my sellCar method.
    Please help me in removing the element. Thanks

    ArrayList<BuyHonda> honda;


    public HondaInventory(){

    honda = new ArrayList<BuyHonda>();

    honda.add(new BuyHonda("1999","Auto", 10000, 50000)); //sold yes/no

    honda.add(new BuyHonda("2003","Manual", 50000, 150000));//sold Yes/No

    honda.add(new BuyHonda("2007","Manual", 5000, 25000));//sold Yes/No

    honda.add(new BuyHonda("2013","Auto", 15000, 90000));//sold Yes/No

    honda.add(new BuyHonda("2011","Manual", 20000, 95000));//sold Yes/No

    }
    public boolean sellCar(String year, String transmission, double price, double kms)
    {
    BuyHonda sold = null;
    for(BuyHonda honda1:honda)
    {
    if(honda1.getYear().equalsIgnoreCase(year) && honda1.getTransmission().equalsIgnoreCase(transmission) && honda1.getPrice() == price && honda1.getKms() == kms)
    {
    sold = honda1;
    break;
    }
    }
    if(sold!=null)
    {
    honda.remove(sold);
    return true;
    }
    return false;
    }


    public class BuyHonda {
    private String year,transmission;
    privatedoubleprice, kms;
    public BuyHonda(){
    }
    public BuyHonda(String hYear, String hTransmission, double hPrice, double hKms){
    this.setKms(hKms);
    this.setPrice(hPrice);
    this.setTransmission(hTransmission);
    this.setYear(hYear);
    }
    public String getYear() {
    returnyear;
    }
    public void setYear(String year) {
    this.year = year;
    }
    public String getTransmission() {
    returntransmission;
    }
    public void setTransmission(String transmission) {
    this.transmission = transmission;
    }

    public double getPrice() {

    return price;

    }

    public void setPrice(double price) {

    this.price = price;

    }

    public double getKms() {

    returnkms;

    }

    public void setKms(double kms) {

    this.kms = kms;

    }
    }
    Here is the console based output..
    Wellcome to choose the model of the car:

    1) Honda cars:

    2) Toyota cars:

    3) Lexus cars:

    Choose the model of the car

    1

    Please choose car from below options:

    YearTransmissionPriceKelometers

    1999Auto1000050000

    2003Manual50000150000

    2007Manual500025000

    2013Auto1500090000

    2011Manual2000095000

    Enter the year

    1999

    Enter transmission mode:

    auto

    choose the price criteria

    10000

    Enter kelometers

    50000

    This car is available. Please press b to buy car

    Please press Buy to buy a car

    b

    Car is sold, press any key to continue
     
    Dharmin, Jun 21, 2015
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.