Express
package cn.kaikeba.bean;
import java.util.Objects;
public class Express {
private String company;
private int number;
private int code;
@Override
public String toString() {
return "Bean{" +
"company='" + company + '\'' +
", number=" + number +
", code=" + code +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Express bean = (Express) o;
return number == bean.number;
}
public Express() {
}
public Express(String company, int number, int code) {
this.company = company;
this.number = number;
this.code = code;
}
@Override
public int hashCode() {
return Objects.hash(number);
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
dao
import cn.kaikeba.bean.Express;
import java.util.Random;
public class Dao {
static Express e = new Express();
static Views v = new Views();
static Random random = new Random();
//先创建一个快递柜,用二维数组实现
static Express[][] data = new Express[10][];
{
for (int i = 0; i < 10; i++) {
data[i] = new Express[10];
}
}
/*
存快递
* */
public static void add(Express e){
if(findByNumber(e) == null) {
int code = random.nextInt(900000) + 100000;
e.setCode(code);
a:for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if( data[i][j] == null) {
data[i][j] = e;
break a;
}
}
}
}else{
v.wrongData();
}
}
/*
删快递
* */
public static int delete(Express e){
if(findByNumber(e) != null){
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if(e.equals(data[i][j])){
data[i][j] = null;
}
}
}
return 0;
} else{
return 1;
}
}
/*
改快递
* */
public static Boolean update(Express olde,Express newe){
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if(olde.equals(data[i][j])){
data[i][j] = newe;
int code = random.nextInt(900000) + 100000;
data[i][j].setCode(code);
}
}
}
return true;
}
/*
查询所有快递
* */
public static void printAll(){
int count = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if(data[i][j] != null){
System.out.println("第" + (i+1) + "行,第" + (j+1) + "列的快递信息如下:");
System.out.println( "快递单号:" + data[i][j].getNumber() + ",快递公司:" + data[i][j].getCompany() + ",取件码:" + data[i][j].getCode() + "。");
count++;
}
}
}
if(count == 0){
v.nothing();
}
}
/*
通过快递单号查找快递
* */
public static Express findByNumber(Express e){
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if(e.equals(data[i][j])){
return data[i][j];
}
}
}
return null;
}
/*
通过取件码查询快递
* */
public static Express findByCode(Express e){
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if(e.getCode() == data[i][j].getCode()){
return data[i][j];
}
}
}
return null;
}
}
main
import cn.kaikeba.bean.Express;
public class Main {
static Express e = new Express();
static Views v = new Views();
static Dao dao = new Dao();
public static void main(String[] args) {
v.welcome();
a:while(true) {
int menu = v.menu();
switch(menu){
case 1:
//快递员
cClient();
break;
case 2:
//用户
uClient();
break;
case 0:
break a;
}
}
v.bye();
}
private static void cClient() {
a:while(true){
int s = v.cGod();
switch(s){
case 1:
e = v.add();
dao.add(e);
v.success();
break;
case 2:
v.delete();
break;
case 3:
v.update();
break;
case 4:
v.printAll();
break;
case 0:
break a;
}
}
}
private static void uClient() {
e = v.uGod();
Express e1 = dao.findByCode(e);
if(e1 == null){ v.wrongData(); }
v.success();
dao.delete(e1);
}
}
泛型
泛型,通俗的理解可以看做“任意的数据类型”;
//泛型类
public class Demo<T,B> {
private T name;
private B age;
}//这里的Demo就是一个泛型类
//泛型方法和泛型接口
public abstract class Person <T extends Demo > {//这里的Person是一个
//泛型接口
//这里面还用了extends来限制类型为Demo
public abstract T say();//say()方法是一个泛型方法
}
下面的案例说明了多态不包含泛型的字符关系
public class Person<T> {
}//创建了一个T泛型类Person
public class Nurse<C> {
}//创建了一个C泛型类Nurse
public class T {
}//将T设置为一个类
public class C extends T {
}//将C设置为T的一个子类
public class Demo {
Person<T> p2 = new Nurse<C>();
//因为Nurse不是Person的子类,所以多态没有成功
//注意:不会因为个字的泛型是字符关系形成多态
}