Advertisement
Guest User

Java Error Project

a guest
Nov 19th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.54 KB | None | 0 0
  1. class Vehicle
  2. {
  3. int year, miles;
  4. double purchasePrice, forSalePrice;
  5. double [] priceHistory = new double[5];
  6. String status = "Unknown", make, model, VIN, color;
  7. String [] feature = new String[5];
  8.  
  9. public Vehicle (int yr, String carMake)
  10. {
  11. year = yr;
  12. make = carMake;
  13. model = "N/A";
  14. VIN = "N/A";
  15. color = "N/A";
  16. }
  17.  
  18. //
  19. public Vehicle (int yr, String carMake, String carModel)
  20. {
  21. year = yr;
  22. make = carMake;
  23. model = carModel;
  24. VIN = "N/A";
  25. color = "N/A";
  26. }
  27.  
  28. public Vehicle(int yr, String carMake, String carModel, char status, double forSalePrice)
  29. {
  30. year = yr;
  31. make = carMake;
  32. model = carModel;
  33. setStatus(status);
  34. this.forSalePrice = forSalePrice;
  35. color = "N/A";
  36. VIN = "N/A";
  37. }
  38.  
  39. public void setYear(int vehYear)
  40. {
  41. year = vehYear;
  42. }
  43.  
  44. public void setMiles(int vehMiles)
  45. {
  46. miles = vehMiles;
  47. }
  48.  
  49. public void setPurchasePrice(double price)
  50. {
  51. purchasePrice = price;
  52. }
  53.  
  54. public void setForSalePrice(double price)
  55. {
  56. updatePriceHistory(price);
  57. forSalePrice = price;
  58. }
  59.  
  60. public void setMake(String make)
  61. {
  62. this.make = make;
  63. }
  64.  
  65. public void setModel(String model)
  66. {
  67. this.model = model;
  68. }
  69.  
  70. public void setVIN(String VIN)
  71. {
  72. this.VIN = VIN;
  73. }
  74.  
  75. public void setColor(String color)
  76. {
  77. this.color = color;
  78. }
  79.  
  80. public void setFeature(String newFeature)
  81. {
  82. int i = 0;
  83. for(i = 0; i < feature.length; i++)
  84. {
  85. if(feature[i] == null)
  86. {
  87. feature[i] = newFeature;
  88. break;
  89. }
  90. }
  91. if(i == feature.length)
  92. System.out.println("Feature not added, please remove a feature to add new ones.");
  93. }
  94.  
  95. public void removeFeature(int a)
  96. {
  97. feature[a] = null;
  98. while(a < feature.length - 1)
  99. {
  100. String temp = feature[a];
  101. feature[a] = feature[a + 1]; //sorting algorithm
  102. feature[a + 1] = temp;
  103. a++;
  104. }
  105. }
  106.  
  107. public String getFeature(int feat)
  108. {
  109. if(feat >= feature.length || feat < 0)
  110. return "Invalid feature index.";
  111. else
  112. {
  113. return feature[feat];
  114. }
  115. }
  116.  
  117. public int getFeatureLength()
  118. {
  119. return feature.length;
  120. }
  121.  
  122. public boolean setStatus(char stat)
  123. {
  124. switch (stat)
  125. {
  126. case 'a': status="Active"; break;
  127. case 'c': status="Canceled"; break;
  128. case 's': status="Sold"; break;
  129. default: status="Unknown";
  130.  
  131. }
  132. if (status.equals("Unknown")) return false;
  133. else return true;
  134. }
  135.  
  136. public String getStatus()
  137. {
  138. return status;
  139. }
  140.  
  141. public String getListing()
  142. {
  143. return make + " " + model +" " + year + " " + color + " " + forSalePrice;
  144. }
  145.  
  146. public String getAllDetails()
  147. {
  148. String temp = feature[0];
  149. if(temp == null)
  150. return "Make: " + make + "\n" + "Model: " + model + "\n" + "Year: " + year + "\n" + "Color: " + color + "\n" + "Purchase price: " + purchasePrice + "\n" + "For Sale price: " + forSalePrice;
  151. else
  152. {
  153. for (int i = 1; i < getFeatureLength(); i++) {
  154. if (getFeature(i) != null)
  155. {
  156. temp = temp + ", " + feature[i];
  157. }
  158. }
  159. return "Make: " + make + "\n" + "Model: " + model + "\n" + "Year: " + year + "\n" + "Color: " + color + "\n" + "Purchase price: " + purchasePrice + "\n" + "For Sale price: " + forSalePrice + "\n" + "Features: " + temp;
  160. }
  161. }
  162.  
  163. public double getSalePrice()
  164. {
  165. return forSalePrice;
  166. }
  167. private void updatePriceHistory(double newPrice)
  168. {
  169. priceHistory[4] = priceHistory[3];
  170. priceHistory[3] = priceHistory[2];
  171. priceHistory[2] = priceHistory[1];
  172. priceHistory[1] = priceHistory[0];
  173. priceHistory[0] = newPrice;
  174. }
  175.  
  176. public String getMakeModel()
  177. {
  178. return make + " " + model;
  179. }
  180. }
  181.  
  182.  
  183. import java.util.Scanner;
  184.  
  185. public class UsedCarLot {
  186. public static void main(String[] args) {
  187. Scanner scanner = new Scanner(System.in);
  188. Vehicle[] auto = { new Vehicle(1998, "Chevrolet"), new Vehicle(2012, "BMW", "M3"), new Vehicle(2015, "Ford"),
  189. new Vehicle(2008, "Hyundai", "Sonata", 'a', 12040), new Vehicle(2013, "Jeep", "Wrangler", 'a', 17900),
  190. new Vehicle(2016, "Mazda", "Miata"), new Vehicle(1969, "Ford", "Mustang") };
  191.  
  192. System.out.println("Hi! Welcome to our Car Lot!");
  193.  
  194. int input = 0;
  195.  
  196. System.out.println("Select any number to perform the following operation.");
  197.  
  198. while (input != 9) {
  199. System.out.println("1. Update Vehicle" + "\n" + "2. List Inventory (All)" + "\n"
  200. + "3. List Summary Inventory (For Sale only)" + "\n" + "4. List Extended Vehicle Details" + "\n"
  201. + "9. Exit");
  202. input = scanner.nextInt(); //Provides error relating to this line.
  203.  
  204. switch (input) {
  205. case 1: // update vehicle
  206. System.out.println("Please select the vehicle you would like to update with the corresponding number.");
  207. for (int a = 0; a < auto.length - 1; a++) {
  208. int b = a + 1;
  209. System.out.println(b + ". " + auto[a].getMakeModel());
  210. }
  211. int v = scanner.nextInt();
  212. updateMenu(auto[v - 1]);
  213. break;
  214. case 2: // 2. List Inventory (All)
  215. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  216. for (int a = 0; a < auto.length - 1; a++) {
  217. int c = a + 1;
  218. System.out.println(c + ". " + auto[a].getListing());
  219. }
  220. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  221. break;
  222. case 3: // 3. List Summary Inventory (For Sale only)
  223. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  224. int b = 1;
  225. for (int a = 0; a < auto.length - 1; a++) {
  226. if (auto[a].getStatus().equals("Active")) {
  227. System.out.println(b + ". " + auto[a].getListing());
  228. b++;
  229. }
  230. }
  231. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  232. break;
  233. case 4:// 4. List ALL Vehicle Details
  234. System.out.println("Select the Make/Model of the car you would like to see extended details about.");
  235. for (int a = 0; a < auto.length - 1; a++) {
  236. int d = a + 1;
  237. System.out.println(d + ". " + auto[a].getMakeModel());
  238. }
  239. int detailScanner = scanner.nextInt();
  240. if (detailScanner < 1 || detailScanner > auto.length - 1)
  241. System.out.println("Invalid car number.");
  242. else {
  243. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  244. System.out.println(auto[detailScanner - 1].getAllDetails());
  245. }
  246. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  247. break;
  248. case 9: // exit
  249. System.out.println("Thank you.");
  250. break;
  251. default:
  252. System.out.println("Please enter a number listed.");
  253. }
  254. }
  255. scanner.close();
  256. }
  257.  
  258. private static void updateMenu(Vehicle auto) {
  259. Scanner scan = new Scanner(System.in);
  260. int input = 1;
  261. while (input != 0) {
  262. System.out.println("Please choose from the following:" + "\n" + "1. Update VIN" + "\n" + "2. Update Model"
  263. + "\n" + "3. Update Miles" + "\n" + "4. Update Purchase Price" + "\n" + "5. Update For Sale Price"
  264. + "\n" + "6. Update Status" + "\n" + "7. Update Color" + "\n" + "8. Add Feature" + "\n"
  265. + "9. Remove Feature" + "\n" + "0. Exit");
  266. input = scan.nextInt();
  267. switch (input) {
  268. case 1: // 1. Update VIN
  269. System.out.println("Please enter the desired VIN");
  270. String tempVIN = scan.next();
  271. if (tempVIN.length() == 17) {
  272. auto.setVIN(tempVIN);
  273. System.out.println("VIN successfully set to " + tempVIN + ".");
  274. } else
  275. System.out.println("invalid VIN length.");
  276. break;
  277. case 2: // 2. Update Model
  278. System.out.println("Please enter the desired Model, in either one word, or dashed.");
  279. String tempModel = scan.next();
  280. auto.setModel(tempModel);
  281. System.out.println("Model successfully set to " + tempModel + ".");
  282. break;
  283. case 3: // 3. Update Miles
  284. System.out.println("Please enter the desired milage");
  285. int tempMiles = scan.nextInt();
  286. auto.setMiles(tempMiles);
  287. System.out.println("Milage successfully set to " + tempMiles + ".");
  288. break;
  289. case 4: // 4. Update Purchase Price
  290. System.out.println("Please enter the desired Purchase Price");
  291. double tempPurchase = scan.nextDouble();
  292. auto.setPurchasePrice(tempPurchase);
  293. System.out.println("Purchase successfully set to " + tempPurchase + ".");
  294. break;
  295. case 5: // 5. Update For Sale Price
  296. System.out.println("Please enter the desired Sale Price");
  297. Double tempSale = scan.nextDouble();
  298. if (tempSale < auto.getSalePrice()) {
  299. System.out.println("This price is lower than the current for sale price. Do you wish to continue?"
  300. + "\n" + "1. Yes." + "\n" + "2. No.");
  301. int a = scan.nextInt();
  302. if (a == 1) {
  303. auto.setForSalePrice(tempSale);
  304. System.out.println("Sale Price successfully set to " + tempSale + ".");
  305. } else if (a == 2)
  306. System.out.println("Sale Price not updated.");
  307. else
  308. System.out.println("Invalid key. Please select 5 if you wish to update in the future.");
  309. } else {
  310. auto.setForSalePrice(tempSale);
  311. System.out.println("Sale Price successfully set to " + tempSale + ".");
  312. }
  313. break;
  314. case 6: // 6. Update Status
  315. System.out.println(
  316. "Please enter the desired Status. Enter a for Active Status. Enter c for Canceled Status. Enter s for Sold Status.");
  317. int temp = 0;
  318. while (temp == 0) {
  319. char tempStatus = scan.next().charAt(0);
  320. if (tempStatus != 'a' && tempStatus != 'c' && tempStatus != 's' && tempStatus != 'e')
  321. System.out.println("Invalid character, please try again, or e to exit.");
  322. else if (tempStatus == 'e')
  323. temp = 1;
  324. else {
  325. auto.setStatus(tempStatus);
  326. System.out.println("Status successfully set to " + tempStatus + ".");
  327. temp = 1;
  328. }
  329. }
  330. break;
  331. case 7:// 7. Update Color
  332. System.out.println("Please enter the desired Color, in either one word, or dashed.");
  333. String tempColor = scan.next();
  334. auto.setColor(tempColor);
  335. System.out.println("Color successfully set to " + tempColor + ".");
  336. break;
  337. case 8: // 8. Add Feature
  338. System.out.println("Please enter the desired Feature, in either one word, or dashed.");
  339. String tempFeat = " ";
  340. while (!tempFeat.equals("0")) {
  341. tempFeat = scan.next();
  342. if (!tempFeat.equals("0")) {
  343. auto.setFeature(tempFeat);
  344. System.out.println("You may type another feature, or 0 to exit.");
  345. }
  346. }
  347. break;
  348. case 9: // 9. Remove Feature
  349. System.out.println("Please remove the desired Feature (select number).");
  350. int temp2 = 1;
  351. while (temp2 != 0) {
  352. System.out.println("List of Features, select number to remove.");
  353. for (int i = 0; i < auto.getFeatureLength(); i++) {
  354. if (auto.getFeature(i) != null) {
  355. System.out.println(i + 1 + ". " + auto.getFeature(i));
  356. }
  357. }
  358. System.out.println("0. To Exit.");
  359.  
  360. temp2 = scan.nextInt();
  361. if (temp2 != 0) {
  362. System.out.println("Feature Successfully removed, remove another or select 0 to exit.");
  363. auto.removeFeature(temp2 - 1);
  364. }
  365. }
  366. break;
  367. case 0: // exit
  368.  
  369. break;
  370. default:
  371. System.out.println("Please enter a number listed.");
  372. break;
  373. }
  374. }
  375. scan.close();
  376. }
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement