Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

when i add try to record tranaction nothing goes into the text file can someone please fix this code, im mainly corcern with the transaction

when i add try to record tranaction nothing goes into the text file can someone please fix this code, im mainly corcern with the transaction text file area and the search make and model area please help.

import java.util.List; import java.util.Scanner;

public class DealershipProgram {

public static void main(String[] args) { Scanner scannerin = new Scanner(System.in); Scanner input = new Scanner(System.in); SalesAssociate employee = null;

boolean continueLoop = true;

do {

System.out.println("(1) To create SalesAssociate account");

System.out.println("(2) to login");

int Account = scannerin.nextInt();

switch (Account) {

case 1:

System.out.print("Enter SalesAssociate name: ");

String name_input = input.nextLine();

System.out.print("Enter the Adress of SalesAssociate: ");

String Adress_input = input.nextLine();

System.out.print("Enter total sales of SalesAssociate: ");

double TotalSales_input = input.nextDouble();

System.out.print("Enter salesAssociateID: ");

String ID_input = input.next();

System.out.print("Enter salesAssociate password: ");

String Password_input = input.next();

SalesAssociate newSalesAssociate = new SalesAssociate(name_input,Adress_input,TotalSales_input,ID_input,Password_input);

CarDealershipSystem.addSAssociates(newSalesAssociate);

break;

case 2:

if (employee == null) {

do {

System.out.print("Please enter your associate ID#: ");

String id = scannerin.next();

employee = CarDealershipSystem.checkAssociate(id);

} while (employee == null);

do {

System.out.print("Please enter your associate Password: ");

String pass = scannerin.nextLine();

if (employee.getPassword().equals(pass))

break;

} while (true);

}}

System.out.println("Please enter a number to complete one of the following action(1-7): "); System.out.println("1) Search inventory"); System.out.println("2) Remove cars from inventory"); System.out.println("3) Add cars to inventory"); System.out.println("4) Record transaction"); System.out.println("5) View sold cars"); System.out.println("6) View total sales"); System.out.println("7) Exit");

int option = scannerin.nextInt(); switch (option) { case 1: System.out.println("Please enter the make: "); String CarModel = input.nextLine().trim(); System.out.println("Enter the model: "); String CarMake = input.nextLine();

List matched = CarDealershipSystem.FindCars(CarMake, CarModel);

if (matched.isEmpty()) { System.out .println("No cars with that precedent were found."); } else { System.out.println("List of matched cars: "); } for (Car car : matched) { System.out.println("\tVin: " + car.getVin() + ", Model: " + car.getModel() + ", Color: " + car.getColor() + ", Make: " + car.getMake() + ", Year: " + car.getYear() + ", Price: " + car.getPrice()); } break;

case 2:

Car car = null; do { System.out .print("Enter the VIN Number of the car you are removing: "); String vin = input.nextLine(); car = CarDealershipSystem.getCar(vin); if (car == null) System.out.println("Car cannot be found: "); } while (car == null);

CarDealershipSystem.removeCar(car); break;

case 3: System.out.print("Enter a car VIN Number: "); String Vin = input.nextLine(); System.out.print("Enter the car model: "); String Model = input.nextLine(); System.out.print("Enter the car color: "); String color = input.nextLine(); System.out.print("Enter the car make: "); String Make = input.nextLine(); System.out.print("Enter the car's year: "); int year = input.nextInt(); System.out.print("Enter the cars' price: "); double price = input.nextDouble();

Car newCar = new Car(Make, Model, year, color, Vin, price); Car anotherCar = new Car (Make, Model, year, color, Vin, price); CarDealershipSystem.setCar(newCar, anotherCar); break;

case 4: System.out.println("Enter a VIN Number for the car being sold: "); String vin = input.nextLine(); car = CarDealershipSystem.getCar(vin);

System.out.print("Please enter the customer's name: "); String name = input.nextLine();

System.out.print("Please enter the customer's address: "); String address = input.nextLine(); System.out.print("Please enter the customer's account number: "); int acn = input.nextInt(); System.out.print("Please enter the customer's phone number: "); String phoneNumber = input.nextLine();

String id = null; Customer customer = new Customer(name, address, id, acn,phoneNumber);

CarDealershipSystem.transactionCollection(car, employee, customer); break;

case 5: List soldCars = CarDealershipSystem.getSoldCars();

System.out.println("Cars Sold: ");

for (Car c : soldCars) { System.out.println("Vin: " + c.getVin() + ", Model: " + c.getModel() + ", Color: " + c.getColor() + ", Make: " + c.getMake() + ", Year: " + c.getYear() + ", Price: " + c.getPrice()); } break;

case 6: int total = 0; for (SalesAssociate s : CarDealershipSystem.people) { System.out.println(s.getName() + ", " + s.gettotalSales()); total += s.gettotalSales(); }

System.out.println("Total cars sold:" + total); break;

case 7: System.out.println("Thank you for shopping!"); continueLoop = false; System.exit(0); default: continueLoop = false; }

} while (continueLoop);

scannerin.close(); } } ----------------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Scanner;

public class CarDealershipSystem {

public static ArrayList cars = new ArrayList(); public static ArrayList people = new ArrayList(); public static ArrayList trades = new ArrayList();

public static SalesAssociate getAssociate(String id) { for (SalesAssociate s : people) { if (s.getId().equals(id)) return s; } return null; }

// 1st bullet point public static ArrayList FindCars(String make, String model) {

ArrayList foundCar = new ArrayList(); foundCar.clear(); for (Car car : carCollection()) { if (car.getModel().equals(model) && car.getMake().equals(make)) foundCar.add(car); } return foundCar; }

// 2nd bullet point public static ArrayList carCollection() { String file = "cars.txt"; Scanner reader = null;

try { reader = new Scanner(new FileReader(file));

while (reader.hasNext()) { String nextLine = reader.nextLine(); String[] parse = nextLine.split(",");

String vin = parse[0]; String make = parse[1]; String model = parse[2]; String color = parse[3]; int year = Integer.parseInt(parse[4]); double price = Double.parseDouble(parse[5]);

cars.add(new Car(make, model, year, color, vin, price)); }

reader.close(); // closes the stream } catch (FileNotFoundException e) { System.out.println("Error opening file!"); System.exit(0); } return cars; }

// 3rd bullet point public static ArrayList employeeCollection() { String file = "employees.txt"; Scanner reader = null;

try { reader = new Scanner(new FileReader(file)); while (reader.hasNext()) { String nextLine = reader.nextLine(); String[] split = nextLine.split(",");

String name = split[0]; String address = split[1]; double sales = Double.parseDouble(split[2]); String id = split[3]; String pass = split[4];

people.add(new SalesAssociate(name, address, sales, id, pass)); } reader.close(); // closes the stream } catch (FileNotFoundException e) { System.out.println("Error opening file!"); System.exit(0); } return people; }

// 4th bullet point public static void transactionCollection(Car car, SalesAssociate employee, Customer person) { String date = "", time = ""; SimpleDateFormat mdyDate = new SimpleDateFormat("MM-dd-yyyy"); SimpleDateFormat hmTime = new SimpleDateFormat("hh:mm a"); Calendar c = Calendar.getInstance(); time = hmTime.format(c.getTime()); date = mdyDate.format(c.getTime()); Transaction transaction = new Transaction(person, car, employee, date + time); trades.add(transaction); }

// 5th bullet point public static void addNewTransactions(Transaction transaction) { Calendar c = Calendar.getInstance();

try { File file = new File("./transactions.txt");

if (!file.exists()) file.createNewFile();

PrintWriter fileWriter = new PrintWriter(new FileOutputStream(file, true));

String output = transaction.getDate() + "," + c.getTime().toString() + "," + transaction.getCustomer() + "," + transaction.getCar() + "," + transaction.getEmployee();

fileWriter.write(output);

fileWriter.flush(); fileWriter.close();

} catch (IOException e) { } }

// 6th bullet point public void addCars(Car car) {

cars.add(car);

try { String file = "./cars.txt";

PrintWriter fileWriter = new PrintWriter(new FileOutputStream(file, true));

for (Car c : cars) { String output = c.getMake() + "," + c.getModel() + "," + c.getYear() + "," + "," + c.getColor() + "," + c.getVin();

fileWriter.write(output); fileWriter.flush(); }

fileWriter.close();

} catch (IOException e) { } }

// 7th bullet point public static void addSAssociates(SalesAssociate employee) { people.add(employee); try { String file = "./employees.txt";

PrintWriter fileWriter = new PrintWriter(new FileOutputStream(file, true));

for (SalesAssociate workers : people){

String output = workers.getName() + "," + workers.getAddress() + "," + workers.gettotalSales() + "," + workers.getId() + "," + workers.getPassword();

fileWriter.write(output); fileWriter.flush(); } fileWriter.close();

} catch (IOException e) { } }

public static Car getCar(String vin) { for (Car car : carCollection()) { if (car.getVin().equals(vin)) return car; } return null; }

public static void removeCar(Car vehicle) { cars.remove(vehicle);

try { PrintWriter words = new PrintWriter("./cars.txt"); for (Car car : cars) words.println(car.getVin() + "," + car.getMake() + "," + car.getModel() + "," + car.getColor() + "," + car.getYear() + "," + car.getPrice()); words.close();

} catch (FileNotFoundException e) { } }

public static Transaction getTrade(Car car) { for (Transaction trade : trades) { if (trade.getCar() == car) return trade; } return null; }

public static List getSoldCars() { ArrayList soldCars = new ArrayList(); for (Car car : cars) { Transaction transaction = getTrade(car); if (transaction == null) continue; soldCars.add(car); } return soldCars; }

public static void setCar(Car oldCar, Car newCar) { if (oldCar != null && cars.contains(oldCar)) cars.set(cars.indexOf(oldCar), newCar); else cars.add(newCar);

try { PrintWriter pWriter = new PrintWriter("./cars.txt"); for (Car automobile : cars) pWriter.println(automobile.getVin() + "," + automobile.getMake() + "," + automobile.getModel() + "," + automobile.getColor() + "," + automobile.getYear() + "," + automobile.getPrice()); pWriter.close(); } catch (FileNotFoundException e) { } }

public static SalesAssociate checkAssociate(String id) { for (SalesAssociate workers : employeeCollection()) { if (workers.getId().equalsIgnoreCase(id)) return workers; } return null; } } -------------------------------------------------------------------------------- public class Car { // private class variables private String make; private String model; private int year; private String color; private String vin; private double price;

// Constructor public Car(String m, String mo, int y, String co, String v, double p) { make = m; model = mo; year = y; color = co; vin = v; price = p; }

// Getters and Setters below public String getMake() { return make; }

public void setMake(String make) { this.make = make; }

public String getModel() { return model; }

public void setModel(String model) { this.model = model; }

public int getYear() { return year; }

public void setYear(int year) { this.year = year; }

public String getColor() { return color; }

public void setColor(String color) { this.color = color; }

public String getVin() { return vin; }

public void setVin(String vin) { this.vin = vin; }

public double getPrice() { return price; }

public void setPrice(double price) { this.price = price; } } --------------------------------------------------------------------------------- public class Customer { private String name; private String address; private String customerID; private int accountNumber; private String phoneNumber;

// Constructor public Customer(String n, String ad, String id, int ac, String pn) { name = n; address = ad; customerID = id; accountNumber = ac; phoneNumber = pn; }

// Getters and Setters below public String getName() { return name; }

public String setname(String name) { this.name = name; return this.name; }

public String getAddress() { return address; }

public void setAddress(String address) { this.address = address; }

public String getId() { return customerID; }

public void setcustomerID(String id) { customerID = id; }

public double getAccountNumber() { return accountNumber; }

public void setAccountNumber(int ac) { accountNumber = ac; }

public String getPhoneNumber() { return phoneNumber; }

public void setPhoneNumber(String number) { phoneNumber = number; }

} ------------------------------------------------------------------------- public class SalesAssociate { private String name; private String address; private double totalSales; private String salesAssociateID; private String password;

// Constructor public SalesAssociate(String n, String ad, double sales, String id, String pword) { name = n; address = ad; totalSales = sales; salesAssociateID = id; password = pword; }

// Getters and Setters below public String getName() { return name; }

public void setName(String name) { this.name = name; }

public String getAddress() { return address; }

public void setAddress(String address) { this.address = address; }

public double gettotalSales() { return totalSales; }

public void setTotalSales(double sales) { totalSales += sales; }

public String getId() { return salesAssociateID; }

public void setSalesAssociateID(String id) { salesAssociateID = id; }

public String getPassword() { return password; }

public void setPassword(String pword) { password = pword; } }

------------------------------------------------------------------- public class Transaction { private String date; private String time; private double cost; private Customer customer; private Car car; private SalesAssociate employee;

// Constructor public Transaction(Customer customer, Car car, SalesAssociate employee, String time) { this.customer = customer; this.car = car; this.employee = employee; this.time = time; }

// Getters and Setters below public String getDate() { return date; }

public void setDate(String date) { this.date = date; }

public String getTime() { return time; }

public void setTime(String time) { this.time = time; }

public double getCost() { return cost; }

public void setCost(double cost) { this.cost = cost; }

public Customer getCustomer() { return customer; }

public void setCustomer(Customer customer) { this.customer = customer; }

public Car getCar() { return car; }

public void setCar(Car car) { this.car = car; }

public SalesAssociate getEmployee() { return employee; }

public void setSalesAssociate(SalesAssociate employee) { this.employee = employee; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions