Skip to content

Commit 48acb04

Browse files
committed
refactored interfaces studio for chapter 12 studio
1 parent 2f1726a commit 48acb04

5 files changed

Lines changed: 102 additions & 12 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.launchcode;
2+
import java.util.ArrayList;
3+
4+
public abstract class BaseDisc {
5+
private String name;
6+
private int storageCapacity;
7+
private int remainingCapacity;
8+
private int capacityUsed;
9+
private String diskType;
10+
private ArrayList<String> contents;
11+
12+
public BaseDisc(String aName, int maxCapacity, String aType, int someUsedCapacity) {
13+
name = aName;
14+
storageCapacity = maxCapacity;
15+
diskType = aType;
16+
capacityUsed = checkCapacity(someUsedCapacity);
17+
remainingCapacity = spaceLeft();
18+
}
19+
20+
private int checkCapacity(int dataWritten) {
21+
if (storageCapacity < dataWritten){
22+
return storageCapacity;
23+
}
24+
return dataWritten;
25+
}
26+
27+
private int spaceLeft(){
28+
return storageCapacity - capacityUsed;
29+
}
30+
31+
public String diskInfo(){
32+
String output = String.format("\nDisk Name: %s\nMax capacity: %d" +
33+
"\nSpace used: %d" +
34+
"\nAvailable space: %d\n", name, storageCapacity, capacityUsed, remainingCapacity);
35+
return output;
36+
}
37+
38+
public String writeData(int dataSize){
39+
if (dataSize > remainingCapacity){
40+
return "Not enough disc space!";
41+
}
42+
capacityUsed += dataSize;
43+
remainingCapacity -= dataSize;
44+
45+
return "Data written to disc. Remaining space = " + remainingCapacity;
46+
}
47+
48+
}
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
package org.launchcode;
22

3-
public class CD {
4-
// TODO: Implement your custom interface.
3+
public class CD extends BaseDisc implements OpticalDisc{
54

6-
// TODO: Determine which fields, methods, and constructors can be extended from the base class and which ones
7-
// need to be declared separately.
5+
public CD(String aName, int maxCapacity, String aType, int someUsedCapacity){
6+
super(aName, maxCapacity, aType, someUsedCapacity);
7+
}
8+
9+
@Override
10+
public void spinDisc() {
11+
System.out.println("A CD spins at a rate of 200 - 500 rpm.");
12+
}
13+
14+
@Override
15+
public void readData() {
16+
System.out.println("Would you like to play a game?");
17+
}
818
}
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
package org.launchcode;
22

3-
public class DVD {
4-
// TODO: Implement your custom interface.
3+
public class DVD extends BaseDisc implements OpticalDisc {
4+
5+
public DVD(String aName, int maxCapacity, String aType, int someUsedCapacity) {
6+
super(aName, maxCapacity, aType, someUsedCapacity);
7+
}
8+
9+
@Override
10+
public void spinDisc() {
11+
System.out.println("A DVD spins at a rate of 570 - 1600 rpm.");
12+
}
13+
14+
@Override
15+
public void readData() {
16+
System.out.println("I'm sorry, Dave. I'm afraid I can't do that.");
17+
}
18+
519

6-
// TODO: Determine which fields, methods, and constructors can be extended from the base class and which ones
7-
// need to be declared separately.
820
}
21+
22+
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
package org.launchcode;
22

33
public class Main {
4-
public static void main(String[] args) {
54

6-
// TODO: Declare and initialize a CD and a DVD object.
5+
public static void main(String[] args){
76

8-
// TODO: Call each CD and DVD method to verify that they work as expected.
7+
CD cd = new CD("CD Example", 700, "CD-R", 350);
8+
DVD dvd = new DVD("DVD Example", 4700, "DVD-R", 1450);
9+
10+
cd.spinDisc();
11+
dvd.spinDisc();
12+
13+
cd.readData();
14+
dvd.readData();
15+
16+
System.out.println(cd.writeData(275));
17+
System.out.println(dvd.writeData(8000));
18+
19+
System.out.println(cd.diskInfo());
20+
System.out.println(dvd.diskInfo());
921
}
10-
}
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.launchcode;
2+
3+
public interface OpticalDisc {
4+
void spinDisc();
5+
void readData();
6+
}

0 commit comments

Comments
 (0)