Refactoring Java code etc -
okay, i'll honest: schoolwork. it's not i'm lazy, i've studies soooo , hard, still can't grip of language.. :(
it's introduction online-course, have noone talk irl.
i'm trying make program car mechanic register incoming vehicles, save objects arraylist , when user doesn't wanna register more vehicles, program read list , write out. feature have included user able search regnr , information vehicle.
the assignment partly take code below made in assignment, , refactor classes, objects, methods etc , think if that, i'll figure rest out.
public static void main(string[] args) throws ioexception { //write list.txt // file myfile = new file("list.txt"); filewriter myfilewriter = new filewriter(myfile, true); printwriter myprintwriter = new printwriter(myfilewriter); string regnr; string nr; int year; string inyear; string model; string name; string date; string indate; int meter; int time; string size; int quest = 0; { // user input: regnr, year, model, first , last name// nr = joptionpane.showinputdialog(null, "registration number: "); regnr = nr.touppercase(); inyear = joptionpane.showinputdialog(null, "year of manufacture: "); year = integer.parseint(inyear); model = joptionpane.showinputdialog(null, "model: "); name = joptionpane.showinputdialog(null, "owner: "); // todays date // indate= calendar.getinstance().gettime().tostring(); date = indate.substring(0, 10); //substring day : month : date // expected service time through .random // double min = 1500 + math.random(); double max = math.random() * (40000 - min); double number = min + max; meter = (int) number; time = meter / 1500; // recommended service size// if (meter <= 8000) { size = "a small service recommended."; } else if (meter > 8000 && meter <= 20000) { size = "a medium size service recommended"; } else { size = "a large service recommended"; } myprintwriter.println("car: " + model + " " + regnr); myprintwriter.println("year of manufacture: " + year); myprintwriter.println("owner: " + name); myprintwriter.println("meter: " + meter + " mil"); myprintwriter.println("start date: " + date); myprintwriter.println("expected time: " + time + " dagar \n" + size); myprintwriter.println("\n"); quest = joptionpane.showconfirmdialog(null, "do want continue?"); } while (quest != 1); myprintwriter.close(); // read list.txt // try (bufferedreader br = new bufferedreader(new filereader("list.txt"))) { stringbuilder sb = new stringbuilder(); string line = br.readline(); while (line != null) { sb.append(line); sb.append(system.lineseparator()); line = br.readline(); } string list = sb.tostring(); br.close(); joptionpane.showmessagedialog(null, list); } } }
i've refactored code, split 4 classes. car, userinteraction, fileinteraction , entrypoint.
car holding information. registrationnumber, model, owner , on. used getters there.
public class car{ private final string registrationnumber; private final int manufactureyear; private final string model; private final string date; private final string owner; private final int drivenmeters; private final string servicelevel; public car( string registrationnumber, int manufactureyear, string model, string owner, int drivenmeters, string servicelevel, string date ){ this.registrationnumber = registrationnumber; this.manufactureyear = manufactureyear; this.model = model; this.owner = owner; this.drivenmeters = drivenmeters; this.servicelevel = servicelevel; this.date = date; } public string getregistrationnumber(){ return registrationnumber; } public int getmanufactureyear(){ return manufactureyear; } public string getmodel(){ return model; } public string getdate(){ return date; } public string getowner(){ return owner; } public int getdrivenmeters(){ return drivenmeters; } public string getservicelevel(){ return servicelevel; } } userinteraction layer between application , user using it. asks user input values. displays message if wants continue , displays cars registered.
import javax.swing.*; import java.util.calendar; public class userinteraction{ public static car fillcarwithuserinput(){ // ask user values final string model = joptionpane.showinputdialog(null, "model: "); final string registrationnumber = joptionpane.showinputdialog(null, "registration number: ").touppercase(); final int manufactureyear = integer.parseint(joptionpane.showinputdialog(null, "year of manufacture: ")); final string owner = joptionpane.showinputdialog(null, "owner: "); // randomly generate meters double min = 1500 * math.random(); double max = math.random() * (40000 - min); final int drivenmeters = (int) (min + max); // determine service level final string servicelevel; if( drivenmeters <= 8000 ){ servicelevel = "a small service recommended."; } else if( drivenmeters <= 20000 ){ servicelevel = "a medium size service recommended"; } else{ servicelevel = "a large service recommended"; } // today date final string date = calendar.getinstance().gettime().tostring().substring(0, 10); // return new car object containing values return new car(registrationnumber, manufactureyear, model, owner, drivenmeters, servicelevel, date); } public static boolean userwantstocontinue(){ return joptionpane.showconfirmdialog(null, "do want continue?") != 1; } public static void showcarstouser( string cars ){ joptionpane.showmessagedialog(null, cars); } } fileinteraction layer between application , filesystem. writes list.txt reads it.
import java.io.*; import java.util.objects; import java.util.stream.collectors; public class fileinteraction implements closeable{ private static final string filename = "list.txt"; private printwriter writer; public void printcar( car car ) throws ioexception{ // lazily initialize writer if( writer == null ){ writer = new printwriter(new filewriter(filename, true)); } writer.println("car: " + car.getmodel() + " " + car.getregistrationnumber()); writer.println("year of manufacture: " + car.getmanufactureyear()); writer.println("owner: " + car.getowner()); writer.println("meter: " + car.getdrivenmeters() + " mil"); writer.println("start date: " + car.getdate()); writer.println("expected time: " + car.getdrivenmeters() / 1500 + " dagar \n" + car.getservicelevel()); writer.println("\n"); } public string readcars(){ // read cars file string carlist; try( bufferedreader reader = new bufferedreader(new filereader(filename)) ){ // following 3 lines new java8 called stream quite handy carlist = reader.lines() // returns lines stream .filter(objects::nonnull) // eliminates null values .collect(collectors.joining("\n")); // appends lines } catch( ioexception e ){ // print stack e.printstacktrace(); // if exception occurs assing empty string carlist = ""; } return carlist; } @override public void close() throws ioexception{ if( writer != null ){ writer.flush(); writer.close(); } } } and entrypoint used bring together. name states entrypoint application. class done.
import java.io.ioexception; public class entrypoint{ public static void main( string[] args ){ try( fileinteraction fileinteraction = new fileinteraction() ){ do{ final car car = userinteraction.fillcarwithuserinput(); fileinteraction.printcar(car); } while( userinteraction.userwantstocontinue() ); string cars = fileinteraction.readcars(); userinteraction.showcarstouser(cars); } catch( ioexception e ){ // print stack e.printstacktrace(); // exit application throw new illegalstateexception("encountered exception!", e); } } } this quite load of work, hope helps you. if have done maybe can inspired this.
Comments
Post a Comment