dbAssignments.java

来源:互联网 发布:java push 编辑:程序博客网 时间:2024/06/14 17:06
//database 1, assignment 1
//Fangjia Luo


import java.util.*;


class dbAssignments {


ArrayList<String> cols = new ArrayList<String>();
HashMap<String, String> funs = new HashMap<String, String>();
ArrayList<String> keys = new ArrayList<String>();


void inputCols() {
cols.clear();
System.out
.println("Input the names of the attributes, for simplicity upper case single characters will do,input the end will end the inputing");
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str = in.nextLine();
if (str.toLowerCase().equals("end"))
break;
for (int i = 0; i < str.length(); i++) {
String s = str.substring(i, i + 1).toUpperCase();
if (!cols.contains(s))
cols.add(s);
}
}
}


void inputFuns() {
funs.clear();
System.out
.println("Input the FDs,like A-B or AB-C,end will exit the inputing");
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str = in.nextLine();
if (str.toLowerCase().equals("end"))
break;
String[] fs = str.split(",");
for (String fun : fs) {
String[] s = fun.split("-");
if (s.length == 2)
funs.put(s[0].toUpperCase(), s[1].toUpperCase());
}
}
}


boolean isInclude(String str1, String str2) {
boolean flag = true;
for (int i = 0; i < str1.length(); i++) {
if (str2.indexOf(str1.charAt(i)) == -1) {
flag = false;
break;
}
}
return flag;
}


String addStr(String small, String big) {
String str = big;
for (int i = 0; i < small.length(); i++) {
if (big.indexOf(small.charAt(i)) == -1) {
str = str + small.substring(i, i + 1);
}
}
return str;
}


String getClosure(String col, HashMap<String, String> fs) {
String result = col.toUpperCase();
boolean bflag = true;
while (bflag) {
bflag = false;
for (String s : fs.keySet()) {
if (isInclude(s, result)) {
String str = addStr(fs.get(s), result);
if (!result.equals(str)) {
result = str;
bflag = true;
}
}
}
}
return result;
}


void getClosure() {
System.out
.println("Please enter the seed of closure, typing 'end' will exit the loop");
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String str = in.nextLine();
if (str.toLowerCase().equals("end"))
break;
String s = getClosure(str, funs);
System.out.println("The closure of " + str + " is " + s);
System.out.println("");
System.out
.println("Please continue to enter the seed of closure, typing 'end' will exit the loop");
}
}


void outputCols_Funs() {
System.out.print("the Attributes are\t:");
System.out.println(cols);
}


void closureKeys() {
inputCols();
inputFuns();
outputCols_Funs();
getClosure();
getKeys();
}


void getKeys() {
String allleft = "", allright = "";
for (String s : funs.keySet()) {
allleft = allleft + s;
allright = allright + funs.get(s);
}
String lefts = "", leftrights = "", nolfs = "";
String allcol = "";
for (String s : cols) {
allcol = allcol + s;
if ((allleft.contains(s)) && (!allright.contains(s)))
lefts = lefts + s;
else if ((allleft.contains(s)) && (allright.contains(s)))
leftrights = leftrights + s;
else if ((!allleft.contains(s)) && (!allright.contains(s)))
nolfs = nolfs + s;
}


keys.clear();
String s = getClosure(lefts + nolfs, funs);
if (isInclude(allcol, s)) {
System.out.println("The only key is :" + lefts + nolfs);
keys.add(lefts + nolfs);
return;
}
char[] lrs = leftrights.toCharArray();
for (int i = 1; i <= lrs.length; i++) {
List L = combine(lrs, i);
for (Object chs : L) {
String tstr = lefts + nolfs + String.copyValueOf((char[]) chs);
s = getClosure(tstr, funs);
if (isInclude(allcol, s)) {
boolean flag = true;
for (String tmp : keys)
if (isInclude(tmp, tstr)) {
flag = false;
break;
}
if (flag)
keys.add(tstr);
}
}
}
System.out.println("The keys are" + keys);
}


public List combine(char[] a, int m) {
int n = a.length;
List result = new ArrayList();
char[] bs = new char[n];
for (char i = 0; i < n; i++) {
bs[i] = 0;
}
for (int i = 0; i < m; i++) {
bs[i] = 1;
}
boolean flag = true;
boolean tempFlag = false;
int pos = 0;
int sum = 0;
do {
sum = 0;
pos = 0;
tempFlag = true;
result.add(print(bs, a, m));
for (int i = 0; i < n - 1; i++) {
if (bs[i] == 1 && bs[i + 1] == 0) {
bs[i] = 0;
bs[i + 1] = 1;
pos = i;
break;
}
}
for (int i = 0; i < pos; i++) {
if (bs[i] == 1) {
sum++;
}
}
for (int i = 0; i < pos; i++) {
if (i < sum) {
bs[i] = 1;
} else {
bs[i] = 0;
}
}
for (int i = n - m; i < n; i++) {
if (bs[i] == 0) {
tempFlag = false;
break;
}
}
if (tempFlag == false) {
flag = true;
} else {
flag = false;
}
} while (flag);
result.add(print(bs, a, m));
return result;
}


private void print(List l) {
System.out.println("There are" + l.size());
for (int i = 0; i < l.size(); i++) {
char[] a = (char[]) l.get(i);
for (int j = 0; j < a.length; j++) {
System.out.print(a[j] + "\t");
}
System.out.println();
}
}


private char[] print(char[] bs, char[] a, int m) {
char[] result = new char[m];
int pos = 0;
for (int i = 0; i < bs.length; i++) {
if (bs[i] == 1) {
result[pos] = a[i];
pos++;
}
}
return result;
}


}
原创粉丝点击