Here is my problem:
I have two java files:
One named Car.java and the other named CarDealerApp.ja va: In the CarDealerApp program, I read in through user input the make, model, year and price of a car and put them into an array called carShop[]. This part of the program is running fine. Every thing in the CarDealerApp.ja va file runs correctly except for the last loop. I need to read in through user input the make of a car and then search the array carShop[] for the make and subtract $1000 from the price listed for all instances of that particular make within the array. I cant seem to make the code work, the user input part does but the for loop does not! Can anyone help! Here is what I have so far: Thanks in advance!! ALI :P
import java.util.*;
import java.net.*;
import java.lang.*;
public class Car{
public String make;
public String model;
public int year;
public int price;
//Car(); constructor with parameters
public Car(String m, String d, int y, int p) {
make = m;
model = d;
year = y;
price = p;
}//end constructor
// return make of car (make accessor)
public String getMake() {
return make;
}//end method
//return model of car (model accessor)
public String getModel() {
return model;
}//end method
//return year of car (year accessor)
public int getYear() {
return year;
}//end method
//return price of car (price accessor)
public int getPrice() {
return price;
}//end method
//set new price of car (price mutator)
public void setPrice(int iPrice) {
price = iPrice;
}//end method
//print method
public void print() {
System.out.prin tln("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
}//end method
}
import java.util.*;
import java.net.*;
import java.lang.*;
public class CarDealerApp{
public static void main(String[] args) {
//declare array of Car objects. Array name is carShop
final int MAX_SIZE = 100;
Car carShop[] = new Car[MAX_SIZE];
Scanner stdin = new Scanner(System. in);
System.out.prin t("Please input the number of effective cars currently in the shop?: ");
int numberOfCars = stdin.nextInt() ;
for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
System.out.prin tln();
System.out.prin t("What is the make of your car? ");
String m = stdin.next();
System.out.prin t("What is the model of your car? ");
String d = stdin.next();
System.out.prin t("What is the Year of your car? ");
int y = stdin.nextInt() ;
System.out.prin t("What is the Price of your car? $");
int p = stdin.nextInt() ;
Car C = new Car(m, d, y, p);
carShop[i] = C;
}
System.out.prin tln();
System.out.prin tln(" The effective cars currently in the shop are: ");
System.out.prin tln();
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
carShop[i].print();
}
System.out.prin tln("The Price of all effective cars currently in the shop is: ");
int prices[] = new int[numberOfCars];
int dataSum = 0;
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
prices[i]= carShop[i].getPrice();
dataSum += prices[i];
}
System.out.prin tln(" Total Price = $" + dataSum);
System.out.prin tln(" Search the shop for car below a certain price. ");
System.out.prin tln();
System.out.prin t("Please enter the price to search for: $ ");
int searchPrice = stdin.nextInt() ;
System.out.prin tln("All cars in the shop with a price of: $" + searchPrice + " or lower are: ");
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
if (carShop[i].getPrice() <= searchPrice) {
carShop[i].print();
}
}
System.out.prin tln(" Search the shop for car of a certain Make ");
System.out.prin tln(" and reduce their price by $1000. ");
System.out.prin tln();
System.out.prin t("Please enter the make to search for: ");
String searchMake = stdin.next();
System.out.prin tln();
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
if (carShop[i].getMake() == searchMake) {
int iPrice = carShop[i].getPrice() - 1000;
carShop[i].setPrice(iPric e);
carShop[i].print();
}
}
}
}
I have two java files:
One named Car.java and the other named CarDealerApp.ja va: In the CarDealerApp program, I read in through user input the make, model, year and price of a car and put them into an array called carShop[]. This part of the program is running fine. Every thing in the CarDealerApp.ja va file runs correctly except for the last loop. I need to read in through user input the make of a car and then search the array carShop[] for the make and subtract $1000 from the price listed for all instances of that particular make within the array. I cant seem to make the code work, the user input part does but the for loop does not! Can anyone help! Here is what I have so far: Thanks in advance!! ALI :P
import java.util.*;
import java.net.*;
import java.lang.*;
public class Car{
public String make;
public String model;
public int year;
public int price;
//Car(); constructor with parameters
public Car(String m, String d, int y, int p) {
make = m;
model = d;
year = y;
price = p;
}//end constructor
// return make of car (make accessor)
public String getMake() {
return make;
}//end method
//return model of car (model accessor)
public String getModel() {
return model;
}//end method
//return year of car (year accessor)
public int getYear() {
return year;
}//end method
//return price of car (price accessor)
public int getPrice() {
return price;
}//end method
//set new price of car (price mutator)
public void setPrice(int iPrice) {
price = iPrice;
}//end method
//print method
public void print() {
System.out.prin tln("Make = " + getMake() + "," + " Model = " + getModel() + "," + " Year = " + getYear() + "," + " Price = $" + getPrice());
}//end method
}
import java.util.*;
import java.net.*;
import java.lang.*;
public class CarDealerApp{
public static void main(String[] args) {
//declare array of Car objects. Array name is carShop
final int MAX_SIZE = 100;
Car carShop[] = new Car[MAX_SIZE];
Scanner stdin = new Scanner(System. in);
System.out.prin t("Please input the number of effective cars currently in the shop?: ");
int numberOfCars = stdin.nextInt() ;
for(int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
System.out.prin tln();
System.out.prin t("What is the make of your car? ");
String m = stdin.next();
System.out.prin t("What is the model of your car? ");
String d = stdin.next();
System.out.prin t("What is the Year of your car? ");
int y = stdin.nextInt() ;
System.out.prin t("What is the Price of your car? $");
int p = stdin.nextInt() ;
Car C = new Car(m, d, y, p);
carShop[i] = C;
}
System.out.prin tln();
System.out.prin tln(" The effective cars currently in the shop are: ");
System.out.prin tln();
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
carShop[i].print();
}
System.out.prin tln("The Price of all effective cars currently in the shop is: ");
int prices[] = new int[numberOfCars];
int dataSum = 0;
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
prices[i]= carShop[i].getPrice();
dataSum += prices[i];
}
System.out.prin tln(" Total Price = $" + dataSum);
System.out.prin tln(" Search the shop for car below a certain price. ");
System.out.prin tln();
System.out.prin t("Please enter the price to search for: $ ");
int searchPrice = stdin.nextInt() ;
System.out.prin tln("All cars in the shop with a price of: $" + searchPrice + " or lower are: ");
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
if (carShop[i].getPrice() <= searchPrice) {
carShop[i].print();
}
}
System.out.prin tln(" Search the shop for car of a certain Make ");
System.out.prin tln(" and reduce their price by $1000. ");
System.out.prin tln();
System.out.prin t("Please enter the make to search for: ");
String searchMake = stdin.next();
System.out.prin tln();
for (int i = 0; (i < numberOfCars) && (i < MAX_SIZE); ++i) {
if (carShop[i].getMake() == searchMake) {
int iPrice = carShop[i].getPrice() - 1000;
carShop[i].setPrice(iPric e);
carShop[i].print();
}
}
}
}
Comment