1.学习内容
了解了泛型的概念,掌握如何使用泛型,泛型是将数据类型参数化,使一个类或一个方法可以在多种不同的类型对象中操作。
package Classwork;
public class Fanxin01<T> {
private T all;
public T all(){
return all;
}
public T getAll() {
return all;
}
public void setAll(T all) {
this.all = all;
}
public static void main(String[] args) {
Fanxin01<String> a=new Fanxin01<String>();
Fanxin01<Integer> b=new Fanxin01<Integer>();
a.setAll("ty");
System.out.println(a.getAll());
b.setAll(18);
System.out.println(b.getAll());
a.setAll("169cm");
System.out.println(a.getAll());
a.setAll("2003.04.29");
System.out.println(a.getAll());
a.setAll("打打游戏,听听音乐,出去玩");
System.out.println("爱好:"+a.getAll());
}
}
了解容器类,掌握容器接口Collection、列表接口List、集合接口Set。
package Classwork;
import java.util.*;
class Stringstack{
private LinkedList<String> ld=new LinkedList<String>();
//private 使ld对象仅在Stringstack类中使用
public void push(String name){
ld.addFirst(name);
}
public String pop(){
return ld.removeFirst();
}
public boolean isEmpty(){
return ld.isEmpty();
}
}
public class LinkedList01 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Stringstack stack=new Stringstack();
System.out.println("请输入数据(stop 结束)");
while (true){
String intput=sc.next();
if(intput.equals("stop"))
break;
stack.push(intput);
}
System.out.println("先进后出的顺序:");
while (!stack.isEmpty())
System.out.println(stack.pop());
}
}
做了关于小吃店的项目,还未完善!!!
package Snackbarsystem;
import java.util.Scanner;
public class loginFood extends managerFood {
public static void main(String[] args) throws Exception {
System.out.println("请输入您的身份:");
for (int i = 0; i < 3; i++) {
System.out.println();
}
System.out.println("1.管理员 2.用户");
Scanner x = new Scanner(System.in);
int identity = x.nextInt();
if (identity == 1) {
managerFood m = new managerFood();
m.show();
}
if (identity == 2) {
userFood u = new userFood();
u.user();
}
}
}
package Snackbarsystem;
public class Food {
private int id;
private String name;
private int price;
private String taste;
public Food(){
}
@Override
public String toString() {
return
" "+id+"\t "+name+"\t "+price+"\t "+taste+"\t \n";
}
public Food(int id, String name, int price, String taste){
this.id=ida;
this.name=name;
this.price=price;
this.taste=taste;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getTaste() {
return taste;
}
public void setTaste(String taste) {
this.taste = taste;
}
}
package Snackbarsystem;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class managerFood {
public void show()throws Exception {
ArrayList<Food> f = new ArrayList<Food>();
Food f1 = new Food(01, "榴莲酥", 5, "咸甜");
Food f2 = new Food(02, "西瓜捞", 8, "凉甜");
Food f3 = new Food(03, "蛋焗南瓜", 10, "鲜甜");
f.add(f1);
f.add(f2);
f.add(f3);
while (true)//无限循环
{
System.out.println("----小吃店管理系统----");
System.out.println("1.添加小吃");
System.out.println("2.删除小吃");
System.out.println("3.更改小吃");
System.out.println("4.查看小吃");
System.out.println("5.退出");
System.out.println("请选择系统功能:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n == 1) {
addFood(f);
} else if (n == 2) {
deleteFood(f);
} else if (n == 3) {
changeFood(f);
} else if (n == 4) {
checkFood(f);
} else if (n == 5) {
System.out.println("谢谢使用小京系统");
break;
}
}
}
public static void addFood(ArrayList<Food> f)throws Exception{
Scanner sc=new Scanner(System.in);
System.out.println("请输入小吃编号:");
int id=sc.nextInt();
boolean flag=true;
for (int i=0;i<f.size();i++){
Food a=f.get(i);//遍历数组列表
if (a.getId()==id){
flag=false;
System.out.println("该小吃已存在");
break;
}
}
if (flag){
System.out.println("请输入小吃名字:");
String Name=sc.next();
System.out.println("请输入小吃价格");
int Price=sc.nextInt();
System.out.println("请输入小吃口味");
String Taste=sc.next();
Food fo=new Food();
fo.setId(id);
fo.setName(Name);
fo.setPrice(Price);
fo.setTaste(Taste);
f.add(fo);
try(
BufferedWriter in=new BufferedWriter(new FileWriter("D:\\idea java\\Foodmagaer\\food1.txt",true));
){
PrintWriter fw=new PrintWriter(in);
fw.print(fo);
fw.flush();
}
System.out.println("添加成功!");
}
}
public static void deleteFood(ArrayList<Food> f)throws Exception{
Scanner sc=new Scanner(System.in);
System.out.println("请输入要删除的小吃编号");
int num=sc.nextInt();
int all=-1;//创建索引,找数组列表中以有数据
for (int i=0;i<f.size();i++){
Food food=f.get(i);//遍历数组列表
if (food.getId()==num) //判断改小吃是否存在
{
all=i;
break;
}
}
if(all==-1){
System.out.println("无此小吃,请重新选择");
}
else {
f.remove(all);
System.out.println("删除小吃成功");
}
}
public static void changeFood(ArrayList<Food> f)throws Exception{
Scanner sc=new Scanner(System.in);
System.out.println("请输入你要修改的小吃编号");
int id=sc.nextInt();
int all=-1;//创建索引,找数组列表中以有数据
for (int i=0;i<f.size();i++){
Food fo=f.get(i);
if (fo.getId()==id)//判断改小吃是否存在
{
all=1;
break;
}
}
if (all==-1){
System.out.println("您要改变的小吃不存在");
}
else{
System.out.println("请输入新小吃名字:");
String Name=sc.next();
System.out.println("请输入新小吃价格");
int Price=sc.nextInt();
System.out.println("请输入新小吃口味");
String Taste=sc.next();
Food fo=new Food();
fo.setId(id);
fo.setName(Name);
fo.setPrice(Price);
fo.setTaste(Taste);
f.set(id-1,fo);//将已完善的小吃在数组列表中创建
System.out.println("修改小吃信息成功");
}
}
public static void checkFood(ArrayList<Food> f){
if (f.size()==0){
System.out.println("暂无小吃信息");
return;//停止程序运行下去
}
System.out.println("编号:\t小吃名:\t 价格:\t 口味:");
// \t将光标移入下一行
for (int i=0;i<f.size();i++){
Food food=f.get(i);
System.out.println(" "+food.getId()+"\t "+food.getName()+"\t "+food.getPrice()+"\t "+food.getTaste()+"\t ");
}
}
}
package Snackbarsystem;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class userFood {
public void user() throws Exception {
while (true) {
System.out.println("----小吃店用户系统----");
System.out.println("1.查看小吃");
System.out.println("2.查看订餐电话");
System.out.println("3.查看本店介绍");
System.out.println("4.退出");
Scanner ch = new Scanner(System.in);
int choose = ch.nextInt();
if (choose == 1) {
System.out.println("----欢迎查看小吃----");
try (
BufferedReader out =new BufferedReader (new FileReader("D:\\idea java\\Foodmagaer\\food1.txt"));
) {
String check;
while ((check = out.readLine()) != null) {
System.out.println(check);
}
}
}
if (choose == 2) {
System.out.println("订餐热线:100820-820-110");
}
if (choose == 3) {
try (
BufferedReader bf = new BufferedReader(new FileReader("D:\\idea java\\Foodmagaer\\src\\File\\Food.txt"));
) {
String introduce;
while ((introduce = bf.readLine()) != null)
{
System.out.println(introduce);
}
}
}
if (choose == 4) {
System.out.println("感谢使用小京系统");
break;
}
}
}
}
2.学习方式
主要将精力放在练习中,在实践中检验自己学过的知识。