Superclass och subclass printwriter.
Jag har nyligen börjat arbeta med superklasser och subklasser. Det är fortfarande aningen svårt. Jag har skapar några olika klasser, en klass som heter CreateCustomer, en superklass som heter Customer och en subklass som heter KeyCustomer. Jag har lagt in en printwriter för att kunna skriva till fil. I main anropar jag metoden Createfile. Så långt är allting som det ska tror jag. Jag kan spara ner information till en fil från superklassen, men när jag försöker göra detsamma med subklassen så får jag bara värdet null i dokumentet. Kan någon förklara för mig varför det blir så?
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
//Customer one = new Customer();
//one.createCustomer();
//KeyCustomer two = new KeyCustomer();
//two.createCustomer();
CreateFile three = new CreateFile();
//System.out.println(one);
//System.out.println(two);
}
}
import java.util.Scanner;
public class Customer {
private String firstName;
private String lastName;
private int birthYear;
//Constructor
public Customer(String thefirstName, String thelastName, int thebirthYear) {
this.firstName = thefirstName;
this.lastName = thelastName;
this.birthYear = thebirthYear;
}
//Constructor
public Customer() {
}
//Getter
public String getFirstName() {
return firstName;
}
//Setter
public void setFirstName(String thefirstName) {
this.firstName = thefirstName;
}
//Getter
public String getLastName() {
return lastName;
}
//Setter
public void setLastName(String thelastName) {
this.lastName = thelastName;
}
//Getter
public int getBirthYear() {
return birthYear;
}
//Setter
public void setBirthYear(int thebirthYear) {
this.birthYear = thebirthYear;
}
@Override
public String toString() {
return firstName + lastName + birthYear;
}
// Create customer
public String createCustomer() {
Scanner sc = new Scanner(System.in);
Customer one = new Customer("", "", 0);
System.out.println("Enter first name.");
firstName = sc.next();
System.out.println("Enter last name.");
lastName = sc.next();
System.out.println("Enter birth year.");
birthYear = sc.nextInt();
return firstName + lastName + birthYear;
}
}
import java.util.Scanner;
public class KeyCustomer extends Customer {
private String billingAdress;
private int payMentPlan;
//Constructor
public KeyCustomer(String thefirstName, String thelastName, int thebirthYear, String billingAdress, int payMentPlan) {
super(thefirstName,thelastName,thebirthYear);
this.billingAdress = billingAdress;
this.payMentPlan = payMentPlan;
}
//Constructor
public KeyCustomer() {
super();
}
//Getter
public String getBillingAdress() {
return billingAdress;
}
//Setter
public void setBillingAdress(String billingAdress) {
this.billingAdress = billingAdress;
}
//Getter
public int getPayMentPlan() {
return payMentPlan;
}
//Setter
public void setPayMentPlan(int payMentPlan) {
this.payMentPlan = payMentPlan;
}
@Override
public String toString() {
return billingAdress + payMentPlan;
}
//Create key customer.
public String createKeyCustomer() {
Scanner sc = new Scanner(System.in);
KeyCustomer one = new KeyCustomer("", "", 0, "", 0);
System.out.println("Enter first name.");
String firstName = sc.next();
System.out.println("Enter last name.");
String lastName = sc.next();
System.out.println("Enter birth year.");
int birthYear = sc.nextInt();
System.out.println("Enter billing adress.");
String billingAdress = sc.next();
System.out.println("Enter desired payment plan, " +
"Press 1 for payment each month, " +
"2 for payment each quarter and press " +
"3 for payment every six months.");
int payMentPlan = sc.nextInt();
System.out.println("Information succesfully saved.");
return firstName + lastName + birthYear+ billingAdress + payMentPlan;
}
}
import java.io.*;
import java.util.Scanner;
public class CreateFile {
public CreateFile() throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter path for where to create the new file.");
String loc = sc.next() + ".txt";
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(loc, true)));
System.out.println("Press 1 for key customer and 2 for ordinary customer.");
int choice = sc.nextInt();
if(choice == 2) {
Customer one = new Customer();
one.createCustomer();
pw.println(one);
pw.close();
}
else {
KeyCustomer one = new KeyCustomer();
one.createKeyCustomer();
pw.println(one);
pw.close();
}
}
}
Mvh