Skip to content

Commit

Permalink
v2. Added features on Table and List
Browse files Browse the repository at this point in the history
  • Loading branch information
soybean15 committed Aug 4, 2022
1 parent 1be03c5 commit 16b852d
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 58 deletions.
120 changes: 87 additions & 33 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,60 +1,114 @@
import cscreen.classes.Position;

import cscreen.classes.Symbol;
import cscreen.components.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


public class Main {


public static void main(String[] args) {

List<List<String>> arr = new ArrayList<>();
arr.add(Arrays.asList("Marilyn Monroe", "21", "March", "1993"));
arr.add(Arrays.asList("Robert De Niro", "22", "August", "1945"));
arr.add(Arrays.asList("Malcolm X", "23", "June", "1944"));
arr.add(Arrays.asList("Mohammad Ali", "24", "March", "1970"));
Scanner sc = new Scanner(System.in);

String[] header = {"Name", "Id", "Month", "Year"};
CTable table = new CTable(header, arr);
table.addRow(new String[]{"Mike Tyson", "25", "April", ""});
table.display();
Screen screen = new Screen(20, 40, true);//20 rows, 40column, hasBorder
screen.useBoxSet();//use box-window char
screen.setTitle("Student Info");

//String[] header2 = {"Name","Id", "Month"};
CTable table2 = new CTable(null);
table2.addList(arr);
table2.useBoxSet();
table2.hasSeparator(true);
Label label1 = new Label(4,3);
label1.setText("FirstName:");
label1.place(screen);

table2.addRow(new String[]{"Mike Tyson", "25", "April", "1980"});
TextBox txtFirstName = new TextBox(3,13);
txtFirstName.setWidth(23);
txtFirstName.place(screen);

System.out.println(table2.getCell(0,0));//print marilyn
table2.setCell(0,0,"Betty White");// change marilyn to betty
table2.removeRow(1); // remove Robert De Niro
table2.setAlignment(Position.CENTER);
table2.display();
Label label2 = new Label(7,3);
label2.setText("LastName:");
label2.place(screen);

// String[] list1 = {"Banana", "Apple", "Potato", "Orange"};
//
// CList cList1 = new CList();
// cList1.setTitle("Fruitserwerr",Position.CENTER);
//
// cList1.display();
// cList1.addItem("Strawberry");
// cList1.addItem("Dragon Fruit34234");
//
// cList1.remove(0);
// cList1.set(0,"Mango");
// cList1.display();
TextBox txtLastName = new TextBox(6,13);
txtLastName.setWidth(23);
txtLastName.place(screen);

//System.out.println(cList1.getItem(0));
Label label3 = new Label(10,3);
label3.setText("DOB:");
label3.place(screen);

TextBox txtDOB = new TextBox(9,13);
txtDOB.setWidth(23);
txtDOB.place(screen);

Label label4 = new Label(13,3);
label4.setText("Age:");
label4.place(screen);

TextBox txtAge = new TextBox(12,13);
txtAge.setWidth(23);
txtAge.place(screen);

Label label5 = new Label(16,3);
label5.setText("Course:");
label5.place(screen);

TextBox txtCourse = new TextBox(15,13);
txtCourse.setWidth(23);
txtCourse.place(screen);

screen.display();

CTable table = new CTable(new String[]{"Full Name","DOB","Age","Course"});
table.useBoxSet();

while (true){
//clear textBox
txtFirstName.clear();
txtLastName.clear();
txtDOB.clear();
txtAge.clear();
txtCourse.clear();

System.out.println("FirstName:");
String firstname = sc.nextLine();
txtFirstName.setText(firstname);
txtFirstName.place(screen);
screen.display();

System.out.println("LastName:");
String lastname = sc.nextLine();
txtLastName.setText(lastname);
txtLastName.place(screen);
screen.display();

System.out.println("DOB:");
String dob = sc.nextLine();
txtDOB.setText(dob);
txtDOB.place(screen);
screen.display();

System.out.println("LastName:");
String age = sc.nextLine();
txtAge.setText(age);
txtAge.place(screen);
screen.display();

System.out.println("LastName:");
String course = sc.nextLine();
txtCourse.setText(course);
txtCourse.place(screen);
screen.display();

table.addRow(new String[]{firstname+" "+lastname,dob,age,course});
table.display();



}

}
}
80 changes: 63 additions & 17 deletions src/cscreen/components/CTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class CTable extends CList {
int[] spaces;

private List<List<String>> list2D;
private List<List<String>> tempList2D;

private boolean onSearch;



private boolean hasSeparator;
Expand Down Expand Up @@ -84,30 +88,37 @@ public CTable(String[] columnHeader, List<List<String>> arr, Position pos,boolea

List<List<String>> copy (List<List<String>> arr){
List<List<String>> newArr = new ArrayList<>();
// String[][] newArr = new String[arr.length+1][arr[0].length];

newArr.add(Arrays.asList(columnHeader));
newArr.addAll(arr);
// newArr[0] = columnHeader;
// for(int i = 1; i<arr.size();i++){
// for (int j = 0;j<newArr[i].length;j++){
// newArr[i][j]=arr[i-1][j];
//
// }
//
// }


return newArr;
}

void init(){
int len = this.list2D.size()+2;
if (columnHeader != null) {
this.list2D = copy(this.list2D);
len = this.list2D.size() + 3;


if(!onSearch){

tempList2D = new ArrayList<>(this.list2D);
}

int len = this.tempList2D.size()+2;

if(!onSearch) {
if (columnHeader != null) {
this.tempList2D = copy(this.list2D);
len = this.tempList2D.size() + 3;

}
}else {
len = this.tempList2D.size() + 3;
}
int colSize = combineRow(this.list2D) + 1;

onSearch = false;

int colSize = combineRow(tempList2D) + 1;
this.screen = new char[len][colSize];

}
Expand All @@ -132,6 +143,7 @@ public void display() {


private int combineRow(List<List<String>> arr) {
list = new ArrayList<>();

char separator = charSets.vertical;
if(!hasSeparator){
Expand Down Expand Up @@ -221,9 +233,7 @@ void addColumnHeader(String str) {
}
}

public void addList(List<List<String>> arr){
this.list2D =arr;
}


private void generateScreen() {
init();
Expand All @@ -238,9 +248,12 @@ private void generateScreen() {
//
// }


String str = "";
for (int i = 0; i < screen.length; i++) {
if (i > start && i < end) {


str = list.get(idx);
idx++;

Expand Down Expand Up @@ -377,4 +390,37 @@ public void setCell(int row, int column,String str){
list2D.get(row).set(column,str);
}

public void addList(List<List<String>> arr){
if(this.list2D.get(0).get(0).equals(" ")){
this.list2D.remove(0);
}
this.list2D.addAll(arr);
//this.list2D= arr;
}


private List<List<String>> findItem(int column, String item){
List<List<String>> newArr = new ArrayList<>();
for (int i=0;i<this.list2D.size();i++){
if(list2D.get(i).get(column).equals(item)){
newArr.add(list2D.get(i));
}
}
return newArr;

}
public void searchRow(int column, String item){

List<List<String>> newList = new ArrayList<>(findItem( column, item));
if(!newList.isEmpty()){
tempList2D = new ArrayList<>(newList);
System.out.println(tempList2D.size()+" item(s) out of "+list2D.size()+" row(s) Found");
if (columnHeader != null) {
tempList2D = copy(tempList2D);
}
onSearch=true;
}else{
System.out.println("No item found");
}
}
}
8 changes: 1 addition & 7 deletions src/cscreen/components/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,7 @@ public void generateScreen() {

}

void clearScreen(){
for(int i=2; i<screen.length-1; i++){
for(int j=2; j<screen[i].length-1; j++){
screen[i][j]=' ';
}
}
}


public void display(){

Expand Down
22 changes: 21 additions & 1 deletion src/cscreen/components/TextBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
public class TextBox extends Box{
String text="";

char[][] screen;

//┌,┐,└,┘

private int height = 3;
Expand All @@ -27,10 +29,28 @@ public void setHeight(int height){
this.height = height;
}

public void clear(){
int start = c;
int end = 0;
if(this.width ==0){
end = (c+text.length())+2;
}else {
if(width<text.length()){
text = text.substring(0, width);

}
end = (c+width)+2;
}

for (int i =start+1; i<end-1;i++){
screen[r+1][i]=' ';
}
}


@Override
public void place(Screen sc){
char[][] screen = sc.screen;
screen = sc.screen;

charSets =sc.charSets;

Expand Down

0 comments on commit 16b852d

Please sign in to comment.