题目1-将数组存放在集合中
现有如下字符串元素:[“aaa”, “bbb”, “aaa”, “aaa”, “ccc”, “bbb”],请将所有的元素按顺序存入ArrayList集合中,并遍历集合查看存储结果。
package cn.kox.homework;
import java.util.ArrayList;
/**
* @ClassName: codeHomework01
* @Author: Kox
* @Data: 2023/1/24
* @Sketch:
*/
public class codeHomework01 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
String[] arr = {"aaa", "bbb", "aaa", "aaa", "ccc", "bbb"};
for (int i = 0; i < arr.length; i++) {
list.add(arr[i]);
}
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
System.out.println(s);
}
}
}
题目2-教师信息查看
请定义教师(Teacher)类,属性包含姓名和专业名称。将若干教师对象存入ArrayList集合中,并遍历集合打印教师信息,格式如下:
姓名:赵老师, 专业:javase
姓名:钱老师, 专业:javaee
姓名:孙老师, 专业:php
姓名:李老师, 专业:python
- Teacher类
package cn.kox.homework;
/**
* @ClassName: Teacher
* @Author: Kox
* @Data: 2023/1/24
* @Sketch:
*/
public class Teacher {
private String name;
private String major;
public Teacher() {
}
public Teacher(String name, String major) {
this.name = name;
this.major = major;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
package cn.kox.homework;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @ClassName: codeHomework02
* @Author: Kox
* @Data: 2023/1/24
* @Sketch:
*/
public class codeHomework02 {
public static void main(String[] args) {
ArrayList<Teacher> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
Teacher t1 = new Teacher();
System.out.println("输入姓名");
String name = sc.next();
t1.setName(name);
System.out.println("输入专业");
String major = sc.next();
t1.setMajor(major);
list.add(t1);
}
for (int i = 0; i < list.size(); i++) {
String name = list.get(i).getName();
String major = list.get(i).getMajor();
System.out.println(name + "," + major);
}
}
}
题目3-员工信息删改查操作
有如下员工信息:
姓名:张三,工资:3000
姓名:李四,工资:3500
姓名:王五,工资:4000
姓名:赵六,工资:4500
姓名:田七,工资:5000
先需要将所有的员工信息都存入ArrayList集合中,并完成如下操作:
1、判断是否有姓名为“王五”的员工,如果有,改名为“王小五”
2、判断是否有姓名为“赵六”的员工,如果有,将其删除
3、给姓名为“田七”的员工,涨500工资
- Worker
package cn.kox.homework;
/**
* @ClassName: Worker
* @Author: Kox
* @Data: 2023/1/24
* @Sketch:
*/
public class Worker {
private String name;
private int salary;
public Worker() {
}
public Worker(String name, int salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
package cn.kox.homework;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @ClassName: codeHomework03
* @Author: Kox
* @Data: 2023/1/24
* @Sketch:
*/
public class codeHomework03 {
public static void main(String[] args) {
ArrayList<Worker> list = new ArrayList<>();
Worker w1 = new Worker("张三", 3000);
Worker w2 = new Worker("李四", 3500);
Worker w3 = new Worker("王五", 4000);
Worker w4 = new Worker("赵六", 4500);
Worker w5 = new Worker("田七", 5000);
list.add(w1);
list.add(w2);
list.add(w3);
list.add(w4);
list.add(w5);
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入相关操作 1. 删除员工 2. 修改员工信息 3. 查看员工信息 4. 退出");
int choose = sc.nextInt();
switch (choose) {
case 1 -> {
delWorker(list);
}
case 2 -> {
updateWorker(list);
}
case 3 -> {
view(list);
}
case 4 -> {
System.out.println("已退出");
System.exit(0);
}
default -> System.out.println("输入错误!");
}
}
}
public static void updateWorker(ArrayList<Worker> list) {
Scanner sc = new Scanner(System.in);
System.out.println("输入员工姓名");
String name = sc.next();
int index = judgeName(list, name);
if (index == -1) {
System.out.println("没有该员工!");
} else {
System.out.println("1. 修改姓名 2. 修改工资");
int choose = sc.nextInt();
switch (choose) {
case 1 -> {
System.out.println("输入将要修改的姓名");
String newName = sc.next();
list.get(index).setName(newName);
System.out.println("修改成功");
}
case 2 -> {
System.out.println("输入将要修改的工资");
int salary = sc.nextInt();
list.get(index).setSalary(salary);
System.out.println("修改成功");
}
}
}
}
public static void delWorker(ArrayList<Worker> list) {
Scanner sc = new Scanner(System.in);
System.out.println("输入员工姓名");
String name = sc.next();
int index = judgeName(list, name);
if (index == -1) {
System.out.println("没有该员工!");
} else {
list.remove(index);
System.out.println("已成功删除" + name);
}
}
public static int judgeName(ArrayList<Worker> list, String name) {
for (int i = 0; i < list.size(); i++) {
String n = list.get(i).getName();
if (name.equals(n)) {
return i;
}
}
return -1;
}
public static void view(ArrayList<Worker> list) {
for (int i = 0; i < list.size(); i++) {
String name = list.get(i).getName();
int salary = list.get(i).getSalary();
System.out.println(name + "," + salary);
}
}
}
题目4(综合)-图书管理系统
利用面向对象的思想设计一个图书管理系统。图书的属性有:编号,书名,作者,价格。要求提供如下功能:
1、提供操作菜单,可以选择要进行的操作。
2、可以添加图书,添加图书时,编号需要唯一,添加成功,返回到菜单。
3、可以查询图书,显示所有图书信息,然后返回到菜单。
4、可以根据书名,查询单本图书信息,显示信息后,返回到菜单。
5、可以删除图书,通过编号删除,删除成功后,返回到菜单。
6、可以修改图书的信息,但编号不可以修改,修改成功后,返回到菜单。
7、可以退出系统,结束程序运行。
提示:
1、所有图书信息由键盘录入
2、图书的价格可以定义为字符串类型,因为在键盘录入时,不可以先使用nextInt()方法获取整数然后再使用next()方法获取字符串,这样会导致next()方法获取不到数据。
- Book类
package cn.kox.homework;
/**
* @ClassName: Book
* @Author: Kox
* @Data: 2023/1/24
* @Sketch:
*/
public class Book {
private String id;
private String name;
private String author;
private float price;
public Book() {
}
public Book(String id, String name, String author, float price) {
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
package cn.kox.homework;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @ClassName: codeHomework04
* @Author: Kox
* @Data: 2023/1/24
* @Sketch:
*/
public class codeHomework04 {
public static void main(String[] args) {
ArrayList<Book> list = new ArrayList<>();
Book b1 = new Book("001", "献给阿尔吉侬的花束", "丹尼尔·凯斯", 33);
list.add(b1);
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("-----图书管理系统-----");
System.out.println("请输入操作选项 1:添加图书 2:查询所有图书信息 3:查询单本图书信息 4:删除图书 5:修改图书信息 6:退出");
int choose = sc.nextInt();
switch (choose) {
case 1 -> {
addBook(list);
System.out.println();
}
case 2 -> {
selectAll(list);
System.out.println();
}
case 3 -> {
select(list);
System.out.println();
}
case 4 -> {
del(list);
System.out.println();
}
case 5 -> {
update(list);
System.out.println();
}
case 6 -> {
System.out.println("已退出");
System.exit(0);
}
default -> {
System.out.println("没有这个选项");
System.out.println();
}
}
}
}
// 1. 添加图书
public static void addBook(ArrayList<Book> list) {
Scanner sc = new Scanner(System.in);
Book b = new Book();
while (true) {
System.out.println("输入添加的编号");
String id = sc.next();
if (id.length() != 3) {
System.out.println("编号命名不合法,请输入三位数的格式!");
} else if (getID(list, id) != -1) {
System.out.println(id + "编号已存在!");
} else {
b.setId(id);
break;
}
}
System.out.println("输入添加的书名");
String name = sc.next();
b.setName(name);
System.out.println("输入添加的作者");
String author = sc.next();
b.setAuthor(author);
System.out.println("输入添加的价格");
int price = sc.nextInt();
b.setPrice(price);
list.add(b);
System.out.println("添加成功");
}
// 2. 查看所有图书信息
public static void selectAll(ArrayList<Book> list) {
System.out.println("编号\t" + "书名\t" + "作者\t" + "价格");
for (int i = 0; i < list.size(); i++) {
Book b = list.get(i);
String id = b.getId();
String name = b.getName();
String author = b.getAuthor();
float price = b.getPrice();
System.out.printf("%s\t%s\t%s\t%s\n",id, name, author, price);
}
}
// 3, 查询单本图书信息
public static void select(ArrayList<Book> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入书名");
String name = sc.next();
int index = getName(list, name);
if (index == -1) {
System.out.println("没有这本书");
} else {
Book b = list.get(index);
String id = b.getId();
String author = b.getAuthor();
float price = b.getPrice();
System.out.printf("%s\t%s\t%s\t%s\n",id, name, author, price);
}
}
// 4. 删除图书
public static void del(ArrayList<Book> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入编号");
String id = sc.next();
int index = getID(list, id);
if (index == -1) {
System.out.println("没有这本书");
} else {
System.out.println("已删除编号为" + id + "的图书");
list.remove(index);
}
}
// 5. 修改图书信息
public static void update(ArrayList<Book> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入编号");
String id = sc.next();
int index = getID(list, id);
Book b = list.get(index);
if (index == -1) {
System.out.println("灭有这本书");
} else {
System.out.println("修改书名");
String name = sc.next();
b.setName(name);
System.out.println("修改作者");
String author = sc.next();
b.setAuthor(author);
System.out.println("修改价格");
int price = sc.nextInt();
b.setPrice(price);
System.out.println("修改成功");
}
}
public static int getName(ArrayList<Book> list, String name) {
for (int i = 0; i < list.size(); i++) {
String n = list.get(i).getName();
if (name.equals(n)) {
return i;
}
}
return -1;
}
public static int getID(ArrayList<Book> list, String id) {
for (int i = 0; i < list.size(); i++) {
String getId = list.get(i).getId();
if (id.equals(getId)) {
return i;
}
}
return -1;
}
}