/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
public class Main
{
public static void main(String[] args) {
UsedCarLot u=new UsedCarLot();
u.printAllSales();
u.sortByProfit();
u.printAllSales();
}
}
/**
* This class will keep track of the sale of a used car
*
* @author Jeff Borland
* @version 1.08
*/
public class Sale
{
private String sellerName;
private int salePrice;
private int carValue;
private int profit;
private int date; //in format 20080212 yyyymmdd
public Sale (int date, String sellerName, int salePrice, int carValue)
{
this.date=date;
this.sellerName=sellerName;
this.salePrice=salePrice;
this.carValue=carValue;
profit=salePrice-carValue;
}
public String getSellerName()
{
return sellerName;
}
public int getDate()
{
return date;
}
public int getSalePrice()
{
return salePrice;
}
public void setSalePrice(int salePrice) {
this.salePrice = salePrice;
}
public int getCarValue()
{
return carValue;
}
public int getProfit()
{
profit=salePrice-carValue;
return profit;
}
@Override
public String toString() {
return "Sale [sellerName=" + sellerName + ", salePrice=" + salePrice + ", carValue=" + carValue + ", profit="
+ profit + ", date=" + date + "]";
}
}
/**
* A class to practice arraylist of objects (of sales)
*
* @author (your name)
* @version (a version number or a date)
*/
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Scanner;
public class UsedCarLot
{
private ArrayList<Sale> allSales=new ArrayList<Sale>();
public UsedCarLot()
{
initializeSales(); //initalizes allSales
}
//The method below will initialize the 50 sales from the text file
public void initializeSales()
{
try
{
Scanner scan=new Scanner(new File("sales.txt"));
while (scan.hasNext())
{
String theLine=scan.nextLine(); //This reads in the next player line
String[] splits=theLine.split(",");
allSales.add(new Sale(Integer.parseInt(splits[0]),splits[1],Integer.parseInt(splits[2]),Integer.parseInt(splits[3])));
//System.out.print("r");
}
System.out.println();
}
catch (IOException e){
System.out.print("Trouble reading");
}
}
public void printAllSales ()
{
System.out.println("Printing out all sales");
for (Sale s: allSales)
System.out.println(s);
System.out.println();
}
//use selection sort
public void sortByProfit()
{
}
//use insertion sort
public void sortByName()
{
}
/*********
* BELOW FOR teacher purposes only.
*/
//The method below will initialize 50 sales randomly
public static void randomSales()
{
int DEFAULTNUMOFSALES=50;
String[] employees={"Ben","Logan", "Darcy", "Abdullahi", "Will"};
NumberFormat twoDigits=NumberFormat.getIntegerInstance();
for (int i=0; i<DEFAULTNUMOFSALES; i++)
{
String randomEmployee=employees[(int)(Math.random()*5)];
twoDigits.setMinimumIntegerDigits(2);
String randomDateString=""+((int)(Math.random()*3)+2016)+twoDigits.format(((int)(Math.random()*12)+1))+twoDigits.format(((int)(Math.random()*31)+1));
int randomDate=Integer.parseInt(randomDateString);
int salePrice=(int)(Math.random()*15500+1400);
int carValue=salePrice-((int)(Math.random()*3500)-500);
System.out.println (randomDate+","+ randomEmployee+","+salePrice+","+carValue);
}
}
}
20160204,Will,13024,10285
20180911,Abdullahi,12478,11321
20170312,Darcy,11761,12233
20170405,Logan,15612,14760
20170115,Abdullahi,7330,6630
20180805,Will,10615,10360
20170630,Abdullahi,10167,8281
20180713,Darcy,5876,5827
20170211,Will,10465,8053
20180928,Logan,16294,14042